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
  • Loading branch information
hadomskyi authored Nov 15, 2023
2 parents f14569c + 6d454f4 commit 2449f97
Show file tree
Hide file tree
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
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
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.4.1",
"version": "4.4.2",
"keywords": [
"craft",
"cms",
Expand Down
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
Expand Up @@ -101,6 +101,7 @@ public function execute($queue): void
"jobRecord" => $jobRecord,
]);

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

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

return;
Expand Down Expand Up @@ -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;
Expand All @@ -186,6 +189,7 @@ function (TranslationResponse $connectorTranslation) {
self::TTR
);

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

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

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

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

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

Expand Down
14 changes: 14 additions & 0 deletions src/parameters/CraftliltpluginParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 [
Expand All @@ -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
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
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
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}
}
Loading

0 comments on commit 2449f97

Please sign in to comment.