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 142 | 1x 1x 2x 2x 1x 1x 2x 12x 2x 2x 1x 1x 1x 6x 6x 6x 6x 6x 6x 3x 2x 3x 2x 1x 4x 4x 2x 6x 6x 6x 6x 5x 4x 2x 2x 2x 3x 3x | /* eslint-disable no-useless-catch */ import moment from 'moment-timezone'; import { getJobsDefined, reloadJobs, runJob } from '../crons'; import CronsInformation from '../source/crons/information'; import CronsUpdate from '../source/crons/update'; import CronsCreate from '../source/crons/create'; function reduceJobs(crons = []) { const jobs = getJobsDefined(); return jobs.reduce((acum, job) => { const findedCron = crons.find((it) => it.name === job.name); // CRONS EXISTENTES if (findedCron) { acum.push({ name: findedCron.name, date: findedCron.date, cron: findedCron.cron, interval: findedCron.interval, jsonParams: findedCron.jsonParams, enabled: findedCron.enabled, description: typeof job.description === 'function' ? job.description() : job.description, }); } else { // CRONS NAO EXISTENTES acum.push({ name: job.name, enabled: false, description: typeof job.description === 'function' ? job.description() : job.description, }); } return acum; }, []); } function searchJob(name = '') { return getJobsDefined().find((job) => job.name === name); } async function getCrons(req, res) { try { const crons = await CronsInformation.getAllCrons(); const values = reduceJobs(crons); res.json({ success: true, data: values }); } catch (err) { throw err; } } async function updateCron(req, res) { try { const { name, date = null, cron = null, interval = null, jsonParams, enabled, } = req.body; const jobFinded = searchJob(name); // Localiza um job por name const { cronValue } = req; let changedCrons = false; // Se existe uma cron entao atualiza if (cronValue) { await CronsUpdate.updateCron(cronValue.id, { date, cron, interval, jsonParams, enabled, updatedUser: req.userId, }); changedCrons = true; } else if (jobFinded) { // Senao existe uma cron mas existe o job cadastrado entao cria uma cron await CronsCreate.createCron({ name, date, cron, interval, jsonParams, enabled, updatedUser: req.userId, }); changedCrons = true; } // Se houve mudanças nas crons entao aplica um reload nos jobs if (changedCrons) reloadJobs(); res.json({ success: true }); } catch (err) { throw err; } } async function executeJob(req, res) { try { const { name } = req.body; const jobFinded = searchJob(name); // Localiza um job por name const cronValue = await CronsInformation.getCron({ name }); // Localiza uma cron por name // Se existe uma cron entao atualiza if (jobFinded) { // Se existe a cron ja criada na tabela entao atualiza if (cronValue) { await CronsUpdate.updateCron(cronValue.id, { forcedExecutionAt: moment().format(), forcedExecutionUser: req.userId, }); } else { await CronsCreate.createCron({ name, updatedUser: req.userId, forcedExecutionAt: moment().format(), forcedExecutionUser: req.userId, }); } runJob(name); // Executa o Job informado } res.json({ success: true }); } catch (err) { throw err; } } export default { getCrons, updateCron, executeJob, }; |