您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

60 行
1.8 KiB

  1. const fs = require('fs-extra');
  2. const { isEmpty } = require('./util');
  3. const config = require('./config');
  4. const debug = require('debug')('server-connect:cron');
  5. exports.start = () => {
  6. if (!config.cron || isEmpty('app/schedule')) return;
  7. debug('Start schedule');
  8. processEntries('app/schedule');
  9. };
  10. function processEntries(path) {
  11. const schedule = require('node-schedule');
  12. const entries = fs.readdirSync(path, { withFileTypes: true });
  13. for (let entry of entries) {
  14. if (entry.isFile() && entry.name.endsWith('.json')) {
  15. try {
  16. const job = fs.readJSONSync(`${path}/${entry.name}`);
  17. const rule = job.settings.options.rule;
  18. debug(`Adding schedule ${entry.name}`);
  19. if (rule == '@reboot') {
  20. setImmediate(exec(job.exec));
  21. } else {
  22. schedule.scheduleJob(rule, exec(job.exec))
  23. }
  24. } catch (e) {
  25. console.error(e);
  26. }
  27. } else if (entry.isDirectory()) {
  28. processEntries(`${path}/${entry.name}`);
  29. }
  30. }
  31. }
  32. function exec(action) {
  33. return async () => {
  34. const App = require('../core/app');
  35. const app = new App({
  36. params: {},
  37. session: {},
  38. cookies: {},
  39. signedCookies: {},
  40. query: {},
  41. headers: {}
  42. }, {
  43. headersSent: false,
  44. set() {},
  45. status() { return this; },
  46. send() { this.headersSent = true; },
  47. json() { this.headersSent = true; },
  48. redirect() { this.headersSent = true; }
  49. });
  50. return app.define(action, true);
  51. }
  52. }