-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(content-releases): Add new scheduling service (#19414)
* feat(content-releases): Add new scheduling service * apply remi's feedback
- Loading branch information
Showing
7 changed files
with
187 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
packages/core/content-releases/server/src/services/__tests__/scheduling.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { scheduleJob } from 'node-schedule'; | ||
import createSchedulingService from '../scheduling'; | ||
|
||
const baseStrapiMock = { | ||
features: { | ||
future: { | ||
isEnabled: jest.fn().mockReturnValue(true), | ||
}, | ||
}, | ||
}; | ||
|
||
jest.mock('node-schedule', () => ({ | ||
scheduleJob: jest.fn(), | ||
})); | ||
|
||
describe('Scheduling service', () => { | ||
describe('set', () => { | ||
it('should throw an error if the release does not exist', async () => { | ||
const strapiMock = { | ||
...baseStrapiMock, | ||
db: { | ||
query: jest.fn(() => ({ | ||
findOne: jest.fn().mockReturnValue(null), | ||
})), | ||
}, | ||
}; | ||
|
||
// @ts-expect-error Ignore missing properties | ||
const schedulingService = createSchedulingService({ strapi: strapiMock }); | ||
expect(() => schedulingService.set('1', new Date())).rejects.toThrow( | ||
'No release found for id 1' | ||
); | ||
}); | ||
|
||
it('should cancel the previous job if it exists and create the new one', async () => { | ||
const mockScheduleJob = jest.fn().mockReturnValue({ cancel: jest.fn() }); | ||
// @ts-expect-error - scheduleJob is a mock | ||
scheduleJob.mockImplementation(mockScheduleJob); | ||
|
||
const strapiMock = { | ||
...baseStrapiMock, | ||
db: { | ||
query: jest.fn(() => ({ | ||
findOne: jest.fn().mockReturnValue({ id: 1 }), | ||
})), | ||
}, | ||
}; | ||
|
||
const oldJobDate = new Date(); | ||
const newJobDate = new Date(oldJobDate.getTime() + 1000); | ||
|
||
// @ts-expect-error Ignore missing properties | ||
const schedulingService = createSchedulingService({ strapi: strapiMock }); | ||
const scheduledJobs = await schedulingService.set('1', oldJobDate); | ||
expect(scheduledJobs.size).toBe(1); | ||
expect(mockScheduleJob).toHaveBeenCalledWith(oldJobDate, expect.any(Function)); | ||
|
||
const oldJob = scheduledJobs.get('1')!; | ||
|
||
await schedulingService.set('1', newJobDate); | ||
|
||
expect(oldJob.cancel).toHaveBeenCalled(); | ||
expect(mockScheduleJob).toHaveBeenCalledWith(newJobDate, expect.any(Function)); | ||
}); | ||
|
||
it('should create a new job', async () => { | ||
const mockScheduleJob = jest.fn().mockReturnValue({ cancel: jest.fn() }); | ||
// @ts-expect-error - scheduleJob is a mock | ||
scheduleJob.mockImplementation(mockScheduleJob); | ||
|
||
const strapiMock = { | ||
...baseStrapiMock, | ||
db: { | ||
query: jest.fn(() => ({ | ||
findOne: jest.fn().mockReturnValue({ id: 1 }), | ||
})), | ||
}, | ||
}; | ||
|
||
const date = new Date(); | ||
|
||
// @ts-expect-error Ignore missing properties | ||
const schedulingService = createSchedulingService({ strapi: strapiMock }); | ||
const scheduledJobs = await schedulingService.set('1', date); | ||
expect(scheduledJobs.size).toBe(1); | ||
expect(mockScheduleJob).toHaveBeenCalledWith(date, expect.any(Function)); | ||
}); | ||
}); | ||
|
||
describe('cancel', () => { | ||
it('should cancel the job if it exists', async () => { | ||
const mockScheduleJob = jest.fn().mockReturnValue({ cancel: jest.fn() }); | ||
// @ts-expect-error - scheduleJob is a mock | ||
scheduleJob.mockImplementation(mockScheduleJob); | ||
|
||
const strapiMock = { | ||
...baseStrapiMock, | ||
db: { | ||
query: jest.fn(() => ({ | ||
findOne: jest.fn().mockReturnValue({ id: 1 }), | ||
})), | ||
}, | ||
}; | ||
|
||
const date = new Date(); | ||
|
||
// @ts-expect-error Ignore missing properties | ||
const schedulingService = createSchedulingService({ strapi: strapiMock }); | ||
const scheduledJobs = await schedulingService.set('1', date); | ||
expect(scheduledJobs.size).toBe(1); | ||
expect(mockScheduleJob).toHaveBeenCalledWith(date, expect.any(Function)); | ||
|
||
schedulingService.cancel('1'); | ||
expect(scheduledJobs.size).toBe(0); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import release from './release'; | ||
import releaseValidation from './validation'; | ||
import scheduling from './scheduling'; | ||
|
||
export const services = { | ||
release, | ||
'release-validation': releaseValidation, | ||
...(strapi.features.future.isEnabled('contentReleasesScheduling') ? { scheduling } : {}), | ||
}; |
53 changes: 53 additions & 0 deletions
53
packages/core/content-releases/server/src/services/scheduling.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { scheduleJob, Job } from 'node-schedule'; | ||
import { LoadedStrapi } from '@strapi/types'; | ||
|
||
import { errors } from '@strapi/utils'; | ||
import { Release } from '../../../shared/contracts/releases'; | ||
import { getService } from '../utils'; | ||
import { RELEASE_MODEL_UID } from '../constants'; | ||
|
||
const createSchedulingService = ({ strapi }: { strapi: LoadedStrapi }) => { | ||
const scheduledJobs = new Map<Release['id'], Job>(); | ||
|
||
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 job = scheduleJob(scheduleDate, async () => { | ||
try { | ||
await getService('release').publish(releaseId); | ||
// @TODO: Trigger webhook with success message | ||
} catch (error) { | ||
// @TODO: Trigger webhook with error message | ||
} | ||
|
||
this.cancel(releaseId); | ||
}); | ||
|
||
if (scheduledJobs.has(releaseId)) { | ||
this.cancel(releaseId); | ||
} | ||
|
||
scheduledJobs.set(releaseId, job); | ||
|
||
return scheduledJobs; | ||
}, | ||
|
||
cancel(releaseId: Release['id']) { | ||
if (scheduledJobs.has(releaseId)) { | ||
scheduledJobs.get(releaseId)!.cancel(); | ||
scheduledJobs.delete(releaseId); | ||
} | ||
|
||
return scheduledJobs; | ||
}, | ||
}; | ||
}; | ||
|
||
export default createSchedulingService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters