feature(ee): WIP weekly cron job

This commit is contained in:
Jamie Howard 2023-07-13 15:03:23 +01:00
parent f86ca68071
commit 1a8b6e78d8
9 changed files with 79 additions and 14 deletions

View File

@ -31,6 +31,8 @@ module.exports = async () => {
// Decorate the entity service with review workflow logic
const { decorator } = getService('review-workflows-decorator');
strapi.entityService.decorate(decorator);
await getService('review-workflows-weekly-metrics').registerCron();
}
await getService('seat-enforcement').seatEnforcementWorkflow();

View File

@ -12,4 +12,5 @@ module.exports = {
'review-workflows-validation': require('./review-workflows/validation'),
'review-workflows-decorator': require('./review-workflows/entity-service-decorator'),
'review-workflows-metrics': require('./review-workflows/metrics'),
'review-workflows-weekly-metrics': require('./review-workflows/metrics/weekly-metrics'),
};

View File

@ -24,6 +24,16 @@ const sendDidEditWorkflow = async () => {
strapi.telemetry.send('didEditWorkflow', {});
};
const sendDidSendReviewWorkflowPropertiesOnceAWeek = async (
numberOfActiveWorkflows,
activatedContentTypes
) => {
strapi.telemetry.send('didSendReviewWorkflowPropertiesOnceAWeek', {
numberOfActiveWorkflows,
activatedContentTypes,
});
};
module.exports = {
sendDidCreateStage,
sendDidEditStage,
@ -31,4 +41,5 @@ module.exports = {
sendDidChangeEntryStage,
sendDidCreateWorkflow,
sendDidEditWorkflow,
sendDidSendReviewWorkflowPropertiesOnceAWeek,
};

View File

@ -0,0 +1,56 @@
'use strict';
const { defaultTo } = require('lodash/fp');
const { add } = require('date-fns');
const { ONE_WEEK, getWeeklyCronScheduleAt } = require('@strapi/utils').cron;
const { getService } = require('../../../../../server/utils');
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 });
module.exports = ({ strapi }) => {
const metrics = getService('review-workflows-metrics', { strapi });
return {
async computeMetrics() {
/*
TODO: compute metrics
numberOfActiveWorkflows,
activatedContentTypes
*/
},
async sendMetrics() {
metrics.sendDidSendReviewWorkflowPropertiesOnceAWeek();
const metricsInfoStored = await getMetricsStoreValue();
await setMetricsStoreValue({ ...metricsInfoStored, lastWeeklyUpdate: new Date().getTime() });
},
async ensureWeeklyStoredCronSchedule() {
const metricsInfoStored = await getMetricsStoreValue();
const { weeklySchedule: currentSchedule, lastWeeklyUpdate } = metricsInfoStored;
const now = new Date();
let weeklySchedule = currentSchedule;
if (!currentSchedule || !lastWeeklyUpdate || lastWeeklyUpdate + ONE_WEEK < now.getTime()) {
weeklySchedule = getWeeklyCronScheduleAt(add(now, { minutes: 5 }));
await setMetricsStoreValue({ ...metricsInfoStored, weeklySchedule });
}
return weeklySchedule;
},
async registerCron() {
const weeklySchedule = await this.ensureWeeklyStoredCronSchedule();
strapi.cron.add({ [weeklySchedule]: this.sendMetrics.bind(this) });
},
};
};

View File

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

View File

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

View File

@ -1,6 +1,4 @@
'use strict';
const { getWeeklyCronScheduleAt } = require('../cron');
import { getWeeklyCronScheduleAt } from '../cron';
describe('cron', () => {
describe('getWeeklyCronScheduleAt', () => {

View File

@ -0,0 +1,6 @@
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,3 +41,4 @@ 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';