Skip to content

Commit 13553a1

Browse files
authored
Merge pull request #80 from lilt/3.x-fix-matrix
Fix copy source text flow for matrix
2 parents e60f981 + a9a1f46 commit 13553a1

23 files changed

+307
-742
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
44

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

7+
## 3.4.1 - 2023-02-13
8+
### Changed
9+
- Queues priority decreased for sending jobs to 1024 and receiving to 2048
10+
- Deprecate FetchInstantJobTranslationsFromConnector and FetchVerifiedJobTranslationsFromConnector
11+
12+
### Fixed
13+
- Copy source text for Matrix fields
14+
715
## 3.4.0 - 2023-02-06
816
### Added
917
- Entry slug name added to translation filenames

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "lilt/craft-lilt-plugin",
33
"description": "The Lilt plugin makes it easy for you to send content to Lilt for translation right from within Craft CMS.",
44
"type": "craft-plugin",
5-
"version": "3.4.0",
5+
"version": "3.4.1",
66
"keywords": [
77
"craft",
88
"cms",

e2e/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ up: clone down
2727
docker-compose exec -T app sh -c 'echo DB_PASSWORD=craft-lilt >> .env'
2828
docker-compose exec -T app sh -c 'echo DB_SCHEMA=public >> .env'
2929
docker-compose exec -T app sh -c 'echo DB_TABLE_PREFIX= >> .env'
30+
docker-compose exec -T app sh -c 'echo CRAFT_LILT_PLUGIN_QUEUE_DELAY_IN_SECONDS=5 >> .env'
3031
docker-compose exec -T app sh -c 'php craft db/restore happylager.sql'
3132
docker-compose exec -T app sh -c 'php craft up'
3233
docker-compose exec -T app sh -c 'php craft migrate/up'

e2e/cypress/support/commands.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ Cypress.Commands.add('waitForJobStatus',
321321
* @returns undefined
322322
*/
323323
Cypress.Commands.add('waitForTranslationDrafts',
324-
(maxAttempts = 30, attempts = 0, waitPerIteration = 3000) => {
324+
(maxAttempts = 100, attempts = 0, waitPerIteration = 1000) => {
325325
if (attempts > maxAttempts) {
326326
throw new Error('Timed out waiting for report to be generated');
327327
}
@@ -362,7 +362,7 @@ Cypress.Commands.add('publishTranslation', (jobTitle, language) => {
362362
cy.get('#translations-publish-action').click();
363363
});
364364

365-
cy.wait(5000); //delay for publishing
365+
cy.wait(10000); //delay for publishing
366366
cy.waitForJobStatus('complete');
367367
});
368368

@@ -391,7 +391,7 @@ Cypress.Commands.add('publishTranslations', (jobTitle, languages) => {
391391

392392
cy.get('#translations-publish-action').click();
393393

394-
cy.wait(5000); //delay for publishing
394+
cy.wait(10000); //delay for publishing
395395
cy.waitForJobStatus('complete');
396396
});
397397

src/controllers/job/GetSendToLiltController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function actionInvoke(): Response
8383
Queue::push(
8484
new SendJobToConnector(['jobId' => $jobId]),
8585
SendJobToConnector::PRIORITY,
86-
SendJobToConnector::DELAY_IN_SECONDS
86+
10 //10 seconds for first job
8787
);
8888

8989
$jobRecord->status = Job::STATUS_IN_PROGRESS;

src/controllers/job/PostSyncController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function actionInvoke(): Response
6464
]
6565
)),
6666
FetchJobStatusFromConnector::PRIORITY,
67-
FetchJobStatusFromConnector::DELAY_IN_SECONDS
67+
10 //10 seconds for first job
6868
);
6969
}
7070

src/exeptions/DraftNotFoundException.php renamed to src/exceptions/DraftNotFoundException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
declare(strict_types=1);
99

10-
namespace lilthq\craftliltplugin\exeptions;
10+
namespace lilthq\craftliltplugin\exceptions;
1111

1212
use RuntimeException;
1313

src/exeptions/JobNotFoundException.php renamed to src/exceptions/JobNotFoundException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
declare(strict_types=1);
99

10-
namespace lilthq\craftliltplugin\exeptions;
10+
namespace lilthq\craftliltplugin\exceptions;
1111

1212
use RuntimeException;
1313

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/**
4+
* @link https://github.com/lilt
5+
* @copyright Copyright (c) 2022 Lilt Devs
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace lilthq\craftliltplugin\exceptions;
11+
12+
use RuntimeException;
13+
14+
class WrongTranslationFilenameException extends RuntimeException
15+
{
16+
}

src/modules/FetchJobStatusFromConnector.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020

2121
class FetchJobStatusFromConnector extends BaseJob implements RetryableJobInterface
2222
{
23-
public const DELAY_IN_SECONDS = 10;
24-
public const PRIORITY = 512;
23+
public const DELAY_IN_SECONDS = 5 * 60;
24+
public const PRIORITY = 1024;
2525
public const TTR = 60 * 30;
2626

2727
private const RETRY_COUNT = 3;
@@ -50,11 +50,12 @@ public function execute($queue): void
5050
}
5151

5252
if (empty($this->liltJobId)) {
53-
//looks like job is
53+
//looks like job is not sent
5454
Queue::push(
5555
new SendJobToConnector(['jobId' => $this->jobId]),
5656
SendJobToConnector::PRIORITY,
57-
SendJobToConnector::DELAY_IN_SECONDS
57+
SendJobToConnector::getDelay(),
58+
SendJobToConnector::TTR
5859
);
5960

6061
$this->markAsDone($queue);
@@ -92,7 +93,8 @@ public function execute($queue): void
9293
]
9394
)),
9495
self::PRIORITY,
95-
self::DELAY_IN_SECONDS
96+
self::getDelay(),
97+
self::TTR
9698
);
9799

98100
return;
@@ -119,7 +121,8 @@ public function execute($queue): void
119121
]
120122
),
121123
FetchTranslationFromConnector::PRIORITY,
122-
FetchTranslationFromConnector::DELAY_IN_SECONDS
124+
10, //10 seconds for first job
125+
FetchTranslationFromConnector::TTR
123126
);
124127
}
125128
}
@@ -149,25 +152,27 @@ public function execute($queue): void
149152
]
150153
),
151154
FetchTranslationFromConnector::PRIORITY,
152-
FetchTranslationFromConnector::DELAY_IN_SECONDS
155+
10, //10 seconds for first job
156+
FetchTranslationFromConnector::TTR
153157
);
154158
}
155159
}
156160

157161
$jobRecord->save();
158-
$this->markAsDone($queue);
159162

160163
Craft::$app->elements->invalidateCachesForElementType(
161164
Job::class
162165
);
166+
167+
$this->markAsDone($queue);
163168
}
164169

165170
/**
166171
* @inheritdoc
167172
*/
168173
protected function defaultDescription(): ?string
169174
{
170-
return Craft::t('app', 'Updating lilt job');
175+
return Craft::t('app', 'Fetching lilt job status');
171176
}
172177

173178
/**
@@ -199,4 +204,14 @@ public function canRetry($attempt, $error): bool
199204
{
200205
return $attempt < self::RETRY_COUNT;
201206
}
207+
208+
public static function getDelay(): int
209+
{
210+
$envDelay = getenv('CRAFT_LILT_PLUGIN_QUEUE_DELAY_IN_SECONDS');
211+
if (!empty($envDelay) || $envDelay === '0') {
212+
return (int) $envDelay;
213+
}
214+
215+
return self::DELAY_IN_SECONDS;
216+
}
202217
}

src/modules/FetchTranslationFromConnector.php

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@
1818
use LiltConnectorSDK\Model\TranslationResponse;
1919
use lilthq\craftliltplugin\Craftliltplugin;
2020
use lilthq\craftliltplugin\elements\Job;
21+
use lilthq\craftliltplugin\parameters\CraftliltpluginParameters;
2122
use lilthq\craftliltplugin\records\TranslationRecord;
2223
use Throwable;
2324
use yii\queue\RetryableJobInterface;
2425

2526
class FetchTranslationFromConnector extends BaseJob implements RetryableJobInterface
2627
{
27-
public const DELAY_IN_SECONDS = 10;
2828
public const DELAY_IN_SECONDS_INSTANT = 10;
2929
public const DELAY_IN_SECONDS_VERIFIED = 60 * 5;
30-
public const PRIORITY = 1024;
30+
public const PRIORITY = 2048;
3131
public const TTR = 60 * 30;
3232

3333
private const RETRY_COUNT = 3;
@@ -84,6 +84,7 @@ public function execute($queue): void
8484
if (empty($translationRecord->connectorTranslationId)) {
8585
Craftliltplugin::getInstance()->updateTranslationsConnectorIds->update($job);
8686
}
87+
$translationRecord->refresh();
8788

8889
$translationFromConnector = Craftliltplugin::getInstance()->connectorTranslationRepository->findById(
8990
$translationRecord->connectorTranslationId
@@ -103,8 +104,8 @@ public function execute($queue): void
103104
]
104105
);
105106

106-
$this->markAsDone($queue);
107107
$mutex->release($mutexKey);
108+
$this->markAsDone($queue);
108109
return;
109110
}
110111

@@ -117,15 +118,12 @@ public function execute($queue): void
117118
'translationId' => $this->translationId,
118119
]
119120
),
120-
FetchTranslationFromConnector::PRIORITY,
121-
($job->isInstantFlow() ?
122-
FetchTranslationFromConnector::DELAY_IN_SECONDS_INSTANT :
123-
FetchTranslationFromConnector::DELAY_IN_SECONDS_VERIFIED
124-
)
121+
self::PRIORITY,
122+
self::getDelay($job->translationWorkflow)
125123
);
126124

127-
$this->markAsDone($queue);
128125
$mutex->release($mutexKey);
126+
$this->markAsDone($queue);
129127

130128
return;
131129
}
@@ -153,15 +151,15 @@ public function execute($queue): void
153151
]
154152
);
155153

156-
$this->markAsDone($queue);
157154
$mutex->release($mutexKey);
155+
$this->markAsDone($queue);
158156
return;
159157
}
160158

161159
Craftliltplugin::getInstance()->updateJobStatusHandler->update($job->id);
162160

163-
$this->markAsDone($queue);
164161
$mutex->release($mutexKey);
162+
$this->markAsDone($queue);
165163
}
166164

167165
/**
@@ -234,4 +232,16 @@ private function isTranslationFinished($job, TranslationResponse $translationFro
234232
|| ($job->isVerifiedFlow() && $translationFromConnector->getStatus(
235233
) === TranslationResponse::STATUS_EXPORT_COMPLETE);
236234
}
235+
236+
public static function getDelay(string $flow): int
237+
{
238+
$envDelay = getenv('CRAFT_LILT_PLUGIN_QUEUE_DELAY_IN_SECONDS');
239+
if (!empty($envDelay) || $envDelay === '0') {
240+
return (int) $envDelay;
241+
}
242+
243+
return strtolower($flow) === strtolower(CraftliltpluginParameters::TRANSLATION_WORKFLOW_INSTANT) ?
244+
self::DELAY_IN_SECONDS_INSTANT :
245+
self::DELAY_IN_SECONDS_VERIFIED;
246+
}
237247
}

src/modules/SendJobToConnector.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121

2222
class SendJobToConnector extends BaseJob implements RetryableJobInterface
2323
{
24-
public const DELAY_IN_SECONDS = 10;
25-
public const PRIORITY = 256;
24+
public const DELAY_IN_SECONDS = 60;
25+
public const PRIORITY = 1024;
2626
public const TTR = 60 * 30;
2727

2828
private const RETRY_COUNT = 3;
@@ -115,4 +115,14 @@ public function canRetry($attempt, $error): bool
115115
{
116116
return $attempt < self::RETRY_COUNT;
117117
}
118+
119+
public static function getDelay(): int
120+
{
121+
$envDelay = getenv('CRAFT_LILT_PLUGIN_QUEUE_DELAY_IN_SECONDS');
122+
if (!empty($envDelay) || $envDelay === '0') {
123+
return (int) $envDelay;
124+
}
125+
126+
return self::DELAY_IN_SECONDS;
127+
}
118128
}

src/services/appliers/ElementTranslatableContentApplier.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use craft\services\Drafts as DraftRepository;
1616
use lilthq\craftliltplugin\Craftliltplugin;
1717
use lilthq\craftliltplugin\datetime\DateTime;
18-
use lilthq\craftliltplugin\exeptions\DraftNotFoundException;
18+
use lilthq\craftliltplugin\exceptions\DraftNotFoundException;
1919
use lilthq\craftliltplugin\records\I18NRecord;
2020
use lilthq\craftliltplugin\services\appliers\field\ApplyContentCommand;
2121
use lilthq\craftliltplugin\services\appliers\field\FieldContentApplier;

src/services/handlers/CreateDraftHandler.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,12 @@ public function create(
4848

4949
$creatorId = Craft::$app->user->getId();
5050
if ($creatorId === null) {
51-
//TODO: it is not expected to reach, but it is possible. Investigation herer, why user id is null?
52-
Craft::error(
53-
"Can't get user from current session with Craft::\$app->user->getId(),"
54-
. "please check you app configuration!"
55-
);
5651
$creatorId = $createFrom->authorId;
5752
}
5853

5954
$draft = Craft::$app->drafts->createDraft(
6055
$createFrom,
61-
$creatorId ?? 0, //TODO: not best but one of the ways. Need to check why user can have nullable id?
56+
$creatorId ?? 0,
6257
sprintf(
6358
'%s [%s -> %s] ' . (new DateTime())->format('H:i:s'),
6459
$jobTitle,
@@ -82,14 +77,27 @@ public function create(
8277
$fields = $fieldLayout ? $fieldLayout->getFields() : [];
8378

8479
foreach ($fields as $field) {
80+
if (get_class($field) === CraftliltpluginParameters::CRAFT_FIELDS_MATRIX) {
81+
$draft->setFieldValue($field->handle, $draft->getFieldValue($field->handle));
82+
83+
Craft::$app->matrix->duplicateBlocks($field, $createFrom, $draft, false, false);
84+
Craft::$app->matrix->saveField($field, $draft);
85+
86+
continue;
87+
}
88+
8589
$field->copyValue($element, $draft);
8690
}
8791

8892
$draft->title = $element->title;
8993

94+
$draft->setCanonicalId(
95+
$createFrom->id
96+
);
97+
9098
$draft->duplicateOf = $element;
9199
$draft->mergingCanonicalChanges = true;
92-
$draft->afterPropagate(false);
100+
$draft->afterPropagate(true);
93101

94102
$copyEntriesSlugFromSourceToTarget = SettingRecord::findOne(
95103
['name' => 'copy_entries_slug_from_source_to_target']
@@ -143,6 +151,8 @@ private function markFieldsAsChanged(ElementInterface $element): void
143151
$this->markFieldsAsChanged($blockElement);
144152
}
145153

154+
$this->upsertChangedFields($element, $field);
155+
146156
continue;
147157
}
148158

src/services/handlers/EditJobHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use Craft;
1313
use lilthq\craftliltplugin\Craftliltplugin;
1414
use lilthq\craftliltplugin\elements\Job;
15-
use lilthq\craftliltplugin\exeptions\JobNotFoundException;
15+
use lilthq\craftliltplugin\exceptions\JobNotFoundException;
1616
use lilthq\craftliltplugin\records\JobRecord;
1717
use lilthq\craftliltplugin\services\handlers\commands\EditJobCommand;
1818
use lilthq\craftliltplugin\services\repositories\JobRepository;

0 commit comments

Comments
 (0)