mirror of
https://github.com/strapi/strapi.git
synced 2025-11-16 18:19:34 +00:00
refactor strapi.cron
This commit is contained in:
parent
b78af34ebf
commit
bac1080f13
@ -376,6 +376,11 @@ class Strapi {
|
||||
entityValidator: this.entityValidator,
|
||||
});
|
||||
|
||||
if (strapi.config.get('server.cron.enabled', true)) {
|
||||
const cronTasks = this.config.get('server.cron.tasks', {});
|
||||
this.cron.add(cronTasks);
|
||||
}
|
||||
|
||||
this.telemetry.bootstrap();
|
||||
|
||||
let oldContentTypes;
|
||||
@ -413,11 +418,7 @@ class Strapi {
|
||||
|
||||
await this.runLifecyclesFunctions(LIFECYCLES.BOOTSTRAP);
|
||||
|
||||
if (strapi.config.get('server.cron.enabled', true)) {
|
||||
const cronTasks = this.config.get('server.cron.tasks', {});
|
||||
this.cron.add(cronTasks, 'user');
|
||||
this.cron.start('user');
|
||||
}
|
||||
this.cron.start();
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -1,18 +1,14 @@
|
||||
'use strict';
|
||||
|
||||
const { Job } = require('node-schedule');
|
||||
const { isFunction, forEach } = require('lodash/fp');
|
||||
const { isFunction } = require('lodash/fp');
|
||||
|
||||
const createCronService = () => {
|
||||
let jobsSpecsMap = {};
|
||||
let jobsSpecs = [];
|
||||
let running = false;
|
||||
|
||||
return {
|
||||
add(tasks = {}, namespace) {
|
||||
if (!namespace) {
|
||||
throw new Error('Tasks should be attached to a namespace.');
|
||||
}
|
||||
jobsSpecsMap[namespace] = jobsSpecsMap[namespace] || [];
|
||||
|
||||
add(tasks = {}) {
|
||||
for (const taskExpression in tasks) {
|
||||
const taskValue = tasks[taskExpression];
|
||||
|
||||
@ -33,42 +29,27 @@ const createCronService = () => {
|
||||
const fnWithStrapi = (...args) => fn({ strapi }, ...args);
|
||||
|
||||
const job = new Job(null, fnWithStrapi);
|
||||
jobsSpecsMap[namespace].push({ job, options });
|
||||
jobsSpecs.push({ job, options });
|
||||
|
||||
if (running) {
|
||||
job.schedule(options);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
},
|
||||
|
||||
start(namespace) {
|
||||
if (namespace && !jobsSpecsMap[namespace]) {
|
||||
throw new Error('namespace not found');
|
||||
}
|
||||
|
||||
if (!namespace) {
|
||||
forEach(jobs => jobs.forEach(({ job, options }) => job.schedule(options)))(jobsSpecsMap);
|
||||
return this;
|
||||
}
|
||||
|
||||
jobsSpecsMap[namespace].forEach(({ job, options }) => job.schedule(options));
|
||||
start() {
|
||||
jobsSpecs.forEach(({ job, options }) => job.schedule(options));
|
||||
running = true;
|
||||
return this;
|
||||
},
|
||||
|
||||
stop(namespace) {
|
||||
if (namespace && !jobsSpecsMap[namespace]) {
|
||||
throw new Error('namespace not found');
|
||||
}
|
||||
|
||||
if (!namespace) {
|
||||
forEach(jobs => jobs.forEach(({ job }) => job.cancel()))(jobsSpecsMap);
|
||||
return this;
|
||||
}
|
||||
|
||||
jobsSpecsMap[namespace].forEach(({ job }) => job.cancel());
|
||||
stop() {
|
||||
jobsSpecs.forEach(({ job }) => job.cancel());
|
||||
running = false;
|
||||
return this;
|
||||
},
|
||||
|
||||
destroy() {
|
||||
this.stop();
|
||||
jobsSpecsMap = {};
|
||||
jobsSpecs = [];
|
||||
return this;
|
||||
},
|
||||
};
|
||||
|
||||
@ -38,7 +38,7 @@ describe('Upload plugin bootstrap function', () => {
|
||||
upload: {
|
||||
services: {
|
||||
metrics: {
|
||||
startRegularMetricsUpdate() {},
|
||||
registerCron() {},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
2
packages/core/upload/server/bootstrap.js
vendored
2
packages/core/upload/server/bootstrap.js
vendored
@ -21,7 +21,7 @@ module.exports = async ({ strapi }) => {
|
||||
|
||||
await registerPermissionActions();
|
||||
|
||||
getService('metrics').startRegularMetricsUpdate();
|
||||
getService('metrics').registerCron();
|
||||
};
|
||||
|
||||
const registerPermissionActions = async () => {
|
||||
|
||||
@ -1,7 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
const { getService } = require('./utils');
|
||||
|
||||
module.exports = () => {
|
||||
getService('metrics').stopRegularMetricsUpdate();
|
||||
};
|
||||
@ -2,7 +2,6 @@
|
||||
|
||||
const register = require('./register');
|
||||
const bootstrap = require('./bootstrap');
|
||||
const destroy = require('./destroy');
|
||||
const contentTypes = require('./content-types');
|
||||
const services = require('./services');
|
||||
const routes = require('./routes');
|
||||
@ -13,7 +12,6 @@ module.exports = () => {
|
||||
return {
|
||||
register,
|
||||
bootstrap,
|
||||
destroy,
|
||||
config,
|
||||
routes,
|
||||
controllers,
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
const metricsService = require('../metrics');
|
||||
|
||||
describe('metrics', () => {
|
||||
describe('computeWeeklyMetrics', () => {
|
||||
describe('computeCronMetrics', () => {
|
||||
test.each([
|
||||
[[], 0, [0, 0, 0, 0, 0]],
|
||||
[
|
||||
@ -54,9 +54,9 @@ describe('metrics', () => {
|
||||
};
|
||||
strapi.db.connection.raw = raw;
|
||||
|
||||
const { computeWeeklyMetrics } = metricsService({ strapi });
|
||||
const { computeCronMetrics } = metricsService({ strapi });
|
||||
|
||||
const results = await computeWeeklyMetrics();
|
||||
const results = await computeCronMetrics();
|
||||
const [
|
||||
assetNumber,
|
||||
maxDepth,
|
||||
|
||||
@ -5,11 +5,8 @@ const { FOLDER_MODEL_UID, FILE_MODEL_UID } = require('../constants');
|
||||
const rand = max => Math.floor(Math.random() * max);
|
||||
const getCronRandomWeekly = () => `${rand(60)} ${rand(60)} ${rand(24)} * * ${rand(7)}`;
|
||||
|
||||
module.exports = ({ strapi }) => {
|
||||
let running = false;
|
||||
|
||||
return {
|
||||
async computeWeeklyMetrics() {
|
||||
module.exports = ({ strapi }) => ({
|
||||
async computeCronMetrics() {
|
||||
// Folder metrics
|
||||
const pathColName = strapi.db.metadata.get(FOLDER_MODEL_UID).attributes.path.columnName;
|
||||
const folderTable = strapi.getModel(FOLDER_MODEL_UID).collectionName;
|
||||
@ -84,28 +81,12 @@ module.exports = ({ strapi }) => {
|
||||
};
|
||||
},
|
||||
|
||||
async startRegularMetricsUpdate() {
|
||||
if (running) {
|
||||
throw new Error('Regular metrics updates are already running');
|
||||
}
|
||||
running = true;
|
||||
|
||||
strapi.cron.add(
|
||||
{
|
||||
async registerCron() {
|
||||
strapi.cron.add({
|
||||
[getCronRandomWeekly()]: async ({ strapi }) => {
|
||||
const metrics = await this.computeWeeklyMetrics();
|
||||
const metrics = await this.computeCronMetrics();
|
||||
strapi.telemetry.send('didSendUploadPropertiesOnceAWeek', metrics);
|
||||
},
|
||||
});
|
||||
},
|
||||
'upload.weekly'
|
||||
);
|
||||
|
||||
strapi.cron.start('upload.weekly');
|
||||
},
|
||||
|
||||
stopRegularMetricsUpdate() {
|
||||
strapi.cron.stop('upload.weekly');
|
||||
running = false;
|
||||
},
|
||||
};
|
||||
};
|
||||
});
|
||||
|
||||
@ -15720,14 +15720,6 @@ jest-message-util@^28.1.1:
|
||||
slash "^3.0.0"
|
||||
stack-utils "^2.0.3"
|
||||
|
||||
jest-mock@^26.6.2:
|
||||
version "26.6.2"
|
||||
resolved "https://registry.yarnpkg.com/jest-mock/-/jest-mock-26.6.2.tgz#d6cb712b041ed47fe0d9b6fc3474bc6543feb302"
|
||||
integrity sha512-YyFjePHHp1LzpzYcmgqkJ0nm0gg/lJx2aZFzFy1S6eUqNjXsOqTK10zNRff2dNfssgokjkG65OlWNcIlgd3zew==
|
||||
dependencies:
|
||||
"@jest/types" "^26.6.2"
|
||||
"@types/node" "*"
|
||||
|
||||
jest-mock@^27.0.6:
|
||||
version "27.5.1"
|
||||
resolved "https://registry.yarnpkg.com/jest-mock/-/jest-mock-27.5.1.tgz#19948336d49ef4d9c52021d34ac7b5f36ff967d6"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user