Skip to content

Commit

Permalink
fix(live-sync): Welcome back LiveSync (#357)
Browse files Browse the repository at this point in the history
Co-authored-by: John.R <[email protected]>
  • Loading branch information
Venatum and fox-john authored Sep 30, 2024
1 parent 7d3b595 commit 2f16ef0
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 50 deletions.
12 changes: 6 additions & 6 deletions e2e-env/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
## Introduction

Enabling the startup of a complete stack to perform e2e tests on `ps_eventbus`.
stack consists of the following elements:
Stack consists of the following elements:

- A storefront under Flashlight (with a mock of `ps_account` and a local link to `ps_eventbus`);
- A MySQL database;
- PHPMyAdmin;
- A mock of the CloudSync APIs.

For the CloudSync APIs mock, it is a NodeJS application simulating the CloudSync APIs. Requests made by `ps_eventbus` to CloudSync are redirected to the mock using a reverse proxy (nginx).
When a request reaches the mock, it utilizes WebSockets to transmit the said request from `ps_eventbus` to the E2E tests, allowing validation of the information coming out of `ps_eventbus`.
For the CloudSync APIs mock, it is a Node.js application simulating the CloudSync APIs. Requests made by `ps_eventbus` to CloudSync are redirected to the mock using a reverse proxy (nginx).
When a request reaches the mock, it uses WebSockets to transmit the said request from `ps_eventbus` to the E2E tests, allowing validation of the information coming out of `ps_eventbus`.

## Troubleshooting

The end-to-end environment relies on PrestaShop Flashlight, which by default runs scripts and php with the user `www-data`. This user is mapped on the uid/gid _1000_, which might perfectly match your own Linux system user.

However, if for any reason your uid is not 1000, you might disable code mentioned with this comment : `# Notice: you might enable this if your uid is not 1000, or encounter permission issues`. One thing you will loose, is the ability to hot-reload local vendors on your host to the guest docker container. Docker compose down/up will be your friend in that very case.

If you are a MacOS user, mounting a docker won't cause any issue, as the process isolation is greater (and slower).
If you are a macOS user, mounting a docker won't cause any issue, as the process isolation is greater (and slower).

## Start the environment

Expand All @@ -41,7 +41,7 @@ Or in detached mode:
docker compose up -d
```

Or specifically only starting PrestaShop (and its dependencies) with special commands to be sure your containers and volumes will be recreacted/renewed:
Or specifically, only starting PrestaShop (and its dependencies) with special commands to be sure your containers and volumes will be recreacted/renewed:

```shell
docker compose up prestashop --force-recreate --renew-anon-volumes
Expand Down Expand Up @@ -74,4 +74,4 @@ docker compose build --no-cache

## Usage

Once the environment is running, simply navigate to the e2e directory and run the e2e tests.
Once the environment is running, navigate to the e2e directory and run the e2e tests.
2 changes: 1 addition & 1 deletion src/Api/LiveSyncApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private function getClient($timeout = Config::SYNC_API_MAX_TIMEOUT)
{
return (new ClientFactory())->getClient([
'allow_redirects' => true,
'connect_timeout' => 10,
'connect_timeout' => 5,
'http_errors' => false,
'timeout' => $timeout,
]);
Expand Down
38 changes: 0 additions & 38 deletions src/Api/SyncApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ class SyncApiClient
*/
private $jwt;

/**
* Accounts Shop UUID
*
* @var string
*/
private $shopId;

/**
* @param string $syncApiUrl
* @param \Ps_eventbus $module
Expand All @@ -43,7 +36,6 @@ public function __construct($syncApiUrl, \Ps_eventbus $module, PsAccountsAdapter
{
$this->module = $module;
$this->jwt = $psAccountsAdapterService->getOrRefreshToken();
$this->shopId = $psAccountsAdapterService->getShopUuid();
$this->syncApiUrl = $syncApiUrl;
}

Expand Down Expand Up @@ -88,34 +80,4 @@ public function validateJobId($jobId)
'httpCode' => $response->getStatusCode(),
];
}

/**
* @param array<mixed> $shopContent
* @param int $shopContentId
* @param string $action
*
* @return array<mixed>
*/
public function liveSync($shopContent, $shopContentId, $action)
{
$response = $this->getClient(3)->sendRequest(
new Request(
'POST',
$this->syncApiUrl . '/notify/' . $this->shopId,
[
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $this->jwt,
'User-Agent' => 'ps-eventbus/' . $this->module->version,
'Content-Type' => 'application/json',
],
'{"shopContents":' . json_encode($shopContent) . ', "shopContentId": ' . $shopContentId . ', "action": "' . $action . '"}'
)
);

return [
'status' => substr((string) $response->getStatusCode(), 0, 1) === '2',
'httpCode' => $response->getStatusCode(),
'body' => $response->getBody(),
];
}
}
4 changes: 2 additions & 2 deletions src/Service/ApiAuthorizationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ public function __construct(
*/
public function authorizeCall($jobId)
{
// Check if job already exist
// Check if the job already exists
$job = $this->eventbusSyncRepository->findJobById($jobId);

if ($job) {
return true;
}

// Check the jobId validity to avoid Dnial Of Service
// Check the jobId validity to avoid Denial Of Service
$jobValidationResponse = $this->syncApiClient->validateJobId($jobId);

if (!is_array($jobValidationResponse) || (int) $jobValidationResponse['httpCode'] !== 201) {
Expand Down
16 changes: 13 additions & 3 deletions src/Service/SynchronizationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PrestaShop\Module\PsEventbus\Service;

use PrestaShop\Module\PsEventbus\Api\LiveSyncApiClient;
use PrestaShop\Module\PsEventbus\Decorator\PayloadDecorator;
use PrestaShop\Module\PsEventbus\Provider\PaginatedApiDataProviderInterface;
use PrestaShop\Module\PsEventbus\Repository\DeletedObjectsRepository;
Expand Down Expand Up @@ -187,7 +188,7 @@ public function handleIncrementalSync(
}

/**
* disables liveSync
* liveSync
*
* @param string $shopContent
* @param int $shopContentId
Expand All @@ -197,8 +198,17 @@ public function handleIncrementalSync(
*/
public function sendLiveSync($shopContent, $shopContentId, $action)
{
if ($this->isFullSyncDone($shopContent)) {
// $this->debounceLiveSync($shopContent);
$defaultIsoCode = $this->languageRepository->getDefaultLanguageIsoCode();

if ($this->isFullSyncDone($shopContent, $defaultIsoCode) && $this->debounceLiveSync($shopContent)) {
try {
/** @var LiveSyncApiClient $liveSyncApiClient */
$liveSyncApiClient = $this->module->getService('PrestaShop\Module\PsEventbus\Api\LiveSyncApiClient');

$liveSyncApiClient->liveSync($shopContent, (int) $shopContentId, $action);
} catch (\Exception $e) {
// FIXME : report this error somehow
}
}
}

Expand Down

0 comments on commit 2f16ef0

Please sign in to comment.