-
Notifications
You must be signed in to change notification settings - Fork 11
refactor job policy checker app #151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ea14164
refactor job policy
jsbroks 24da36f
clean up lock
jsbroks bd624bd
clean up docker compose
jsbroks c3cc4d4
Update apps/jobs/src/index.ts
jsbroks 5534903
Update apps/jobs/src/index.ts
jsbroks e7ca90b
fix lint
jsbroks 662117a
fix docs path
jsbroks e95e558
add rabbit config
jsbroks d5e6328
fix running all jobs if args not passed in
jsbroks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
| # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json | ||
| language: en-US | ||
|
|
||
| reviews: | ||
| poem: false |
This file contains hidden or 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 hidden or 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,5 +1,5 @@ | ||
| export default { | ||
| webservice: "Webservice", | ||
| "event-worker": "Event Worker", | ||
| "job-policy-checker": "Job Policy Checker", | ||
| jobs: "Jobs Runner", | ||
| }; |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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
File renamed without changes.
File renamed without changes.
This file contains hidden or 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 hidden or 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,100 @@ | ||
| // Examples: | ||
| // | ||
| // Run specific jobs with default schedules: | ||
| // node index.js -j policy-checker | ||
| // | ||
| // Run jobs with policy-checker running every 5 minutes: | ||
| // node index.js -j "policy-checker=*/5 * * * *" | ||
| // | ||
| // Run all jobs once: | ||
| // node index.js -r | ||
|
|
||
| import { parseArgs } from "node:util"; | ||
| import { CronJob } from "cron"; | ||
| import { z } from "zod"; | ||
|
|
||
| import { logger } from "@ctrlplane/logger"; | ||
|
|
||
| import { run as jobPolicyChecker } from "./policy-checker/index.js"; | ||
|
|
||
| const jobs: Record<string, { run: () => Promise<void>; schedule: string }> = { | ||
| "policy-checker": { | ||
| run: jobPolicyChecker, | ||
| schedule: "* * * * *", // Default: Every minute | ||
| }, | ||
| }; | ||
|
|
||
| const jobSchema = z.object({ | ||
| job: z.array(z.string()).optional(), | ||
| runOnce: z.boolean().optional(), | ||
| }); | ||
|
|
||
| const parseJobArgs = () => { | ||
| const { values } = parseArgs({ | ||
| options: { | ||
| job: { | ||
| type: "string", | ||
| short: "j", | ||
| multiple: true, | ||
| }, | ||
| runOnce: { | ||
| type: "boolean", | ||
| short: "r", | ||
| }, | ||
| }, | ||
| }); | ||
| return jobSchema.parse(values); | ||
| }; | ||
|
|
||
| const getJobConfig = (jobName: string) => { | ||
| const [job, schedule] = jobName.split("="); | ||
| const jobConfig = jobs[job ?? ""]; | ||
jsbroks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (jobConfig == null) | ||
| throw new Error(`Job ${job} not found in configuration`); | ||
| return { job, schedule: schedule ?? jobConfig.schedule }; | ||
| }; | ||
|
|
||
| const parseJobSchedulePairs = (parsedValues: z.infer<typeof jobSchema>) => { | ||
| if (parsedValues.job != null && parsedValues.job.length > 0) | ||
| return parsedValues.job.map(getJobConfig); | ||
|
|
||
| return Object.entries(jobs).map(([job, config]) => ({ | ||
| job, | ||
| schedule: config.schedule, | ||
| })); | ||
| }; | ||
|
|
||
| const runJob = async (job: string) => { | ||
| logger.info(`Running job: ${job}`); | ||
| try { | ||
| await jobs[job]?.run(); | ||
| logger.info(`Job ${job} completed successfully`); | ||
| } catch (error: any) { | ||
| logger.error(`Error running job ${job}: ${error.message}`, error); | ||
| } | ||
| }; | ||
| const main = () => { | ||
| const parsedValues = parseJobArgs(); | ||
| const jobSchedulePairs = parseJobSchedulePairs(parsedValues); | ||
|
|
||
| logger.info( | ||
| `Starting jobs: ${jobSchedulePairs.map((pair) => pair.job).join(", ")}`, | ||
| ); | ||
|
|
||
| for (const { job, schedule } of jobSchedulePairs) { | ||
| if (job == null) continue; | ||
|
|
||
| if (parsedValues.runOnce) { | ||
| logger.info(`Running job ${job} once`); | ||
| runJob(job); | ||
| continue; | ||
| } | ||
| const cronJob = new CronJob(schedule, () => runJob(job)); | ||
|
|
||
| cronJob.start(); | ||
| logger.info(`Scheduled job ${job} with cron: ${schedule}`); | ||
| runJob(job); | ||
| } | ||
| }; | ||
|
|
||
| main(); | ||
This file contains hidden or 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
File renamed without changes.
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.