Skip to content

Commit c3b5f73

Browse files
committed
fix: limit the delay between tasks to 60s
Ensure the checker prints some message at least once per minute, to prevent Station Core for considering it as stuck. As a side effect, this also fixes the problem when maxTasksPerRound is zero, the delay is calculated as Infinity and we call `setTimeout(Infinity)`. Such call is a no-op in Deno, which causes the checker to loop very quickly and pollute the logs with too many "Completed all tasks for the current round." messages. After this change, the message is print once per minute. Signed-off-by: Miroslav Bajtoš <oss@bajtos.net>
1 parent 8214ca9 commit c3b5f73

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

lib/spark.js

+26-3
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,17 @@ export default class Spark {
211211
this.handleRunError(err)
212212
}
213213
const duration = Date.now() - started
214-
const baseDelay = APPROX_ROUND_LENGTH_IN_MS / this.#tasker.maxTasksPerRound
215-
const delay = baseDelay - duration
214+
const delay = calculateDelayBeforeNextTask({
215+
roundLengthInMs: APPROX_ROUND_LENGTH_IN_MS,
216+
maxTasksPerRound: this.#tasker.maxTasksPerRound,
217+
lastTaskDurationInMs: duration
218+
})
219+
216220
if (delay > 0) {
217-
console.log('Sleeping for %s seconds before starting the next task...', Math.round(delay / 1000))
221+
console.log(
222+
'Sleeping for %s seconds before starting the next task...',
223+
Math.round(delay / 1000)
224+
)
218225
await sleep(delay)
219226
console.log() // add an empty line to visually delimit logs from different tasks
220227
}
@@ -231,6 +238,22 @@ export default class Spark {
231238
}
232239
}
233240

241+
/**
242+
* @param {object} args
243+
* @param {number} args.roundLengthInMs
244+
* @param {number} args.maxTasksPerRound
245+
* @param {number} args.lastTaskDurationInMs
246+
*/
247+
export function calculateDelayBeforeNextTask ({
248+
roundLengthInMs,
249+
maxTasksPerRound,
250+
lastTaskDurationInMs
251+
}) {
252+
const baseDelay = roundLengthInMs / maxTasksPerRound
253+
const delay = baseDelay - lastTaskDurationInMs
254+
return Math.min(delay, 60_000)
255+
}
256+
234257
export function newStats () {
235258
return {
236259
timeout: false,

test/spark.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* global Zinnia */
22

3-
import Spark, { newStats } from '../lib/spark.js'
3+
import Spark, { calculateDelayBeforeNextTask, newStats } from '../lib/spark.js'
44
import { test } from 'zinnia:test'
55
import { assertInstanceOf, assertEquals, assertArrayIncludes } from 'zinnia:assert'
66
import { SPARK_VERSION } from '../lib/constants.js'
@@ -271,3 +271,33 @@ test('submitRetrieval', async () => {
271271
}
272272
])
273273
})
274+
275+
test('calculateDelayBeforeNextTask() returns value based on average task duration', () => {
276+
const delay = calculateDelayBeforeNextTask({
277+
lastTaskDurationInMs: 3_000,
278+
279+
// one task every 10 seconds (on average)
280+
roundLengthInMs: 60_000,
281+
maxTasksPerRound: 6
282+
})
283+
assertEquals(delay, 7_000)
284+
})
285+
286+
test('calculateDelayBeforeNextTask() handles zero tasks per round', () => {
287+
const delay = calculateDelayBeforeNextTask({
288+
maxTasksPerRound: 0,
289+
// the values below are not important
290+
roundLengthInMs: 12345,
291+
lastTaskDurationInMs: 12
292+
})
293+
assertEquals(delay, 60_000)
294+
})
295+
296+
test('calculateDelayBeforeNextTask() handles one task per round', () => {
297+
const delay = calculateDelayBeforeNextTask({
298+
roundLengthInMs: 20 * 60_000,
299+
maxTasksPerRound: 1,
300+
lastTaskDurationInMs: 1_000
301+
})
302+
assertEquals(delay, 60_000)
303+
})

0 commit comments

Comments
 (0)