Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | require('babel-register'); require('babel-polyfill'); const { parentPort, workerData } = require('worker_threads'); const fs = require('fs'); const path = require('path'); const ReportsController = require('../../controllers/reports'); const databaseConnect = require('../../../config/database'); const { default: UserNotificationCreate, } = require('../../source/userNotification/create'); const { default: UserNotificationAttachmentCreate, } = require('../../source/userNotificationAttachment/create'); const { default: UserNotificationTypes, } = require('../../database/enumerators/userNotification'); const { default: I18n } = require('../../helpers/i18n'); const { PATH_ROOT } = require('../../../root'); const { sequelize } = databaseConnect; // Funcao de envio de mensagem utilizada para logging function sendMessage(message = '') { if (parentPort) parentPort.postMessage(message); } function capitalizeFirstLetter(string) { return string.charAt(0).toUpperCase() + string.slice(1); } const jobExec = async () => { const { userId } = workerData.req; const nameDoc = capitalizeFirstLetter(workerData.req.body.nameDoc || ''); try { sendMessage('Database connecting...'); await databaseConnect.default(); await I18n.loadKeys(); await sequelize.transaction(async (transaction) => { // Cadastrar notificação para o usuario com status inativa sendMessage('Gerando relatorio...'); const { filePath, fileName, filenameUniqueId } = await ReportsController.default.generate( workerData.req, workerData.res ); fs.renameSync(filePath, path.join(PATH_ROOT, 'report', filenameUniqueId)); const storagePath = `report/${filenameUniqueId}`; sendMessage('Gravando notificacao...'); // Enviar notificacao const userNotification = await UserNotificationCreate.createUserNotification( { userId, type: UserNotificationTypes.REPORT, readed: false, firstTitle: I18n.t( 'BEE528' /* Relatório */, undefined, workerData.req.appLanguage ), secondTitle: nameDoc, description: I18n.t( 'BEE2569' /* Relatório gerado via segundo plano */, undefined, workerData.req.appLanguage ), contentTitle: `${I18n.t( 'BEE528' /* Relatório */, undefined, workerData.req.appLanguage )} - ${nameDoc}`, contentMessage: I18n.t( 'BEE2569' /* Relatório gerado via segundo plano */, undefined, workerData.req.appLanguage ), }, { transaction } ); await UserNotificationAttachmentCreate.createUserNotificationAttachment( { userNotificationId: userNotification.id, name: fileName, type: fileName.split('.')[1], storagePath, }, { transaction } ); }); } catch (e) { sendMessage('Falha ao gravar notificacao...'); await UserNotificationCreate.createUserNotification({ userId, type: UserNotificationTypes.REPORT, readed: false, firstTitle: I18n.t( 'BEE528' /* Relatório */, undefined, workerData.req.appLanguage ), secondTitle: nameDoc, description: I18n.t( 'BEE547', { 0: e.message } /* Erro ao gerar relatório. Código: %{0} */, undefined, workerData.req.appLanguage ), contentTitle: `${I18n.t( 'BEE528' /* Relatório */, undefined, workerData.req.appLanguage )} - ${nameDoc}`, contentMessage: I18n.t( 'BEE547', { 0: e.message } /* Erro ao gerar relatório. Código: %{0} */, undefined, workerData.req.appLanguage ), }); throw e; } sendMessage('Relatorio gerado...'); }; (async () => { // funcao que executa o job await jobExec(); // processo necessario para informar a conclusao do job if (parentPort) sendMessage('done'); else process.exit(0); })(); |