Skip to content

Commit

Permalink
Merge pull request #63 from lilt/4.x-development
Browse files Browse the repository at this point in the history
Add retry logic for background job
  • Loading branch information
hadomskyi authored Oct 12, 2022
2 parents acb856a + 9be5d2c commit 1088d2a
Show file tree
Hide file tree
Showing 16 changed files with 462 additions and 150 deletions.
87 changes: 6 additions & 81 deletions CHANGELOG.md
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -4,89 +4,14 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## 4.0.0 - 2022-10-04
### Added
- Support of CraftCMS v4.0.0 and higher (^4.0.0)

## 3.0.0 - 2022-10-04
### Added
- Support of CraftCMS v3.0.0 and higher (^3.0.0)
### Fixed
- Style issue on create job page

## 0.8.1 - 2022-10-02
### Fixed
- Status query for translation elements index
- Translation elements capability with different versions of CraftCMS
- Invalidate cache for translations on publish & review

## 0.8.0 - 2022-09-08
### Added
- Asynchronous job transfer to lilt platform

## 0.7.2 - 2022-09-08
### Added
- Spinner for not loaded element index on translation preview page

### Fixed
- Joins for translations search query

## 0.7.1 - 2022-09-08
### Fixed
- Column name mask for translation query

## 0.7.0 - 2022-09-08
### Added
- Added new button to publish changes without review

### Fixed
- Element index behaviour
- New job status color
- Custom sources for element index modal

## 0.6.0 - 2022-09-04
## 4.1.0 - 2022-10-11
### Added
- New translations element index on the preview page
- Retry logic for failed jobs

## 0.5.1 - 2022-08-27
### Fixed
- Translation preview page: modal not opening issue

## 0.5.0 - 2022-08-27
### Added
- Default sorting for translations

## 0.4.3 - 2022-08-17
### Fixed
- Removed value updating for BaseOption & Lightswitch fields

## 0.4.2 - 2022-08-17
### Fixed
- Translation applier wasn't able to find source element for site id
- Wrong language formatting when locale is empty

## 0.4.1 - 2022-08-16
### Added
- Error logging for all exception catches

## 0.4.0 - 2022-08-13
### Added
- Remove entry translation drafts on job removal
- Empty content now excluded from translation source body
- Warning for job entries, when translation in progress (on job creation)
- Fixed: Empty I18N entry creation
- Fixed: Translation preview issue (error while encoding of json)
- Fixed: Hide warning when element index not exist
- Fixed: Argument 2 passed to craft\services\Drafts::createDraft() must be of the type int, null given

## 0.3.1 - 2022-08-13
### Added
- Updated connector sdk dependency
- Job and translation status update after sync action
- Error message for manual retry

## 0.3.0 - 2022-08-03
### Added
- Fixed applying translations for nested fields

## 0.2.1 - 2022-08-03
## 4.0.0 - 2022-10-04
### Added
- Initial beta release
- Support of CraftCMS v4.0.0 and higher (^4.0.0)
2 changes: 1 addition & 1 deletion composer.json
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "lilt/craft-lilt-plugin",
"description": "The Lilt plugin makes it easy for you to send content to Lilt for translation right from within Craft CMS.",
"type": "craft-plugin",
"version": "4.0.0",
"version": "4.1.0",
"keywords": [
"craft",
"cms",
Expand Down
4 changes: 2 additions & 2 deletions src/assets/resources/job-try-again-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ CraftliltPlugin.JobTryAgainButton = Garnish.Base.extend({
location.reload();
}).catch(exception => {
Craft.cp.displayError(
Craft.t('app', 'Can\'t mark reviewed, unexpected issue occurred'));
Craft.t('app', 'Can\'t retry job, unexpected issue occurred'));
this.$spinner.remove();
$(this).removeClass('disabled');
this.$container.removeClass('disabled');
});
},
_getJobId: function() {
Expand Down
30 changes: 27 additions & 3 deletions src/controllers/job/PostSyncController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
use craft\helpers\Queue;
use craft\web\Controller;
use lilthq\craftliltplugin\Craftliltplugin;
use lilthq\craftliltplugin\elements\Job;
use lilthq\craftliltplugin\elements\Translation;
use lilthq\craftliltplugin\modules\FetchJobStatusFromConnector;
use lilthq\craftliltplugin\records\JobRecord;
use lilthq\craftliltplugin\records\TranslationRecord;
use yii\web\Response;

class PostSyncController extends Controller
Expand All @@ -32,11 +36,18 @@ public function actionInvoke(): Response
$jobIds
);

if (count($jobs) === 0) {
return $this->response->setStatusCode(404, json_encode(['msg' => 'Job(s) not found']));
}

$selectedJobIds = [];
foreach ($jobs as $job) {
if ($job->isCopySourceTextFlow()) {
continue;
}

$selectedJobIds[] = $job->id;

Queue::push(
(new FetchJobStatusFromConnector(
[
Expand All @@ -49,9 +60,22 @@ public function actionInvoke(): Response
);
}

if (count($jobs) === 0) {
return $this->response->setStatusCode(400, json_encode(['msg' => 'Job not found']));
}
TranslationRecord::updateAll(
[
'status' => TranslationRecord::STATUS_IN_PROGRESS
],
['jobId' => $selectedJobIds]
);

JobRecord::updateAll(
[
'status' => Job::STATUS_IN_PROGRESS
],
['id' => $selectedJobIds]
);

Craft::$app->elements->invalidateCachesForElementType(Translation::class);
Craft::$app->elements->invalidateCachesForElementType(Job::class);

return $this->response->setStatusCode(200);
}
Expand Down
8 changes: 4 additions & 4 deletions src/modules/FetchInstantJobTranslationsFromConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class FetchInstantJobTranslationsFromConnector extends BaseJob implements Retrya
{
public const DELAY_IN_SECONDS = 10;
public const PRIORITY = null;
public const TTR = 10 * 60;
public const TTR = 60 * 5; // 5 minutes

private const RETRY_COUNT = 0;
private const RETRY_COUNT = 3;

/**
* @var int $jobId
Expand Down Expand Up @@ -102,12 +102,12 @@ private function markAsDone($queue): void
);
}

public function getTtr()
public function getTtr(): int
{
return self::TTR;
}

public function canRetry($attempt, $error)
public function canRetry($attempt, $error): bool
{
return $attempt < self::RETRY_COUNT;
}
Expand Down
17 changes: 14 additions & 3 deletions src/modules/FetchJobStatusFromConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
use lilthq\craftliltplugin\Craftliltplugin;
use lilthq\craftliltplugin\elements\Job;
use lilthq\craftliltplugin\records\JobRecord;
use yii\queue\RetryableJobInterface;

class FetchJobStatusFromConnector extends BaseJob
class FetchJobStatusFromConnector extends BaseJob implements RetryableJobInterface
{
public const DELAY_IN_SECONDS = 10;
public const PRIORITY = null;
public const TTR = null;
public const TTR = 60 * 5; // 5 minutes

private const RETRY_COUNT = 0;
private const RETRY_COUNT = 3;

/**
* @var int $jobId
Expand Down Expand Up @@ -169,4 +170,14 @@ private function markAsDone($queue): void
)
);
}

public function getTtr(): int
{
return self::TTR;
}

public function canRetry($attempt, $error): bool
{
return $attempt < self::RETRY_COUNT;
}
}
19 changes: 15 additions & 4 deletions src/modules/FetchVerifiedJobTranslationsFromConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
use lilthq\craftliltplugin\elements\Translation;
use lilthq\craftliltplugin\records\JobRecord;
use lilthq\craftliltplugin\records\TranslationRecord;
use yii\queue\RetryableJobInterface;

class FetchVerifiedJobTranslationsFromConnector extends BaseJob
class FetchVerifiedJobTranslationsFromConnector extends BaseJob implements RetryableJobInterface
{
public const DELAY_IN_SECONDS = 5 * 60;
public const DELAY_IN_SECONDS = 60 * 5; // 5 minutes
public const PRIORITY = null;
public const TTR = null;
public const TTR = 60 * 5; // 5 minutes

private const RETRY_COUNT = 0;
private const RETRY_COUNT = 3;

/**
* @var int $jobId
Expand Down Expand Up @@ -209,4 +210,14 @@ private function markAsDone($queue): void
)
);
}

public function getTtr(): int
{
return self::TTR;
}

public function canRetry($attempt, $error): bool
{
return $attempt < self::RETRY_COUNT;
}
}
37 changes: 20 additions & 17 deletions src/modules/SendJobToConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@
use Craft;
use craft\errors\InvalidFieldException;
use craft\queue\BaseJob;
use Exception;
use LiltConnectorSDK\ApiException;
use lilthq\craftliltplugin\Craftliltplugin;
use lilthq\craftliltplugin\elements\Job;
use lilthq\craftliltplugin\records\JobRecord;
use Throwable;
use yii\queue\RetryableJobInterface;

class SendJobToConnector extends BaseJob
class SendJobToConnector extends BaseJob implements RetryableJobInterface
{
public const DELAY_IN_SECONDS = 10;
public const PRIORITY = null;
public const TTR = null;
public const TTR = 60 * 5; // 5 minutes

private const RETRY_COUNT = 0;
private const RETRY_COUNT = 3;

/**
* @var int $jobId
Expand Down Expand Up @@ -67,19 +67,12 @@ public function execute($queue): void
return;
}

try {
if ($job->isVerifiedFlow() || $job->isInstantFlow()) {
Craftliltplugin::getInstance()->sendJobToLiltConnectorHandler->__invoke($job);
}

if ($job->isCopySourceTextFlow()) {
Craftliltplugin::getInstance()->copySourceTextHandler->__invoke($job);
}
} catch (Exception $ex) {
$jobRecord->status = Job::STATUS_FAILED;
$jobRecord->save();
if ($job->isVerifiedFlow() || $job->isInstantFlow()) {
Craftliltplugin::getInstance()->sendJobToLiltConnectorHandler->__invoke($job);
}

Craft::$app->elements->invalidateCachesForElement($job);
if ($job->isCopySourceTextFlow()) {
Craftliltplugin::getInstance()->copySourceTextHandler->__invoke($job);
}

$this->markAsDone($queue);
Expand All @@ -91,7 +84,7 @@ public function execute($queue): void
*/
protected function defaultDescription(): ?string
{
return Craft::t('app', 'Lilt translations');
return Craft::t('app', 'Sending jobs to lilt');
}

/**
Expand All @@ -112,4 +105,14 @@ private function markAsDone($queue): void
)
);
}

public function getTtr(): int
{
return self::TTR;
}

public function canRetry($attempt, $error): bool
{
return $attempt < self::RETRY_COUNT;
}
}
2 changes: 2 additions & 0 deletions src/parameters/CraftliltpluginParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Craft;
use LiltConnectorSDK\Model\SettingsResponse;
use lilthq\craftliltplugin\services\listeners\AfterDraftAppliedListener;
use lilthq\craftliltplugin\services\listeners\AfterErrorListener;
use lilthq\craftliltplugin\services\listeners\RegisterDefaultTableAttributesListener;
use lilthq\craftliltplugin\services\listeners\RegisterElementTypesListener;
use lilthq\craftliltplugin\services\listeners\RegisterTableAttributesListener;
Expand Down Expand Up @@ -62,6 +63,7 @@ class CraftliltpluginParameters
RegisterElementTypesListener::class,
RegisterDefaultTableAttributesListener::class,
RegisterTableAttributesListener::class,
AfterErrorListener::class,
];

public const TRANSLATION_WORKFLOW_INSTANT = SettingsResponse::LILT_TRANSLATION_WORKFLOW_INSTANT;
Expand Down
Loading

0 comments on commit 1088d2a

Please sign in to comment.