Skip to content

Commit

Permalink
♻️ refactor: use job notification service for upload
Browse files Browse the repository at this point in the history
  • Loading branch information
david-vaclavek committed Oct 6, 2023
1 parent 9f745c6 commit ea952e0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
10 changes: 4 additions & 6 deletions server/controllers/localazy-transfer-controller.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
"use strict";


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) {
const streamIdentifier = generateRandomId();
const JobNotificationService = new jobNotificationServiceFactory(strapi.StrapIO);
try {
const LocalazyTransferUploadService = strapi
.plugin("localazy")
Expand All @@ -19,15 +17,15 @@ 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 upload (that it's subscribed to the stream)
setTimeout(() => (LocalazyTransferUploadService.upload(streamIdentifier, ctx)), 1000);
setTimeout(() => (LocalazyTransferUploadService.upload(JobNotificationService, ctx)), 1000);

ctx.body = {
streamIdentifier,
streamIdentifier: JobNotificationService.getStreamIdentifier(),
};

} catch (e) {
strapi.log.error(e.message);
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${UPLOAD_FINISHED_EVENT}:${streamIdentifier}`, {
await JobNotificationService.emit(UPLOAD_FINISHED_EVENT, {
success: false,
message: e.message,
});
Expand Down
24 changes: 10 additions & 14 deletions server/services/localazy-transfer-upload-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ const {
isoStrapiToLocalazy,
} = require("../utils/iso-locales-utils");
const omitDeep = require("../utils/omit-deep");
const { LOCALAZY_PLUGIN_CHANNEL } = require('../constants/channels');
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}`, {
async upload(JobNotificationService, ctx) {
console.time("upload");
await JobNotificationService.emit(UPLOAD_EVENT, {
message: 'Upload started',
});

Expand Down Expand Up @@ -49,8 +48,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}`, {
await JobNotificationService.emit(UPLOAD_FINISHED_EVENT, {
success,
message,
});
Expand Down Expand Up @@ -81,8 +79,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}`, {
await JobNotificationService.emit(UPLOAD_EVENT, {
message,
});
strapi.log.info(message);
Expand All @@ -94,8 +91,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}`, {
await JobNotificationService.emit(UPLOAD_EVENT, {
message,
});
strapi.log.warn(message);
Expand Down Expand Up @@ -161,18 +157,18 @@ 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}`, {
await JobNotificationService.emit(UPLOAD_EVENT, {
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}`, {
strapi.log.info("Upload finished in");
await JobNotificationService.emit(UPLOAD_FINISHED_EVENT, {
success,
message: "Upload finished",
});
console.timeEnd("upload");
},
});

0 comments on commit ea952e0

Please sign in to comment.