mon-entreprise/server/source/jobs.ts

47 lines
1013 B
TypeScript
Raw Normal View History

2022-10-05 10:56:06 +00:00
import later from '@breejs/later'
import Bree, { BreeOptions } from 'bree'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
2022-11-03 16:32:04 +00:00
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',
},
{
name: 'daily-stand-up',
2022-10-10 15:25:14 +00:00
interval: 'on Monday through Thursday at 16:42',
2022-10-05 10:56:06 +00:00
},
{
name: 'weekly-randomizer',
2022-10-13 15:22:28 +00:00
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 }