mirror of
https://github.com/strapi/strapi.git
synced 2025-08-24 08:38:52 +00:00
feature(ee): WIP weekly cron job
This commit is contained in:
parent
f86ca68071
commit
1a8b6e78d8
2
packages/core/admin/ee/server/bootstrap.js
vendored
2
packages/core/admin/ee/server/bootstrap.js
vendored
@ -31,6 +31,8 @@ module.exports = async () => {
|
|||||||
// Decorate the entity service with review workflow logic
|
// Decorate the entity service with review workflow logic
|
||||||
const { decorator } = getService('review-workflows-decorator');
|
const { decorator } = getService('review-workflows-decorator');
|
||||||
strapi.entityService.decorate(decorator);
|
strapi.entityService.decorate(decorator);
|
||||||
|
|
||||||
|
await getService('review-workflows-weekly-metrics').registerCron();
|
||||||
}
|
}
|
||||||
|
|
||||||
await getService('seat-enforcement').seatEnforcementWorkflow();
|
await getService('seat-enforcement').seatEnforcementWorkflow();
|
||||||
|
@ -12,4 +12,5 @@ module.exports = {
|
|||||||
'review-workflows-validation': require('./review-workflows/validation'),
|
'review-workflows-validation': require('./review-workflows/validation'),
|
||||||
'review-workflows-decorator': require('./review-workflows/entity-service-decorator'),
|
'review-workflows-decorator': require('./review-workflows/entity-service-decorator'),
|
||||||
'review-workflows-metrics': require('./review-workflows/metrics'),
|
'review-workflows-metrics': require('./review-workflows/metrics'),
|
||||||
|
'review-workflows-weekly-metrics': require('./review-workflows/metrics/weekly-metrics'),
|
||||||
};
|
};
|
||||||
|
@ -24,6 +24,16 @@ const sendDidEditWorkflow = async () => {
|
|||||||
strapi.telemetry.send('didEditWorkflow', {});
|
strapi.telemetry.send('didEditWorkflow', {});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const sendDidSendReviewWorkflowPropertiesOnceAWeek = async (
|
||||||
|
numberOfActiveWorkflows,
|
||||||
|
activatedContentTypes
|
||||||
|
) => {
|
||||||
|
strapi.telemetry.send('didSendReviewWorkflowPropertiesOnceAWeek', {
|
||||||
|
numberOfActiveWorkflows,
|
||||||
|
activatedContentTypes,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
sendDidCreateStage,
|
sendDidCreateStage,
|
||||||
sendDidEditStage,
|
sendDidEditStage,
|
||||||
@ -31,4 +41,5 @@ module.exports = {
|
|||||||
sendDidChangeEntryStage,
|
sendDidChangeEntryStage,
|
||||||
sendDidCreateWorkflow,
|
sendDidCreateWorkflow,
|
||||||
sendDidEditWorkflow,
|
sendDidEditWorkflow,
|
||||||
|
sendDidSendReviewWorkflowPropertiesOnceAWeek,
|
||||||
};
|
};
|
@ -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) });
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
@ -2,10 +2,8 @@
|
|||||||
|
|
||||||
const { defaultTo } = require('lodash/fp');
|
const { defaultTo } = require('lodash/fp');
|
||||||
const { add } = require('date-fns');
|
const { add } = require('date-fns');
|
||||||
|
const { ONE_WEEK, getWeeklyCronScheduleAt } = require('@strapi/utils').cron;
|
||||||
const { FOLDER_MODEL_UID, FILE_MODEL_UID } = require('../../constants');
|
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 getMetricsStoreValue = async () => {
|
||||||
const value = await strapi.store.get({ type: 'plugin', name: 'upload', key: 'metrics' });
|
const value = await strapi.store.get({ type: 'plugin', name: 'upload', key: 'metrics' });
|
||||||
|
@ -1,8 +0,0 @@
|
|||||||
'use strict';
|
|
||||||
|
|
||||||
const getWeeklyCronScheduleAt = (date) =>
|
|
||||||
`${date.getSeconds()} ${date.getMinutes()} ${date.getHours()} * * ${date.getDay()}`;
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
getWeeklyCronScheduleAt,
|
|
||||||
};
|
|
@ -1,6 +1,4 @@
|
|||||||
'use strict';
|
import { getWeeklyCronScheduleAt } from '../cron';
|
||||||
|
|
||||||
const { getWeeklyCronScheduleAt } = require('../cron');
|
|
||||||
|
|
||||||
describe('cron', () => {
|
describe('cron', () => {
|
||||||
describe('getWeeklyCronScheduleAt', () => {
|
describe('getWeeklyCronScheduleAt', () => {
|
6
packages/core/utils/src/cron.ts
Normal file
6
packages/core/utils/src/cron.ts
Normal 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 };
|
@ -41,3 +41,4 @@ export * as file from './file';
|
|||||||
export * as traverse from './traverse';
|
export * as traverse from './traverse';
|
||||||
export { default as webhook } from './webhook';
|
export { default as webhook } from './webhook';
|
||||||
export { isOperator, isOperatorOfType } from './operators';
|
export { isOperator, isOperatorOfType } from './operators';
|
||||||
|
export * as cron from './cron';
|
||||||
|
Loading…
x
Reference in New Issue
Block a user