Skip to content
Snippets Groups Projects
Commit c6c05929 authored by Rémi PAILHAREY's avatar Rémi PAILHAREY :fork_knife_plate:
Browse files

fix: randomize cron seconds for triggers

parent 6443703d
No related branches found
No related tags found
2 merge requests!10622.7 Release,!1027Grdf too many requests
......@@ -9,11 +9,20 @@ describe('ConfigService service', () => {
})
})
describe('getCronArgs', () => {
it('should return the args of a cron', () => {
const max = 5
const min = 5
it('should return minimum cron', () => {
const min = 7
const max = 9
jest.spyOn(global.Math, 'random').mockReturnValue(0)
const result = configService.getCronArgs(min, max)
expect(typeof result).toEqual('string')
expect(result).toEqual(`0 0 ${min} * * *`)
})
it('should return maximum cron', () => {
const min = 7
const max = 9
jest.spyOn(global.Math, 'random').mockReturnValue(0.9999)
const result = configService.getCronArgs(min, max)
expect(result).toEqual(`59 59 ${max} * * *`)
})
})
})
......@@ -7,13 +7,14 @@ export default class ConfigService {
}
/**
*
* Generate a cron between min:00:00 and max:59:59
* @param min Minimum hour for cron
* @param max Maximum hour for cron
*/
public getCronArgs(min = 8, max = 9): string {
public getCronArgs(min: number, max: number): string {
const randomHour = Math.floor(Math.random() * (max - min + 1) + min) // NOSONAR
const randomMinutes = Math.floor(Math.random() * 59) // NOSONAR
return `0 ${randomMinutes} ${randomHour} * * *`
const randomMinutes = Math.floor(Math.random() * 60) // NOSONAR
const randomSeconds = Math.floor(Math.random() * 60) // NOSONAR
return `${randomSeconds} ${randomMinutes} ${randomHour} * * *`
}
}
......@@ -25,7 +25,7 @@ export default class TriggerService {
private createTriggerAttributes(account: Account, konnector: Konnector) {
const configService = new ConfigService()
const cronArgs = configService.getCronArgs()
const cronArgs = configService.getCronArgs(7, 9)
const triggerAttributes: TriggerAttributes = buildAttributes({
account: account,
cron: cronArgs,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment