Skip to content

Commit

Permalink
Merge pull request #135 from lilt/4.x-fixes
Browse files Browse the repository at this point in the history
Add cache for get job and get translations requests
hadomskyi authored Nov 15, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents f14569c + 6d454f4 commit 2449f97
Showing 11 changed files with 283 additions and 6 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -4,6 +4,13 @@ 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.4.2 - 2023-11-15
### Fixed
- Lock release issue after queue message processing

### Added
- Support for the [Typed Link Field plugin](https://plugins.craftcms.com/typedlinkfield)

## 4.4.1 - 2023-11-08
### Added
- Ignore dropdowns setting
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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.4.1",
"version": "4.4.2",
"keywords": [
"craft",
"cms",
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace craft\contentmigrations;

use Craft;
use craft\db\Migration;
use craft\fields\Matrix;
use lilthq\craftliltplugin\parameters\CraftliltpluginParameters;

/**
* m230304_162344_set_fields_translatable migration.
*/
class m231004_192344_set_fields_propagation extends Migration
{
/**
* @inheritdoc
*/
public function safeUp()
{
$ignoreDropdownsRecord = new \lilthq\craftliltplugin\records\SettingRecord(['name' => 'ignore_dropdowns']);
$ignoreDropdownsRecord->value = 1;
$ignoreDropdownsRecord->save();
}

/**
* @inheritdoc
*/
public function safeDown()
{
echo "m230304_162344_set_fields_translatable can't be reverted.\n";

return true;
}
}
6 changes: 6 additions & 0 deletions src/modules/FetchJobStatusFromConnector.php
Original file line number Diff line number Diff line change
@@ -101,6 +101,7 @@ public function execute($queue): void
"jobRecord" => $jobRecord,
]);

$mutex->release($mutexKey);
$this->markAsDone($queue);
return;
}
@@ -118,6 +119,7 @@ public function execute($queue): void
self::TTR
);

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

return;
@@ -169,6 +171,7 @@ function (TranslationResponse $connectorTranslation) {
Craft::$app->elements->invalidateCachesForElementType(TranslationRecord::class);
Craft::$app->elements->invalidateCachesForElementType(Job::class);

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

return;
@@ -186,6 +189,7 @@ function (TranslationResponse $connectorTranslation) {
self::TTR
);

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

return;
@@ -240,6 +244,7 @@ function (TranslationResponse $connectorTranslation) {
"jobRecord" => $jobRecord,
]);

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

return;
@@ -273,6 +278,7 @@ function (TranslationResponse $connectorTranslation) {
Job::class
);

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

14 changes: 14 additions & 0 deletions src/parameters/CraftliltpluginParameters.php
Original file line number Diff line number Diff line change
@@ -61,6 +61,8 @@ class CraftliltpluginParameters
public const LINKIT_FIELD = 'fruitstudios\linkit\fields\LinkitField';
public const COLOUR_SWATCHES_FIELD = 'percipioglobal\colourswatches\fields\ColourSwatches';

public const LENZ_LINKFIELD = 'lenz\linkfield\fields\LinkField';

public const LISTENERS = [
AfterDraftAppliedListener::class,
RegisterCpUrlRulesListener::class,
@@ -76,6 +78,7 @@ class CraftliltpluginParameters
public const TRANSLATION_WORKFLOW_VERIFIED = SettingsResponse::LILT_TRANSLATION_WORKFLOW_VERIFIED;
public const TRANSLATION_WORKFLOW_COPY_SOURCE_TEXT = 'COPY_SOURCE_TEXT';

public const DEFAULT_CACHE_RESPONSE_IN_SECONDS = 60;
public static function getTranslationWorkflows(): array
{
return [
@@ -96,4 +99,15 @@ public static function getTranslationWorkflows(): array
)
];
}

public static function getResponseCache(): int
{
$envCache = getenv('CRAFT_LILT_PLUGIN_CACHE_RESPONSE_IN_SECONDS');
$cache =
!empty($envCache) || $envCache === '0' ?
(int)$envCache :
CraftliltpluginParameters::DEFAULT_CACHE_RESPONSE_IN_SECONDS;

return $cache;
}
}
13 changes: 11 additions & 2 deletions src/services/ServiceInitializer.php
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@
use lilthq\craftliltplugin\services\appliers\field\ColourSwatchesContentApplier;
use lilthq\craftliltplugin\services\appliers\field\ElementQueryContentApplier;
use lilthq\craftliltplugin\services\appliers\field\FieldContentApplier;
use lilthq\craftliltplugin\services\appliers\field\LenzLinkFieldContentApplier;
use lilthq\craftliltplugin\services\appliers\field\LightswitchContentApplier;
use lilthq\craftliltplugin\services\appliers\field\LinkitContentApplier;
use lilthq\craftliltplugin\services\appliers\field\PlainTextContentApplier;
@@ -50,6 +51,7 @@
use lilthq\craftliltplugin\services\providers\field\ColourSwatchesContentProvider;
use lilthq\craftliltplugin\services\providers\field\ElementQueryContentProvider;
use lilthq\craftliltplugin\services\providers\field\FieldContentProvider;
use lilthq\craftliltplugin\services\providers\field\LenzLinkFieldContentProvider;
use lilthq\craftliltplugin\services\providers\field\LightswitchContentProvider;
use lilthq\craftliltplugin\services\providers\field\LinkitContentProvider;
use lilthq\craftliltplugin\services\providers\field\PlainTextContentProvider;
@@ -162,7 +164,6 @@ function () {
CraftliltpluginParameters::CRAFT_FIELDS_TABLE => new TableContentProvider(),
CraftliltpluginParameters::CRAFT_FIELDS_LIGHTSWITCH => new LightswitchContentProvider(),

CraftliltpluginParameters::LINKIT_FIELD => new LinkitContentProvider(),
CraftliltpluginParameters::COLOUR_SWATCHES_FIELD => new ColourSwatchesContentProvider(),

# Options
@@ -171,6 +172,11 @@ function () {
CraftliltpluginParameters::CRAFT_FIELDS_MULTISELECT => new BaseOptionFieldContentProvider(),
CraftliltpluginParameters::CRAFT_FIELDS_CHECKBOXES => new BaseOptionFieldContentProvider(),

# Links
CraftliltpluginParameters::LINKIT_FIELD => new LinkitContentProvider(),
CraftliltpluginParameters::LENZ_LINKFIELD => new LenzLinkFieldContentProvider(),


### ELEMENT QUERY PROVIDERS

#Matrix
@@ -220,7 +226,6 @@ function () {
CraftliltpluginParameters::CRAFT_FIELDS_TABLE => new TableContentApplier(),
CraftliltpluginParameters::CRAFT_FIELDS_LIGHTSWITCH => new LightswitchContentApplier(),

CraftliltpluginParameters::LINKIT_FIELD => new LinkitContentApplier(),
CraftliltpluginParameters::COLOUR_SWATCHES_FIELD => new ColourSwatchesContentApplier(),

### Options
@@ -229,6 +234,10 @@ function () {
CraftliltpluginParameters::CRAFT_FIELDS_MULTISELECT => new BaseOptionFieldContentApplier(),
CraftliltpluginParameters::CRAFT_FIELDS_CHECKBOXES => new BaseOptionFieldContentApplier(),

### Links
CraftliltpluginParameters::LINKIT_FIELD => new LinkitContentApplier(),
CraftliltpluginParameters::LENZ_LINKFIELD => new LenzLinkFieldContentApplier(),

### ELEMENT QUERY APPLIERS

# Matrix
47 changes: 47 additions & 0 deletions src/services/appliers/field/LenzLinkFieldContentApplier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace lilthq\craftliltplugin\services\appliers\field;

use craft\errors\InvalidFieldException;
use fruitstudios\linkit\fields\LinkitField;
use lenz\linkfield\fields\LinkField;
use lenz\linkfield\models\input\InputLink;
use lilthq\craftliltplugin\parameters\CraftliltpluginParameters;

class LenzLinkFieldContentApplier extends AbstractContentApplier implements ApplierInterface
{
/**
* @throws InvalidFieldException
*/
public function apply(ApplyContentCommand $command): ApplyContentResult
{
/** @var LinkField $field */
$field = $command->getField();
$fieldKey = $this->getFieldKey($command->getField());
$content = $command->getContent();

if (!isset($content[$fieldKey])) {
return ApplyContentResult::fail();
}

/**
* @var InputLink $fieldValue
*/
$fieldValue = $command->getElement()->getFieldValue(
$field->handle
);
$fieldValue->customText = $content[$fieldKey];

$command->getElement()->setFieldValue($field->handle, $fieldValue);

return ApplyContentResult::applied();
}

public function support(ApplyContentCommand $command): bool
{
return get_class($command->getField()) === CraftliltpluginParameters::LENZ_LINKFIELD
&& $command->getField()->getIsTranslatable($command->getElement());
}
}
39 changes: 39 additions & 0 deletions src/services/providers/field/LenzLinkFieldContentProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace lilthq\craftliltplugin\services\providers\field;

use craft\errors\InvalidFieldException;
use fruitstudios\linkit\fields\LinkitField;
use lenz\linkfield\fields\LinkField;
use lenz\linkfield\models\input\InputLink;
use lilthq\craftliltplugin\parameters\CraftliltpluginParameters;
use lilthq\craftliltplugin\services\providers\command\ProvideContentCommand;

class LenzLinkFieldContentProvider extends AbstractContentProvider
{
/**
* @throws InvalidFieldException
*/
public function provide(ProvideContentCommand $provideContentCommand): ?string
{
/** @var LinkField $field */
$field = $provideContentCommand->getField();

/**
* @var InputLink $fieldValue
*/
$fieldValue = $provideContentCommand->getElement()->getFieldValue(
$field->handle
);

return $fieldValue->getCustomText();
}

public function support(ProvideContentCommand $command): bool
{
return get_class($command->getField()) === CraftliltpluginParameters::LENZ_LINKFIELD
&& $command->getField()->getIsTranslatable($command->getElement());
}
}
60 changes: 59 additions & 1 deletion src/services/repositories/external/ConnectorJobRepository.php
Original file line number Diff line number Diff line change
@@ -9,6 +9,8 @@
use LiltConnectorSDK\ApiException;
use LiltConnectorSDK\Model\JobResponse;
use LiltConnectorSDK\Model\SettingsResponse;
use LiltConnectorSDK\ObjectSerializer;
use lilthq\craftliltplugin\parameters\CraftliltpluginParameters;

class ConnectorJobRepository extends AbstractConnectorExternalRepository
{
@@ -68,6 +70,62 @@ public function start(int $liltJobId): bool
*/
public function findOneById(int $liltJobId): JobResponse
{
return $this->apiInstance->servicesApiJobsGetJobById($liltJobId);
$cacheKey = __METHOD__ . ':' . $liltJobId;
$cache = CraftliltpluginParameters::getResponseCache();

if (!empty($cache)) {
$response = $this->getResponseFromCache($cacheKey, $liltJobId);

if (!empty($response)) {
return $response;
}
}

$response = $this->apiInstance->servicesApiJobsGetJobById($liltJobId);

$data = $response->__toString();

if (!empty($cache)) {
Craft::$app->cache->add(
$cacheKey,
$data,
$cache
);
}

return $response;
}

/**
* @param string $cacheKey
* @param int $liltJobId
* @return JobResponse
*/
private function getResponseFromCache(string $cacheKey, int $liltJobId): ?JobResponse
{
$response = null;

try {
$dataFromCache = Craft::$app->cache->get($cacheKey);

if ($dataFromCache) {
/**
* @var JobResponse $response
*/
$response = ObjectSerializer::deserialize($dataFromCache, JobResponse::class);
}
} catch (Exception $ex) {
Craft::error([
"message" => sprintf(
'Deserialize error for lilt job %d: %s ',
$liltJobId,
$ex->getMessage()
),
"FILE" => __FILE__,
"LINE" => __LINE__,
]);
}

return $response;
}
}
Original file line number Diff line number Diff line change
@@ -4,10 +4,14 @@

namespace lilthq\craftliltplugin\services\repositories\external;

use Craft;
use Exception;
use LiltConnectorSDK\ApiException;
use LiltConnectorSDK\Model\JobResponse1 as ConnectorTranslationsResponse;
use LiltConnectorSDK\Model\TranslationResponse;
use LiltConnectorSDK\ObjectSerializer;
use lilthq\craftliltplugin\exceptions\WrongTranslationFilenameException;
use lilthq\craftliltplugin\parameters\CraftliltpluginParameters;

class ConnectorTranslationRepository extends AbstractConnectorExternalRepository
{
@@ -16,11 +20,34 @@ class ConnectorTranslationRepository extends AbstractConnectorExternalRepository
*/
public function findByJobId(int $jobId): ConnectorTranslationsResponse
{
return $this->apiInstance->servicesApiDeliveriesGetDeliveriesByJobId(
$cacheKey = __METHOD__ . ':' . $jobId;
$cache = CraftliltpluginParameters::getResponseCache();

if (!empty($cache)) {
$response = $this->getResponseFromCache($cacheKey, $jobId);

if (!empty($response)) {
return $response;
}
}

$response = $this->apiInstance->servicesApiDeliveriesGetDeliveriesByJobId(
100,
"00",
$jobId
);

$data = $response->__toString();

if (!empty($cache)) {
Craft::$app->cache->add(
$cacheKey,
$data,
$cache
);
}

return $response;
}

/**
@@ -54,4 +81,37 @@ public function getElementIdFromTranslationResponse(TranslationResponse $transla

return (int) $matches[1];
}

/**
* @param string $cacheKey
* @param int $jobId
* @return ConnectorTranslationsResponse|null
*/
private function getResponseFromCache(string $cacheKey, int $jobId): ?ConnectorTranslationsResponse
{
$response = null;

try {
$dataFromCache = Craft::$app->cache->get($cacheKey);

if ($dataFromCache) {
/**
* @var ConnectorTranslationsResponse $response
*/
$response = ObjectSerializer::deserialize($dataFromCache, ConnectorTranslationsResponse::class);
}
} catch (Exception $ex) {
Craft::error([
"message" => sprintf(
'Deserialize error for lilt job %d: %s ',
$jobId,
$ex->getMessage()
),
"FILE" => __FILE__,
"LINE" => __LINE__,
]);
}

return $response;
}
}
3 changes: 2 additions & 1 deletion tests/.env.test
Original file line number Diff line number Diff line change
@@ -11,4 +11,5 @@ DB_PORT="3306"
SECURITY_KEY="abcde12345"

CRAFT_LILT_PLUGIN_CONNECTOR_API_KEY="SECURE_API_KEY_FOR_LILT_CONNECTOR"
CRAFT_LILT_PLUGIN_CONNECTOR_API_URL="http://wiremock/api/v1.0"
CRAFT_LILT_PLUGIN_CONNECTOR_API_URL="http://wiremock/api/v1.0"
CRAFT_LILT_PLUGIN_CACHE_RESPONSE_IN_SECONDS=0

0 comments on commit 2449f97

Please sign in to comment.