fix: move cron utils to upload

This commit is contained in:
Marc-Roig 2023-07-18 16:22:20 +02:00
parent e024f590ab
commit dc09aa2f8f
No known key found for this signature in database
GPG Key ID: FB4E2C43A0BEE249
6 changed files with 23 additions and 15 deletions

View File

@ -2,14 +2,18 @@
const { flow, map, sum, size, mean, max, defaultTo } = require('lodash/fp');
const { add } = require('date-fns');
const { ONE_WEEK, getWeeklyCronScheduleAt } = require('@strapi/utils').cron;
const { getService } = require('../../../../../server/utils');
const ONE_WEEK = 7 * 24 * 60 * 60 * 1000;
const getWeeklyCronScheduleAt = (date) =>
`${date.getSeconds()} ${date.getMinutes()} ${date.getHours()} * * ${date.getDay()}`;
const getMetricsStoreValue = async () => {
const value = await strapi.store.get({ type: 'plugin', name: 'ee', key: 'metrics' });
return defaultTo({}, value);
};
const setMetricsStoreValue = (value) =>
strapi.store.set({ type: 'plugin', name: 'ee', key: 'metrics', value });
@ -20,7 +24,7 @@ module.exports = ({ strapi }) => {
return {
async computeMetrics() {
// There will never be more than 200 workflow, so we can safely fetch them all
const workflows = await workflowsService.findMany({ populate: 'stages' });
const workflows = await workflowsService.find({ populate: 'stages' });
const stagesCount = flow(
map('stages'), // Number of stages per workflow
@ -41,7 +45,7 @@ module.exports = ({ strapi }) => {
},
async sendMetrics() {
const computedMetrics = this.computeMetrics();
const computedMetrics = await this.computeMetrics();
metrics.sendDidSendReviewWorkflowPropertiesOnceAWeek(computedMetrics);
const metricsInfoStored = await getMetricsStoreValue();
@ -56,7 +60,7 @@ module.exports = ({ strapi }) => {
let weeklySchedule = currentSchedule;
if (!currentSchedule || !lastWeeklyUpdate || lastWeeklyUpdate + ONE_WEEK < now.getTime()) {
weeklySchedule = getWeeklyCronScheduleAt(add(now, { minutes: 5 }));
weeklySchedule = getWeeklyCronScheduleAt(add(now, { seconds: 10 }));
await setMetricsStoreValue({ ...metricsInfoStored, weeklySchedule });
}

View File

@ -2,9 +2,11 @@
const { defaultTo } = require('lodash/fp');
const { add } = require('date-fns');
const { ONE_WEEK, getWeeklyCronScheduleAt } = require('@strapi/utils').cron;
const { getWeeklyCronScheduleAt } = require('../../utils/cron');
const { FOLDER_MODEL_UID, FILE_MODEL_UID } = require('../../constants');
const ONE_WEEK = 7 * 24 * 60 * 60 * 1000;
const getMetricsStoreValue = async () => {
const value = await strapi.store.get({ type: 'plugin', name: 'upload', key: 'metrics' });
return defaultTo({}, value);

View File

@ -1,10 +1,11 @@
import { getWeeklyCronScheduleAt } from '../cron';
'use strict';
const { getWeeklyCronScheduleAt } = require('../cron');
describe('cron', () => {
describe('getWeeklyCronScheduleAt', () => {
test('2022-07-22T15:43:40.036 => 40 43 15 * * 5', () => {
const date = new Date('2022-07-22T15:43:40.036'); // it's a friday
const result = getWeeklyCronScheduleAt(date);
expect(result).toBe('40 43 15 * * 5');
});

View File

@ -0,0 +1,8 @@
('use strict');
const getWeeklyCronScheduleAt = (date) =>
`${date.getSeconds()} ${date.getMinutes()} ${date.getHours()} * * ${date.getDay()}`;
module.exports = {
getWeeklyCronScheduleAt,
};

View File

@ -1,6 +0,0 @@
const ONE_WEEK: number = 7 * 24 * 60 * 60 * 1000;
const getWeeklyCronScheduleAt = (date: Date): string => `0 * * * * *`;
// TODO revert to this
// `${date.getSeconds()} ${date.getMinutes()} ${date.getHours()} * * ${date.getDay()}`;
export { ONE_WEEK, getWeeklyCronScheduleAt };

View File

@ -41,4 +41,3 @@ export * as file from './file';
export * as traverse from './traverse';
export { default as webhook } from './webhook';
export { isOperator, isOperatorOfType } from './operators';
export * as cron from './cron';