-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(orchestrator): add support for schedules (#2260)
Based on #2256. Review this one first 🙏 This PR is adding model and logic to create schedules in the orchestrator. In a following PR I will add a scheduler worker_thread that will create tasks based on exisiting schedules when necessary ## Checklist before requesting a review (skip if just adding/editing APIs & templates) - [x] I added tests, otherwise the reason is: - [ ] I added observability, otherwise the reason is: - [ ] I added analytics, otherwise the reason is: --------- Co-authored-by: Samuel Bodin <[email protected]>
- Loading branch information
1 parent
5e75624
commit ad420f8
Showing
16 changed files
with
628 additions
and
124 deletions.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { z } from 'zod'; | ||
import type { JsonValue } from 'type-fest'; | ||
import type { Scheduler } from '@nangohq/scheduler'; | ||
import type { ApiError, Endpoint } from '@nangohq/types'; | ||
import type { EndpointRequest, EndpointResponse, RouteHandler, Route } from '@nangohq/utils'; | ||
import { validateRequest } from '@nangohq/utils'; | ||
import { syncArgsSchema } from '../../clients/validate.js'; | ||
|
||
const path = '/v1/recurring'; | ||
const method = 'POST'; | ||
|
||
export type PostRecurring = Endpoint<{ | ||
Method: typeof method; | ||
Path: typeof path; | ||
Body: { | ||
name: string; | ||
startsAt: Date; | ||
frequencyMs: number; | ||
args: JsonValue; | ||
}; | ||
Error: ApiError<'recurring_failed'>; | ||
Success: { scheduleId: string }; | ||
}>; | ||
|
||
const validate = validateRequest<PostRecurring>({ | ||
parseBody: (data: any) => { | ||
return z | ||
.object({ | ||
name: z.string().min(1), | ||
startsAt: z.coerce.date(), | ||
frequencyMs: z.number().int().positive(), | ||
args: syncArgsSchema | ||
}) | ||
.parse(data); | ||
} | ||
}); | ||
|
||
const handler = (scheduler: Scheduler) => { | ||
return async (req: EndpointRequest<PostRecurring>, res: EndpointResponse<PostRecurring>) => { | ||
const schedule = await scheduler.recurring({ | ||
name: req.body.name, | ||
payload: req.body.args, | ||
startsAt: req.body.startsAt, | ||
frequencyMs: req.body.frequencyMs | ||
}); | ||
if (schedule.isErr()) { | ||
return res.status(500).json({ error: { code: 'recurring_failed', message: schedule.error.message } }); | ||
} | ||
return res.status(201).json({ scheduleId: schedule.value.id }); | ||
}; | ||
}; | ||
|
||
export const route: Route<PostRecurring> = { path, method }; | ||
|
||
export const routeHandler = (scheduler: Scheduler): RouteHandler<PostRecurring> => { | ||
return { | ||
...route, | ||
validate, | ||
handler: handler(scheduler) | ||
}; | ||
}; |
Oops, something went wrong.