Skip to content

Commit

Permalink
fix/crash product suppliers sync (#245)
Browse files Browse the repository at this point in the history
Co-authored-by: Clément Désiles <[email protected]>
  • Loading branch information
Clap404 and jokesterfr authored Feb 21, 2024
1 parent 49212a6 commit a7a76a6
Show file tree
Hide file tree
Showing 26 changed files with 526 additions and 403 deletions.
5 changes: 4 additions & 1 deletion controllers/front/apiInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ public function postProcess()
$langIso = Tools::getValue('lang_iso', '');
$serverInfo = $serverInformationRepository->getServerInformation($langIso);

/** @var bool $initFullSync */
$initFullSync = \Tools::getValue('full', 0) == 1;

try {
$response = $this->proxyService->upload($jobId, $serverInfo, $this->startTime);
$response = $this->proxyService->upload($jobId, $serverInfo, $this->startTime, $initFullSync);
} catch (EnvVarException|Exception $exception) {
$this->exitWithExceptionMessage($exception);
}
Expand Down
5 changes: 4 additions & 1 deletion controllers/front/apiThemes.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ public function postProcess()
/** @var array $themeInfo */
$themeInfo = $themeRepository->getThemes();

/** @var bool $initFullSync */
$initFullSync = \Tools::getValue('full', 0) == 1;

try {
$response = $this->proxyService->upload($jobId, $themeInfo, $this->startTime);
$response = $this->proxyService->upload($jobId, $themeInfo, $this->startTime, $initFullSync);
} catch (EnvVarException|Exception $exception) {
$this->exitWithExceptionMessage($exception);
}
Expand Down
4 changes: 2 additions & 2 deletions e2e-env/cloudsync-mock/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "cloudsync-mock",
"version": "0.0.0",
"main": "dist/app.js",
"type": "module",
"type": "module",
"private": true,
"engines": {
"yarn": "please use pnpm",
Expand Down Expand Up @@ -35,6 +35,6 @@
"nodemon": "^3.0.2",
"rimraf": "^5.0.5",
"typescript": "^5.3.3",
"ws": "^8.15.1"
"ws": "^8.16.0"
}
}
8 changes: 4 additions & 4 deletions e2e-env/cloudsync-mock/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 8 additions & 11 deletions e2e-env/cloudsync-mock/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { CollectorApiServer } from "./lib/collector-api";
import { LiveSyncApiServer } from "./lib/live-sync-api";
import { SyncApiServer } from "./lib/sync-api";
import {CollectorApiServer} from "./lib/collector-api";
import {LiveSyncApiServer} from "./lib/live-sync-api";
import {SyncApiServer} from "./lib/sync-api";
import {WsServer} from "./lib/ws-server";

const syncApi = new SyncApiServer(process.env.SYNC_API_PORT ?? "3232");
const collectorApi = new CollectorApiServer(
process.env.COLLECTOR_API_PORT ?? "3333"
);
const liveSyncApi = new LiveSyncApiServer(process.env.LIVE_SYNC_API_PORT ?? '3434');
const probe = new WsServer(+process.env.PROBE_PORT || 8080)

syncApi.listen();
collectorApi.listen();
liveSyncApi.listen();
new SyncApiServer(probe).listen(+process.env.SYNC_API_PORT || 3232);
new CollectorApiServer(probe).listen(+process.env.COLLECTOR_API_PORT || 3333);
new LiveSyncApiServer(probe).listen(+process.env.LIVE_SYNC_API_PORT || 3434);
5 changes: 3 additions & 2 deletions e2e-env/cloudsync-mock/src/lib/collector-api.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Server } from "./server";
import {WsServer} from "./ws-server";

export class CollectorApiServer extends Server {
public constructor(port: string) {
super(parseInt(port));
public constructor( probe: WsServer) {
super( probe);

this.api.get("/", (_req, res) => {
res.status(200).end();
Expand Down
5 changes: 3 additions & 2 deletions e2e-env/cloudsync-mock/src/lib/live-sync-api.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Server } from "./server";
import {WsServer} from "./ws-server";

export class LiveSyncApiServer extends Server {
public constructor(port: string) {
super(parseInt(port));
public constructor(probe: WsServer) {
super( probe);

this.api.get("/", (_req, res) => {
res.status(200).end();
Expand Down
30 changes: 14 additions & 16 deletions e2e-env/cloudsync-mock/src/lib/server.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
import express, { Request, Response, Express, NextFunction } from "express";
import { WsServer } from "./ws-server";
// @ts-expect-error Express is imported as commonjs module. Raises an error because there is no tsconfig.
import express, {Request, Response, Express, NextFunction} from "express";
import {WsServer} from "./ws-server";

export class Server {
api: Express;
port: number;

wsServer: WsServer;

public constructor(port: number) {
public constructor(probe: WsServer) {
this.api = express();

const wsServer = WsServer.getInstance();

this.api.use((req: Request, res: Response, next: NextFunction) => {
wsServer.sendDataToWS(this.constructor.name, req);
next();
this.api.get("/healthcheck", (_req, res) => {
res.status(200).send({mock: this.constructor.name});
});

this.api.use((req: Request, res: Response, next: NextFunction) => {
//TODO : make prettier
// send data to probe after parsing params
req.on('close', () => {
probe.sendDataToWS(this.constructor.name, req);
})
req.on('data', buf => console.log(buf.toString('utf8')));
next();
});
this.port = port;
}

public async listen() {
console.log(`${this.constructor.name} listening on port \x1b[96m${this.port}\x1b[0m`);
return this.api.listen(this.port);
public async listen(port: number) {
console.log(`${this.constructor.name} listening on port \x1b[96m${port}\x1b[0m`);
return this.api.listen(port);
}
}
7 changes: 4 additions & 3 deletions e2e-env/cloudsync-mock/src/lib/sync-api.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Server } from "./server";
import {Server} from "./server";
import {WsServer} from "./ws-server";

export class SyncApiServer extends Server {
public constructor(port: string) {
super(parseInt(port));
public constructor(probe: WsServer) {
super(probe);

this.api.get("/", (_req, res) => {
res.status(200).end();
Expand Down
80 changes: 39 additions & 41 deletions e2e-env/cloudsync-mock/src/lib/ws-server.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,43 @@
import WebSocket, { WebSocketServer } from 'ws';
import { Request } from "express";
import {WebSocketServer} from 'ws';
import {Request} from "express";

/**
* Websocket server used by [mock-probe.ts] to wait for and inspect requests during testing.
*/
export class WsServer {
private static instance: WsServer;
private static server: WebSocketServer;
private static client: WebSocket|null;

private constructor() { }

public static getInstance(): WsServer {
if (!WsServer.instance) {
WsServer.instance = new WsServer();

WsServer.server = new WebSocketServer({ port: 8080 });

WsServer.server.on('connection', (ws: WebSocket) => {
if (WsServer.server.clients.size > 1) {
throw new Error('Too many connection to websocket server !');
}
WsServer.client = ws;
});
WsServer.server.on('error', err => { throw err } )

console.log(`WS server started on port \x1b[96m8080\x1b[0m`);
}

return WsServer.instance;
}

public sendDataToWS(apiName: string, request: Request) {
if (!WsServer.client) return;

const data = {
apiName,
method: request.method,
headers: request.headers,
url: request.url,
query: request.query,
body: request.body ?? {},
};

WsServer.client.send(JSON.stringify(data));
private server: WebSocketServer;

constructor(port: number) {
this.server = new WebSocketServer({port});

this.server.on('error', err => {
console.error(err);
})

console.log(`Probe listening on port \x1b[96m${port}\x1b[0m`);
}

/**
* send data to all connected clients
* @param apiName
* @param request
*/
public sendDataToWS(apiName: string, request: Request) {
const data = {
apiName,
method: request.method,
headers: request.headers,
url: request.url,
query: request.query,
params: request.params,
body: request.body ?? {},
};

// there may be no client if we're not runing an automated test suite
if (this.server.clients.size > 0) {
this.server.clients.forEach((client) => {
client.send(JSON.stringify(data));
})
}
}
}
4 changes: 2 additions & 2 deletions e2e-env/init-scripts/install-module.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ ps_eventbus_install() {
# - ..:/var/www/html/modules/ps_eventbus:rw => mount all the sources
# - /var/www/html/modules/ps_eventbus/vendor => void the specific vendor dir, makint it empty
# - /var/www/html/modules/ps_eventbus/tools/vendor => void the specific vendor dev dir, making it empty
#
# That said, we now want our container to have RW access on these directories,
#
# That said, we now want our container to have RW access on these directories,
# and to install the required composer dependencies for the module to work.
#
# Other scenarios could be imagined, but this is the best way to avoid writes on a mounted volume,
Expand Down
61 changes: 32 additions & 29 deletions e2e/package.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
{
"name": "ps_eventbus_e2e_tests",
"version": "0.0.0",
"private": true,
"scripts": {
"test:e2e": "jest --config=jest.config.json --runInBand",
"test:e2e:watch": "jest --config=jest.config.json --watchAll --runInBand",
"test:e2e:detectOpenHandles": "jest --config=jest.config.json --runInBand --detectOpenHandles"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"@types/jest": "^29.5.10",
"@types/node": "^20.10.0",
"@types/supertest": "^2.0.16",
"jest-expect-message": "^1.1.3",
"jest-extended": "^4.0.2",
"jest-mock-extended": "^3.0.5",
"jest": "^29.7.0",
"supertest": "6.3.3",
"ts-jest": "^29.1.1",
"typescript": "^5.3.2",
"@types/ws": "^8.5.10",
"ws": "^8.15.1"
},
"engines": {
"yarn": "please use pnpm",
"npm": "please use pnpm",
"node": ">=20",
"pnpm": ">=8"
}
"name": "ps_eventbus_e2e_tests",
"version": "0.0.0",
"private": true,
"scripts": {
"test:e2e": "jest --config=jest.config.json",
"test:e2e:watch": "jest --config=jest.config.json --watchAll",
"test:e2e:detectOpenHandles": "jest --config=jest.config.json --detectOpenHandles"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"@types/jest": "^29.5.10",
"@types/node": "^20.10.0",
"@types/supertest": "^2.0.16",
"jest-expect-message": "^1.1.3",
"jest-extended": "^4.0.2",
"jest-mock-extended": "^3.0.5",
"jest": "^29.7.0",
"ts-jest": "^29.1.1",
"typescript": "^5.3.2",
"@types/ws": "^8.5.10",
"ws": "^8.16.0",
"rxjs": "^7.8.1",
"ramda": "^0.29.1",
"@types/ramda": "^0.29.1",
"axios": "^1.6.7"
},
"engines": {
"yarn": "please use pnpm",
"npm": "please use pnpm",
"node": ">=18",
"pnpm": ">=8"
}
}
Loading

0 comments on commit a7a76a6

Please sign in to comment.