mon-entreprise/server/source/jobs.ts

48 lines
1.0 KiB
TypeScript
Raw Normal View History

2022-10-05 10:56:06 +00:00
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
2022-11-03 16:32:04 +00:00
2023-06-15 13:57:48 +00:00
import later from '@breejs/later'
import Bree, { BreeOptions } from 'bree'
2022-10-05 10:56:06 +00:00
import { NODE_ENV } from './config.js'
const __dirname = dirname(fileURLToPath(import.meta.url))
2022-10-10 08:14:31 +00:00
// Doc for interval : https://breejs.github.io/later/parsers.html#text
2022-10-05 10:56:06 +00:00
const jobs: BreeOptions['jobs'] = [
{
name: 'refresh-token',
interval: 'at 2:00',
},
2023-04-11 09:03:40 +00:00
// {
// name: 'daily-stand-up',
// interval: 'on Monday through Thursday at 16:42',
// },
2023-04-05 10:42:08 +00:00
// {
// name: 'weekly-randomizer',
// interval: 'on Thursday at 16:40',
// },
2022-10-05 10:56:06 +00:00
]
const badJob = jobs.findIndex(
(job) =>
typeof job === 'object' &&
'interval' in job &&
// eslint-disable-next-line
later.parse.text(job.interval).error >= 0
)
if (badJob >= 0) {
throw new Error(`Bad interval in job n°${badJob}`)
}
const bree = new Bree({
root: join(__dirname, 'jobs'),
defaultExtension: NODE_ENV === 'production' ? 'js' : 'ts',
2022-10-06 15:27:03 +00:00
timezone: 'Europe/Paris',
2022-10-05 10:56:06 +00:00
jobs,
})
await bree.start()
2022-10-06 15:07:50 +00:00
export { bree }