Skip to content

Commit

Permalink
Merge pull request #145 from lilt/3.x-disable-auto-sync
Browse files Browse the repository at this point in the history
Configuration to enable/disable automatic sync jobs
  • Loading branch information
hadomskyi authored Apr 8, 2024
2 parents 29755ce + c6828d2 commit 43841ef
Show file tree
Hide file tree
Showing 18 changed files with 814 additions and 48 deletions.
24 changes: 23 additions & 1 deletion .github/workflows/craft-versions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,30 @@ jobs:
"3.8.5",
"3.8.6",
"3.8.7",
"3.8.8",
"3.8.9",
"3.8.10",
"3.8.10.1",
"3.8.10.2",
"3.8.11",
"3.8.12",
"3.8.13",
"3.8.14",
"3.8.15",
"3.8.16",
"3.8.17",
"3.9.0",
"3.9.1",
"3.9.2",
"3.9.3",
"3.9.4",
"3.9.5",
"3.9.6",
"3.9.10",
"3.9.11",
"3.9.12",
]
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- name: Set the value
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ ubuntu-latest ]
os: [ ubuntu-20.04 ]
scenario: [
"cypress/e2e/jobs/copy-source-text-flow/filters.cy.js",
"cypress/e2e/jobs/copy-source-text-flow/success-path-multiple.cy.js",
Expand Down
14 changes: 7 additions & 7 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
- "*"
jobs:
tests-php-72:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- name: Set the value
Expand All @@ -23,7 +23,7 @@ jobs:
run: make test

tests-php-73:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- name: Set the value
Expand All @@ -38,7 +38,7 @@ jobs:
run: make test

tests-php-74:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- name: Set the value
Expand All @@ -53,7 +53,7 @@ jobs:
run: make test

tests-php-80:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- name: Set the value
Expand All @@ -68,7 +68,7 @@ jobs:
run: make test

tests-php-72-guzzle-6:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- name: Set the value
Expand All @@ -85,7 +85,7 @@ jobs:
run: make test

tests-php-80-mysql-80:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- name: Set the value
Expand All @@ -101,7 +101,7 @@ jobs:
run: make test

tests-php-latest:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- name: Set the value
Expand Down
12 changes: 12 additions & 0 deletions src/controllers/PostConfigurationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public function actionInvoke(): Response
$request->getBodyParam('copyEntriesSlugFromSourceToTarget') ?? '0'
);

// queueEachTranslationFileSeparately
$queueEachTranslationFileSeparately = $request->getBodyParam('queueEachTranslationFileSeparately');
if (empty($queueEachTranslationFileSeparately)) {
$queueEachTranslationFileSeparately = 0;
Expand All @@ -108,6 +109,17 @@ public function actionInvoke(): Response
(string)$queueEachTranslationFileSeparately
);

// queueDisableAutomaticSync
$queueDisableAutomaticSync = $request->getBodyParam('queueDisableAutomaticSync');
if (empty($queueDisableAutomaticSync)) {
$queueDisableAutomaticSync = 0;
}

Craftliltplugin::getInstance()->settingsRepository->save(
SettingsRepository::QUEUE_DISABLE_AUTOMATIC_SYNC,
(string)$queueDisableAutomaticSync
);

$settingsRequest = new SettingsRequest();
$settingsRequest->setProjectPrefix(
$request->getBodyParam('projectPrefix')
Expand Down
53 changes: 33 additions & 20 deletions src/modules/FetchJobStatusFromConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use lilthq\craftliltplugin\elements\Job;
use lilthq\craftliltplugin\records\JobRecord;
use lilthq\craftliltplugin\records\TranslationRecord;
use lilthq\craftliltplugin\services\repositories\SettingsRepository;

class FetchJobStatusFromConnector extends AbstractRetryJob
{
Expand Down Expand Up @@ -107,18 +108,24 @@ public function execute($queue): void
}

if (!$isJobFinished) {
Queue::push(
(new FetchJobStatusFromConnector(
[
'jobId' => $this->jobId,
'liltJobId' => $this->liltJobId,
]
)),
self::PRIORITY,
self::getDelay(),
self::TTR
$queueDisableAutomaticSync = (bool) Craftliltplugin::getInstance()->settingsRepository->get(
SettingsRepository::QUEUE_DISABLE_AUTOMATIC_SYNC
);

if (!$queueDisableAutomaticSync) {
Queue::push(
(new FetchJobStatusFromConnector(
[
'jobId' => $this->jobId,
'liltJobId' => $this->liltJobId,
]
)),
self::PRIORITY,
self::getDelay(),
self::TTR
);
}

$mutex->release($mutexKey);
$this->markAsDone($queue);

Expand Down Expand Up @@ -177,18 +184,24 @@ function (TranslationResponse $connectorTranslation) {
return;
}

Queue::push(
(new FetchJobStatusFromConnector(
[
'jobId' => $this->jobId,
'liltJobId' => $this->liltJobId,
]
)),
self::PRIORITY,
self::getDelay(),
self::TTR
$queueDisableAutomaticSync = (bool) Craftliltplugin::getInstance()->settingsRepository->get(
SettingsRepository::QUEUE_DISABLE_AUTOMATIC_SYNC
);

if (!$queueDisableAutomaticSync) {
Queue::push(
(new FetchJobStatusFromConnector(
[
'jobId' => $this->jobId,
'liltJobId' => $this->liltJobId,
]
)),
self::PRIORITY,
self::getDelay(),
self::TTR
);
}

$mutex->release($mutexKey);
$this->markAsDone($queue);

Expand Down
18 changes: 17 additions & 1 deletion src/modules/QueueManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
use Craft;
use craft\queue\BaseJob;
use craft\helpers\Queue as CraftHelpersQueue;
use lilthq\craftliltplugin\Craftliltplugin;
use lilthq\craftliltplugin\elements\Job;
use lilthq\craftliltplugin\parameters\CraftliltpluginParameters;
use lilthq\craftliltplugin\records\JobRecord;
use lilthq\craftliltplugin\services\repositories\SettingsRepository;

class QueueManager extends BaseJob
{
Expand All @@ -25,8 +27,17 @@ class QueueManager extends BaseJob
*/
public function execute($queue): void
{
$queueDisableAutomaticSync = (bool) Craftliltplugin::getInstance()->settingsRepository->get(
SettingsRepository::QUEUE_DISABLE_AUTOMATIC_SYNC
);

if ($queueDisableAutomaticSync) {
// skip because automatic sync is disabled
return;
}

$mutex = Craft::$app->getMutex();
$mutexKey = __CLASS__ . '_' . __FUNCTION__;
$mutexKey = self::getMutexKey();
if (!$mutex->acquire($mutexKey)) {
Craft::warning('Lilt queue manager is already running');

Expand Down Expand Up @@ -104,4 +115,9 @@ protected function defaultDescription(): ?string
{
return Craft::t('app', 'Lilt queue manager');
}

public static function getMutexKey(): string
{
return __CLASS__ . '_' . __FUNCTION__;
}
}
21 changes: 14 additions & 7 deletions src/modules/SendTranslationToConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use lilthq\craftliltplugin\models\TranslationModel;
use lilthq\craftliltplugin\records\TranslationRecord;
use lilthq\craftliltplugin\services\handlers\commands\SendTranslationCommand;
use lilthq\craftliltplugin\services\repositories\SettingsRepository;
use Throwable;

class SendTranslationToConnector extends AbstractRetryJob
Expand Down Expand Up @@ -180,15 +181,21 @@ function (TranslationModel $translationModel) {
);
}

Queue::push(
(new FetchJobStatusFromConnector([
'jobId' => $command->getJob()->id,
'liltJobId' => $command->getJob()->liltJobId,
])),
FetchJobStatusFromConnector::PRIORITY,
10
$queueDisableAutomaticSync = (bool) Craftliltplugin::getInstance()->settingsRepository->get(
SettingsRepository::QUEUE_DISABLE_AUTOMATIC_SYNC
);

if (!$queueDisableAutomaticSync) {
Queue::push(
(new FetchJobStatusFromConnector([
'jobId' => $command->getJob()->id,
'liltJobId' => $command->getJob()->liltJobId,
])),
FetchJobStatusFromConnector::PRIORITY,
10
);
}

$this->markAsDone($queue);
$this->release();

Expand Down
21 changes: 14 additions & 7 deletions src/services/handlers/SendJobToLiltConnectorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,21 @@ public function __invoke(Job $job): void
'Job uploaded to Lilt Platform'
);

Queue::push(
(new FetchJobStatusFromConnector([
'jobId' => $job->id,
'liltJobId' => $jobLilt->getId(),
])),
FetchJobStatusFromConnector::PRIORITY,
10 //10 seconds for fist job
$queueDisableAutomaticSync = (bool) Craftliltplugin::getInstance()->settingsRepository->get(
SettingsRepository::QUEUE_DISABLE_AUTOMATIC_SYNC
);

if (!$queueDisableAutomaticSync) {
// push fetch status job from connector
Queue::push(
(new FetchJobStatusFromConnector([
'jobId' => $job->id,
'liltJobId' => $jobLilt->getId(),
])),
FetchJobStatusFromConnector::PRIORITY,
10 //10 seconds for fist job
);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ public function send(SendTranslationCommand $sendTranslationCommand): void
);
}

$translation->status = TranslationRecord::STATUS_IN_PROGRESS;
$translation->sourceContent = $content;
$translation->translatedDraftId = $draft->id;
$translation->markAttributeDirty('sourceContent');
Expand Down
21 changes: 20 additions & 1 deletion src/services/repositories/SettingsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class SettingsRepository
{
public const ENABLE_ENTRIES_FOR_TARGET_SITES = 'enable_entries_for_target_sites';
public const COPY_ENTRIES_SLUG_FROM_SOURCE_TO_TARGET = 'copy_entries_slug_from_source_to_target';

public const QUEUE_EACH_TRANSLATION_FILE_SEPARATELY = 'queue_each_translation_file_separately';
public const QUEUE_DISABLE_AUTOMATIC_SYNC = 'queue_disable_automatic_sync';
public const QUEUE_MANAGER_EXECUTED_AT = 'queue_manager_executed_at';

public const IGNORE_DROPDOWNS = 'ignore_dropdowns';
Expand Down Expand Up @@ -56,6 +56,25 @@ public function isQueueEachTranslationFileSeparately(): bool
return (bool)$settingValue->value;
}

public function get(string $name): ?string
{
$tableSchema = Craft::$app->getDb()->schema->getTableSchema(CraftliltpluginParameters::SETTINGS_TABLE_NAME);
if ($tableSchema === null) {
return null;
}


$settingValue = SettingRecord::findOne(
['name' => $name]
);

if (empty($settingValue) || empty($settingValue->value)) {
return null;
}

return $settingValue->value;
}

public function ignoreDropdowns(): bool
{
$tableSchema = Craft::$app->getDb()->schema->getTableSchema(CraftliltpluginParameters::SETTINGS_TABLE_NAME);
Expand Down
11 changes: 11 additions & 0 deletions src/templates/_components/utilities/configuration.twig
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@
}) }}
</div>

<div class="field">
{{ forms.checkbox({
label: 'Disable automatic synchronization of sent translation jobs',
name: 'queueDisableAutomaticSync',
id: 'queueDisableAutomaticSync',
checked: queueDisableAutomaticSync,
errors: model is defined? model.getErrors('queueDisableAutomaticSync') : [],
disabled: liltConfigDisabled
}) }}
</div>

{{ forms.textField({
name: 'liltConfigDisabled',
id: 'liltConfigDisabled',
Expand Down
6 changes: 6 additions & 0 deletions src/utilities/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ public static function contentHtml(): string
);
$queueEachTranslationFileSeparately = (bool) ($queueEachTranslationFileSeparately->value ?? false);

$queueDisableAutomaticSync = SettingRecord::findOne(
['name' => SettingsRepository::QUEUE_DISABLE_AUTOMATIC_SYNC,]
);
$queueDisableAutomaticSync = (bool) ($queueDisableAutomaticSync->value ?? false);

return Craft::$app->getView()->renderTemplate(
'craft-lilt-plugin/_components/utilities/configuration.twig',
[
Expand All @@ -106,6 +111,7 @@ public static function contentHtml(): string
'enableEntriesForTargetSites' => $enableEntriesForTargetSites,
'copyEntriesSlugFromSourceToTarget' => $copyEntriesSlugFromSourceToTarget,
'queueEachTranslationFileSeparately' => $queueEachTranslationFileSeparately,
'queueDisableAutomaticSync' => $queueDisableAutomaticSync,
]
);
}
Expand Down
Loading

0 comments on commit 43841ef

Please sign in to comment.