Skip to content

Commit

Permalink
🐛 fix: blocked event loop causes not emitting socket messages
Browse files Browse the repository at this point in the history
  • Loading branch information
david-vaclavek committed Oct 5, 2023
1 parent 4f5e0c1 commit ac8d373
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
13 changes: 13 additions & 0 deletions server/services/localazy-transfer-download-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ 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}`, {
message: 'Download started',
});
Expand Down Expand Up @@ -87,6 +88,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}`, {
success,
message,
Expand All @@ -106,6 +108,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}`, {
success,
message,
Expand All @@ -126,6 +129,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}`, {
success,
message,
Expand All @@ -141,6 +145,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}`, {
success,
message,
Expand Down Expand Up @@ -184,10 +189,12 @@ module.exports = ({ strapi }) => ({
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}`, {
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}`, {
message: e.message,
});
Expand Down Expand Up @@ -320,13 +327,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}`, {
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}`, {
message: `Cannot create an entry in ${isoStrapi} for ${uid}[${baseEntry.id}]: ${e.message}`,
});
Expand All @@ -348,13 +357,15 @@ 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}`, {
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}`, {
message: `Cannot update an ${uid}[${baseEntryCurrentLanguageLocalizationInfo.id}] (${isoStrapi}): ${e.message}`,
});
Expand All @@ -363,6 +374,7 @@ module.exports = ({ strapi }) => ({
} 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}`, {
success,
message: `An error occured while processing download: ${e.message}`,
Expand All @@ -372,6 +384,7 @@ module.exports = ({ strapi }) => ({
}
}

await new Promise((resolve) => setTimeout(resolve, 1));
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${DOWNLOAD_FINISHED_EVENT}:${streamIdentifier}`, {
success,
message: 'Download finished',
Expand Down
6 changes: 6 additions & 0 deletions server/services/localazy-transfer-upload-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const { UPLOAD_EVENT, UPLOAD_FINISHED_EVENT } = require('../constants/events');

module.exports = ({ strapi }) => ({
async upload(streamIdentifier, ctx) {
await new Promise((resolve) => setTimeout(resolve, 1));
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${UPLOAD_EVENT}:${streamIdentifier}`, {
message: 'Upload started',
});
Expand Down Expand Up @@ -48,6 +49,7 @@ module.exports = ({ strapi }) => ({
if (!contentTransferSetup.has_setup) {
const message = "Content transfer setup is not set up.";
success = false;
await new Promise((resolve) => setTimeout(resolve, 1));
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${UPLOAD_FINISHED_EVENT}:${streamIdentifier}`, {
success,
message,
Expand Down Expand Up @@ -79,6 +81,7 @@ module.exports = ({ strapi }) => ({

if (!isCollectionTransferEnabled(setup, collectionName)) {
const message = `Collection ${collectionName} transfer is disabled.`;
await new Promise((resolve) => setTimeout(resolve, 1));
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${UPLOAD_EVENT}:${streamIdentifier}`, {
message,
});
Expand All @@ -91,6 +94,7 @@ module.exports = ({ strapi }) => ({
const pickPaths = getPickPathsWithComponents(currentTransferSetupModel);
if (!pickPaths.length) {
const message = `No fields for collection ${collectionName} transfer are enabled.`;
await new Promise((resolve) => setTimeout(resolve, 1));
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${UPLOAD_EVENT}:${streamIdentifier}`, {
message,
});
Expand Down Expand Up @@ -157,13 +161,15 @@ module.exports = ({ strapi }) => ({
// Use `deprecate: "file"` if there is one chunk of transferred data only!
const hasMoreTransferFilesChunks = importFile.length > 1;
const uploadConfig = !hasMoreTransferFilesChunks ? { deprecate: "file" } : {};
await new Promise((resolve) => setTimeout(resolve, 1));
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${UPLOAD_EVENT}:${streamIdentifier}`, {
message: "Uploading collections to Localazy...",
});
await LocalazyUploadService.upload(
importFile,
uploadConfig
);
await new Promise((resolve) => setTimeout(resolve, 1));
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${UPLOAD_FINISHED_EVENT}:${streamIdentifier}`, {
success,
message: "Upload finished",
Expand Down

0 comments on commit ac8d373

Please sign in to comment.