Repository: strapi/strapi
File: packages/core/content-releases/server/src/services/scheduling.ts (TypeScript)

Code before:

  return {
    async set(releaseId: Release['id'], scheduleDate: Date) {
      const release = await strapi.db
        .query(RELEASE_MODEL_UID)
        .findOne({ where: { id: releaseId, releasedAt: null } });

      if (!release) {
        throw new errors.NotFoundError(`No release found for id ${releaseId}`);
      }

      const task = {
        async task() {
          try {
            await getService('release', { strapi }).publish(releaseId);
            // @TODO: Trigger webhook with success message
          } catch (error) {
            // @TODO: Trigger webhook with error message
          }
        },
        options: scheduleDate,
      };

      const tasks: { [key: string]: typeof task } = {};
      const taskName = `publishRelease_${releaseId}`;
      tasks[taskName] = task;

      strapi.cron.add(tasks);

      if (scheduledJobs.has(releaseId)) {
        this.cancel(releaseId);
      }

      scheduledJobs.set(releaseId, taskName);

      return scheduledJobs;
    },

    cancel(releaseId: Release['id']) {
      if (scheduledJobs.has(releaseId)) {
        strapi.cron.remove(scheduledJobs.get(releaseId)!);
        scheduledJobs.delete(releaseId);
      }

      return scheduledJobs;
    },

    getAll() {
      return scheduledJobs;
    },


Review comment (suggestion):
Just a style thing but I feel like this could be a bit more concise. Something like:

```suggestion
 const taskName = `publishRelease_${releaseId}`;
  const tasks = {
    [taskName]: {
      async task() {
        try {
          await getService('release', { strapi }).publish(releaseId);
          // @TODO: Trigger webhook with success message
        } catch (error) {
          // @TODO: Trigger webhook with error message
        }
      },
      options: scheduleDate,
    },
  };
```


Code after:

  return {
    async set(releaseId: Release['id'], scheduleDate: Date) {
      const release = await strapi.db
        .query(RELEASE_MODEL_UID)
        .findOne({ where: { id: releaseId, releasedAt: null } });

      if (!release) {
        throw new errors.NotFoundError(`No release found for id ${releaseId}`);
      }

      const taskName = `publishRelease_${releaseId}`;

      strapi.cron.add({
        [taskName]: {
          async task() {
            try {
              await getService('release', { strapi }).publish(releaseId);
              // @TODO: Trigger webhook with success message
            } catch (error) {
              // @TODO: Trigger webhook with error message
            }
          },
          options: scheduleDate,
        },
      });

      if (scheduledJobs.has(releaseId)) {
        this.cancel(releaseId);
      }

      scheduledJobs.set(releaseId, taskName);

      return scheduledJobs;
    },

    cancel(releaseId: Release['id']) {
      if (scheduledJobs.has(releaseId)) {
        strapi.cron.remove(scheduledJobs.get(releaseId)!);
        scheduledJobs.delete(releaseId);
      }

      return scheduledJobs;
    },

    getAll() {
      return scheduledJobs;
    },

    /**
     * On bootstrap, we can use this function to make sure to sync the scheduled jobs from the database that are not yet released