Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sync concurrency for send translation #151

Open
wants to merge 1 commit into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/modules/ManualJobSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ class ManualJobSync extends BaseJob
public const DELAY_IN_SECONDS = 0;
public const PRIORITY = 512;

private const SUPPORTED_JOBS = [
public const SUPPORTED_JOBS = [
FetchJobStatusFromConnector::class,
FetchInstantJobTranslationsFromConnector::class,
FetchVerifiedJobTranslationsFromConnector::class,
FetchTranslationFromConnector::class,
SendJobToConnector::class,
SendTranslationToConnector::class,
];

/**
Expand Down
122 changes: 122 additions & 0 deletions tests/integration/modules/ManualJobSyncCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
use LiltConnectorSDK\Model\JobResponse;
use lilthq\craftliltplugin\Craftliltplugin;
use lilthq\craftliltplugin\elements\Job;
use lilthq\craftliltplugin\modules\FetchInstantJobTranslationsFromConnector;
use lilthq\craftliltplugin\modules\FetchJobStatusFromConnector;
use lilthq\craftliltplugin\modules\FetchTranslationFromConnector;
use lilthq\craftliltplugin\modules\FetchVerifiedJobTranslationsFromConnector;
use lilthq\craftliltplugin\modules\ManualJobSync;
use lilthq\craftliltplugin\modules\SendJobToConnector;
use lilthq\craftliltplugin\modules\SendTranslationToConnector;
Expand All @@ -22,9 +25,21 @@
use lilthq\craftliltplugintests\integration\AbstractIntegrationCest;
use lilthq\tests\fixtures\EntriesFixture;
use PHPUnit\Framework\Assert;
use yii\db\Expression;

class ManualJobSyncCest extends AbstractIntegrationCest
{
const RESERVED_STATUS = 2;

const SUPPORTED_JOBS = [
FetchJobStatusFromConnector::class,
FetchInstantJobTranslationsFromConnector::class,
FetchVerifiedJobTranslationsFromConnector::class,
FetchTranslationFromConnector::class,
SendJobToConnector::class,
SendTranslationToConnector::class,
];

public function _fixtures(): array
{
return [
Expand Down Expand Up @@ -281,6 +296,113 @@ public function testDelayedJob(IntegrationTester $I): void
Assert::assertEquals(0, $jobDetails['delay']);
}

public function testDelayedJobAllTypes(IntegrationTester $I): void
{
foreach (self::SUPPORTED_JOBS as $supportedJob) {
$job = $I->createJob([
'title' => 'Awesome test job',
'elementIds' => [123, 456, 789],
'targetSiteIds' => '*',
'sourceSiteId' => Craftliltplugin::getInstance()->languageMapper->getSiteIdByLanguage('en-US'),
'translationWorkflow' => CraftliltpluginParameters::TRANSLATION_WORKFLOW_INSTANT,
'status' => Job::STATUS_IN_PROGRESS,
'versions' => [],
'authorId' => 1,
]);

$queue = Craft::$app->getQueue();
$queueId = $queue
->priority(1024)
->delay(99999)
->ttr(null)
->push(
new $supportedJob(
['jobId' => $job->id]
)
);

$I->executeQueue(
ManualJobSync::class,
[
'jobIds' => [$job->id],
]
);

$jobInfos = Craft::$app->queue->getJobInfo();
Assert::assertNotEmpty($jobInfos);

$I->assertJobInQueue(
new $supportedJob(
['jobId' => $job->id]
),
$job->status
);

$jobDetails = Craft::$app->queue->getJobDetails((string)$queueId);
Assert::assertEquals(0, $jobDetails['delay']);
}
}

public function testReservedJobAllTypes(IntegrationTester $I): void
{
foreach (self::SUPPORTED_JOBS as $supportedJob) {
$I->clearQueue();

$job = $I->createJob([
'title' => 'Awesome test job',
'elementIds' => [123, 456, 789],
'targetSiteIds' => '*',
'sourceSiteId' => Craftliltplugin::getInstance()->languageMapper->getSiteIdByLanguage('en-US'),
'translationWorkflow' => CraftliltpluginParameters::TRANSLATION_WORKFLOW_INSTANT,
'status' => Job::STATUS_IN_PROGRESS,
'versions' => [],
'authorId' => 1,
]);

$queue = Craft::$app->getQueue();
$queueId = $queue
->priority(1024)
->delay(99999)
->ttr(null)
->push(
new $supportedJob(
['jobId' => $job->id]
)
);

$db = Craft::$app->getDb();
$db->createCommand()
->update(Table::QUEUE,
[
'timeUpdated' => new Expression('UNIX_TIMESTAMP()')
],
['id' => $queueId])
->execute();

$I->executeQueue(
ManualJobSync::class,
[
'jobIds' => [$job->id],
]
);

$jobInfos = Craft::$app->queue->getJobInfo();
Assert::assertNotEmpty($jobInfos);

Assert::assertCount(1, $jobInfos);

// Job still in reserved
Assert::assertEquals(self::RESERVED_STATUS, $jobInfos[0]['status']);

$I->assertJobInQueue(
new $supportedJob(
['jobId' => $job->id]
),
$job->status
);
}
}

public function testFailedJob(IntegrationTester $I): void
{
$job = $I->createJob([
Expand Down
Loading