Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: limit the delay between tasks to 60s #98

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions lib/spark.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,17 @@ export default class Spark {
this.handleRunError(err)
}
const duration = Date.now() - started
const baseDelay = APPROX_ROUND_LENGTH_IN_MS / this.#tasker.maxTasksPerRound
const delay = baseDelay - duration
const delay = calculateDelayBeforeNextTask({
roundLengthInMs: APPROX_ROUND_LENGTH_IN_MS,
maxTasksPerRound: this.#tasker.maxTasksPerRound,
lastTaskDurationInMs: duration
})

if (delay > 0) {
console.log('Sleeping for %s seconds before starting the next task...', Math.round(delay / 1000))
console.log(
'Sleeping for %s seconds before starting the next task...',
Math.round(delay / 1000)
)
await sleep(delay)
console.log() // add an empty line to visually delimit logs from different tasks
}
Expand All @@ -231,6 +238,22 @@ export default class Spark {
}
}

/**
* @param {object} args
* @param {number} args.roundLengthInMs
* @param {number} args.maxTasksPerRound
* @param {number} args.lastTaskDurationInMs
*/
export function calculateDelayBeforeNextTask ({
roundLengthInMs,
maxTasksPerRound,
lastTaskDurationInMs
}) {
const baseDelay = roundLengthInMs / maxTasksPerRound
const delay = baseDelay - lastTaskDurationInMs
return Math.min(delay, 60_000)
}

export function newStats () {
return {
timeout: false,
Expand Down
32 changes: 31 additions & 1 deletion test/spark.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* global Zinnia */

import Spark, { newStats } from '../lib/spark.js'
import Spark, { calculateDelayBeforeNextTask, newStats } from '../lib/spark.js'
import { test } from 'zinnia:test'
import { assertInstanceOf, assertEquals, assertArrayIncludes } from 'zinnia:assert'
import { SPARK_VERSION } from '../lib/constants.js'
Expand Down Expand Up @@ -271,3 +271,33 @@ test('submitRetrieval', async () => {
}
])
})

test('calculateDelayBeforeNextTask() returns value based on average task duration', () => {
const delay = calculateDelayBeforeNextTask({
lastTaskDurationInMs: 3_000,

// one task every 10 seconds (on average)
roundLengthInMs: 60_000,
maxTasksPerRound: 6
})
assertEquals(delay, 7_000)
})

test('calculateDelayBeforeNextTask() handles zero tasks per round', () => {
const delay = calculateDelayBeforeNextTask({
maxTasksPerRound: 0,
// the values below are not important
roundLengthInMs: 12345,
lastTaskDurationInMs: 12
})
assertEquals(delay, 60_000)
})

test('calculateDelayBeforeNextTask() handles one task per round', () => {
const delay = calculateDelayBeforeNextTask({
roundLengthInMs: 20 * 60_000,
maxTasksPerRound: 1,
lastTaskDurationInMs: 1_000
})
assertEquals(delay, 60_000)
})