-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
118 additions
and
36 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 was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,104 @@ | ||
// 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()), | ||
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 ?? ""]; | ||
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.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) { | ||
logger.error(`Error running job ${job}:`, error); | ||
} | ||
}; | ||
|
||
const main = () => { | ||
const parsedValues = parseJobArgs(); | ||
const jobSchedulePairs = parseJobSchedulePairs(parsedValues); | ||
|
||
if (jobSchedulePairs.length === 0) { | ||
logger.info("No jobs specified to run."); | ||
return; | ||
} | ||
|
||
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 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.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.