Skip to content

Commit

Permalink
♻️ refactor: use job notification service; use it for download
Browse files Browse the repository at this point in the history
  • Loading branch information
david-vaclavek committed Oct 6, 2023
1 parent ac8d373 commit 9f745c6
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 39 deletions.
9 changes: 5 additions & 4 deletions server/controllers/localazy-transfer-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const generateRandomId = require('../utils/generate-random-id');
const { LOCALAZY_PLUGIN_CHANNEL } = require('../constants/channels');
const { UPLOAD_FINISHED_EVENT, DOWNLOAD_FINISHED_EVENT } = require('../constants/events');
const jobNotificationServiceFactory = require('../services/helpers/job-notification-service');

module.exports = {
async upload(ctx) {
Expand Down Expand Up @@ -35,7 +36,7 @@ module.exports = {
},

async download(ctx) {
const streamIdentifier = generateRandomId();
const JobNotificationService = new jobNotificationServiceFactory(strapi.StrapIO);
try {
const LocalazyTransferDownloadService = strapi
.plugin("localazy")
Expand All @@ -46,14 +47,14 @@ module.exports = {
* (to let the client receive and subscribe to the messages stream)
*/
// TODO: let the client send a message to the server to start the download (that it's subscribed to the stream)
setTimeout(() => (LocalazyTransferDownloadService.download(streamIdentifier, ctx)), 1000);
setTimeout(() => (LocalazyTransferDownloadService.download(JobNotificationService, ctx)), 1000);

ctx.body = {
streamIdentifier,
streamIdentifier: JobNotificationService.getStreamIdentifier(),
};
} catch (e) {
strapi.log.error(e.message);
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${DOWNLOAD_FINISHED_EVENT}:${streamIdentifier}`, {
await JobNotificationService.emit(DOWNLOAD_FINISHED_EVENT, {
success: false,
message: e.message,
});
Expand Down
30 changes: 30 additions & 0 deletions server/services/helpers/job-notification-service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"use strict";

const { LOCALAZY_PLUGIN_CHANNEL } = require('../../constants/channels');
const generateRandomId = require('../../utils/generate-random-id');

class JobNotificationService {
_strapio;
_channel;
_jobStreamIdentifier;
constructor(strapio) {
this._strapio = strapio;
this._channel = LOCALAZY_PLUGIN_CHANNEL;
this._jobStreamIdentifier = generateRandomId();
}

setChannel(channel) {
this._channel = channel;
}

getStreamIdentifier() {
return this._jobStreamIdentifier;
}

async emit(event, data) {
await new Promise((resolve) => setImmediate(resolve));
this._strapio.emitRaw(this._channel, `${event}:${this._jobStreamIdentifier}`, data);
}
}

module.exports = JobNotificationService;
63 changes: 28 additions & 35 deletions server/services/localazy-transfer-download-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const set = require("lodash/set");
const isEmpty = require("lodash/isEmpty");
const RequestInitiatorHelper = require('../utils/request-initiator-helper');
const PluginSettingsServiceHelper = require('../services/helpers/plugin-settings-service-helper');
const { LOCALAZY_PLUGIN_CHANNEL } = require('../constants/channels');
const { DOWNLOAD_EVENT, DOWNLOAD_FINISHED_EVENT } = require('../constants/events');

const getFilteredLanguagesCodesForDownload = async (languagesCodes) => {
Expand All @@ -39,14 +38,14 @@ const getFilteredLanguagesCodesForDownload = async (languagesCodes) => {
};

module.exports = ({ strapi }) => ({
async download(streamIdentifier, ctx) {
await new Promise((resolve) => setTimeout(resolve, 1));
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${DOWNLOAD_EVENT}:${streamIdentifier}`, {
async download(JobNotificationService, ctx) {
console.time("download");
strapi.log.info("Download started");
await JobNotificationService.emit(DOWNLOAD_EVENT, {
message: 'Download started',
});

let success = true;
const messageReport = [];

// Strapi service
const StrapiService = strapi
Expand Down Expand Up @@ -88,8 +87,7 @@ module.exports = ({ strapi }) => ({
success = false;
const message = `File ${config.LOCALAZY_DEFAULT_FILE_NAME} not found`;
strapi.log.error(message);
await new Promise((resolve) => setTimeout(resolve, 1));
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${DOWNLOAD_FINISHED_EVENT}:${streamIdentifier}`, {
await JobNotificationService.emit(DOWNLOAD_FINISHED_EVENT, {
success,
message,
});
Expand All @@ -108,8 +106,7 @@ module.exports = ({ strapi }) => ({
success = false;
const message = `Project ${user.project.id} not found`;
strapi.log.error(message);
await new Promise((resolve) => setTimeout(resolve, 1));
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${DOWNLOAD_FINISHED_EVENT}:${streamIdentifier}`, {
await JobNotificationService.emit(DOWNLOAD_FINISHED_EVENT, {
success,
message,
});
Expand All @@ -129,8 +126,7 @@ module.exports = ({ strapi }) => ({
success = false;
const message = "Source language not found";
strapi.log.error(message);
await new Promise((resolve) => setTimeout(resolve, 1));
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${DOWNLOAD_FINISHED_EVENT}:${streamIdentifier}`, {
await JobNotificationService.emit(DOWNLOAD_FINISHED_EVENT, {
success,
message,
});
Expand All @@ -145,8 +141,7 @@ module.exports = ({ strapi }) => ({
if (!projectLanguagesWithoutSourceLanguage.length) {
const message = "Your Localazy project is not translated to other languages.";
strapi.log.info(message);
await new Promise((resolve) => setTimeout(resolve, 1));
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${DOWNLOAD_FINISHED_EVENT}:${streamIdentifier}`, {
await JobNotificationService.emit(DOWNLOAD_FINISHED_EVENT, {
success,
message,
});
Expand Down Expand Up @@ -183,19 +178,19 @@ module.exports = ({ strapi }) => ({
ctx,
isoLocalazy
);
messageReport.push(`Created locale ${newLocaleCode}`);
await JobNotificationService.emit(DOWNLOAD_EVENT, {
message: `Created locale ${newLocaleCode}`,
});
} catch (e) {
strapi.log.error(e.message);
if (e.name === "ValidationError") {
// store unsupported language code
strapiUnsupportedLanguages.push(isoLocalazy);
await new Promise((resolve) => setTimeout(resolve, 1));
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${DOWNLOAD_EVENT}:${streamIdentifier}`, {
await JobNotificationService.emit(DOWNLOAD_EVENT, {
message: `Language ${isoLocalazy} is not supported by Strapi`,
});
} else {
await new Promise((resolve) => setTimeout(resolve, 1));
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${DOWNLOAD_EVENT}:${streamIdentifier}`, {
await JobNotificationService.emit(DOWNLOAD_EVENT, {
message: e.message,
});
}
Expand All @@ -217,7 +212,9 @@ module.exports = ({ strapi }) => ({
lang: isoStrapi,
});
if (!result.success) {
messageReport.push(result.message);
await JobNotificationService.emit(DOWNLOAD_EVENT, {
message: result.message,
});
}
const langKeys = result.data;
localazyContent[isoLocalazy] = langKeys;
Expand All @@ -231,9 +228,9 @@ module.exports = ({ strapi }) => ({
for (const [isoLocalazy, keys] of Object.entries(localazyContent)) {
const isoStrapi = isoLocalazyToStrapi(isoLocalazy);
if (!isoStrapi) {
messageReport.push(
`Language ${isoLocalazy} is not supported by Strapi`
);
await JobNotificationService.emit(DOWNLOAD_EVENT, {
message: `Language ${isoLocalazy} is not supported by Strapi`,
});
continue;
}

Expand Down Expand Up @@ -326,17 +323,15 @@ module.exports = ({ strapi }) => ({
);

const message = `Created new entry ${uid}[${createdEntry.id}] in language ${isoStrapi}`;
strapi.log.info(`Created new entry ${uid}[${createdEntry.id}] in language ${isoStrapi}`);
await new Promise((resolve) => setTimeout(resolve, 1));
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${DOWNLOAD_EVENT}:${streamIdentifier}`, {
strapi.log.info(message);
await JobNotificationService.emit(DOWNLOAD_EVENT, {
message,
});
} catch (e) {
success = false;
strapi.log.error(e.message);
strapi.log.error(JSON.stringify(e.details?.errors || {}));
await new Promise((resolve) => setTimeout(resolve, 1));
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${DOWNLOAD_EVENT}:${streamIdentifier}`, {
await JobNotificationService.emit(DOWNLOAD_EVENT, {
message: `Cannot create an entry in ${isoStrapi} for ${uid}[${baseEntry.id}]: ${e.message}`,
});
}
Expand All @@ -357,25 +352,22 @@ module.exports = ({ strapi }) => ({

const message = `Updated ${uid}[${updatedEntry.id}] (${isoStrapi})`;
strapi.log.info(message);
await new Promise((resolve) => setTimeout(resolve, 1));
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${DOWNLOAD_EVENT}:${streamIdentifier}`, {
await JobNotificationService.emit(DOWNLOAD_EVENT, {
message,
});
} catch (e) {
success = false;
strapi.log.error(e.message);
strapi.log.error(JSON.stringify(e.details?.errors || {}));
await new Promise((resolve) => setTimeout(resolve, 1));
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${DOWNLOAD_EVENT}:${streamIdentifier}`, {
await JobNotificationService.emit(DOWNLOAD_EVENT, {
message: `Cannot update an ${uid}[${baseEntryCurrentLanguageLocalizationInfo.id}] (${isoStrapi}): ${e.message}`,
});
}
}
} catch (e) {
success = false;
strapi.log.error(e.message);
await new Promise((resolve) => setTimeout(resolve, 1));
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${DOWNLOAD_FINISHED_EVENT}:${streamIdentifier}`, {
await JobNotificationService.emit(DOWNLOAD_FINISHED_EVENT, {
success,
message: `An error occured while processing download: ${e.message}`,
});
Expand All @@ -384,10 +376,11 @@ module.exports = ({ strapi }) => ({
}
}

await new Promise((resolve) => setTimeout(resolve, 1));
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${DOWNLOAD_FINISHED_EVENT}:${streamIdentifier}`, {
strapi.log.info("Download finished in");
await JobNotificationService.emit(DOWNLOAD_FINISHED_EVENT, {
success,
message: 'Download finished',
});
console.timeEnd("download");
},
});

0 comments on commit 9f745c6

Please sign in to comment.