15 lines
587 B
TypeScript
15 lines
587 B
TypeScript
|
|
/**
|
||
|
|
* Convert a polling interval in seconds into a cron expression.
|
||
|
|
* Pure and side-effect free so it can be unit-tested without touching the DB.
|
||
|
|
* - < 60s → every minute ("* * * * *")
|
||
|
|
* - < 60 minutes → every N minutes ("*\/15 * * * *")
|
||
|
|
* - otherwise → every N hours ("0 *\/6 * * *")
|
||
|
|
*/
|
||
|
|
export function secondsToCron(seconds: number): string {
|
||
|
|
if (seconds < 60) return '* * * * *'
|
||
|
|
const minutes = Math.round(seconds / 60)
|
||
|
|
if (minutes < 60) return `*/${minutes} * * * *`
|
||
|
|
const hours = Math.round(minutes / 60)
|
||
|
|
return `0 */${hours} * * *`
|
||
|
|
}
|