|
3 | 3 | */ |
4 | 4 |
|
5 | 5 | import { factories } from '@strapi/strapi'; |
| 6 | +import getPluginService from '../utils/getPluginService'; |
| 7 | +import logMessage from '../utils/logMessage'; |
6 | 8 |
|
7 | | -export default factories.createCoreService('plugin::publisher.action'); |
| 9 | +export default factories.createCoreService('plugin::publisher.action', ({ strapi }) => ({ |
| 10 | + /** |
| 11 | + * Schedule a cron job for a specific action |
| 12 | + */ |
| 13 | + scheduleCronJob(action) { |
| 14 | + const taskName = `publisherAction_${action.documentId}`; |
| 15 | + const executeAt = new Date(action.executeAt); |
| 16 | + |
| 17 | + // Check if the execution time is in the future |
| 18 | + if (executeAt <= new Date()) { |
| 19 | + strapi.log.warn(logMessage(`Action ${action.documentId} has an executeAt time in the past, skipping cron job creation.`)); |
| 20 | + return; |
| 21 | + } |
| 22 | + |
| 23 | + strapi.log.info(logMessage(`Scheduling cron job for action ${action.documentId} at ${executeAt.toISOString()}`)); |
| 24 | + |
| 25 | + strapi.cron.add({ |
| 26 | + [taskName]: { |
| 27 | + async task() { |
| 28 | + try { |
| 29 | + // Fetch the action again to ensure it still exists |
| 30 | + const currentAction = await strapi.documents('plugin::publisher.action').findOne({ |
| 31 | + documentId: action.documentId, |
| 32 | + }); |
| 33 | + |
| 34 | + if (!currentAction) { |
| 35 | + strapi.log.warn(logMessage(`Action ${action.documentId} no longer exists, skipping execution.`)); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + // Execute the publication action |
| 40 | + await getPluginService('publicationService').toggle(currentAction, currentAction.mode); |
| 41 | + |
| 42 | + strapi.log.info(logMessage(`Successfully executed action ${action.documentId}`)); |
| 43 | + } catch (error) { |
| 44 | + strapi.log.error(logMessage(`Error executing action ${action.documentId}: ${error.message}`)); |
| 45 | + } finally { |
| 46 | + // Remove the cron job after execution |
| 47 | + strapi.cron.remove(taskName); |
| 48 | + } |
| 49 | + }, |
| 50 | + options: executeAt, |
| 51 | + }, |
| 52 | + }); |
| 53 | + }, |
| 54 | + |
| 55 | + /** |
| 56 | + * Remove a scheduled cron job |
| 57 | + */ |
| 58 | + removeCronJob(actionDocumentId) { |
| 59 | + const taskName = `publisherAction_${actionDocumentId}`; |
| 60 | + |
| 61 | + try { |
| 62 | + strapi.cron.remove(taskName); |
| 63 | + strapi.log.info(logMessage(`Removed cron job for action ${actionDocumentId}`)); |
| 64 | + } catch (error) { |
| 65 | + strapi.log.warn(logMessage(`Could not remove cron job ${taskName}: ${error.message}`)); |
| 66 | + } |
| 67 | + }, |
| 68 | + |
| 69 | + /** |
| 70 | + * Override create to schedule cron job |
| 71 | + */ |
| 72 | + async create(...args) { |
| 73 | + const result = await super.create(...args); |
| 74 | + |
| 75 | + // Schedule the cron job for this action |
| 76 | + this.scheduleCronJob(result); |
| 77 | + |
| 78 | + return result; |
| 79 | + }, |
| 80 | + |
| 81 | + /** |
| 82 | + * Override update to reschedule cron job |
| 83 | + */ |
| 84 | + async update(documentId, ...args) { |
| 85 | + // Remove the old cron job |
| 86 | + this.removeCronJob(documentId); |
| 87 | + |
| 88 | + const result = await super.update(documentId, ...args); |
| 89 | + |
| 90 | + // Schedule a new cron job with updated data |
| 91 | + this.scheduleCronJob(result); |
| 92 | + |
| 93 | + return result; |
| 94 | + }, |
| 95 | + |
| 96 | + /** |
| 97 | + * Override delete to remove cron job |
| 98 | + */ |
| 99 | + async delete(documentId, ...args) { |
| 100 | + // Remove the cron job before deleting |
| 101 | + this.removeCronJob(documentId); |
| 102 | + |
| 103 | + const result = await super.delete(documentId, ...args); |
| 104 | + |
| 105 | + return result; |
| 106 | + }, |
| 107 | +})); |
0 commit comments