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

feat: randomize cron seconds for triggers

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