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

feat: honour maxTasksPerRound from spark-api #47

Merged
merged 5 commits into from
Dec 13, 2023
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
2 changes: 1 addition & 1 deletion lib/constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const SPARK_VERSION = '1.6.0'
export const DELAY_BETWEEN_RETRIEVALS = 10_000
export const MAX_CAR_SIZE = 200 * 1024 * 1024 // 200 MB
export const APPROX_ROUND_LENGTH_IN_MS = 60 * 60_000 // 1 hour
bajtos marked this conversation as resolved.
Show resolved Hide resolved
16 changes: 13 additions & 3 deletions lib/spark.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/* global Zinnia */

import { ActivityState } from './activity-state.js'
import { SPARK_VERSION, DELAY_BETWEEN_RETRIEVALS, MAX_CAR_SIZE } from './constants.js'
import { SPARK_VERSION, MAX_CAR_SIZE, APPROX_ROUND_LENGTH_IN_MS } from './constants.js'
import { encodeHex } from './deno-encoding-hex.js'

const sleep = dt => new Promise(resolve => setTimeout(resolve, dt))

export default class Spark {
#fetch
#activity = new ActivityState()
#maxTasksPerNode = 360
juliangruber marked this conversation as resolved.
Show resolved Hide resolved

constructor ({ fetch = globalThis.fetch } = {}) {
this.#fetch = fetch
Expand All @@ -23,9 +24,11 @@ export default class Spark {
})
await assertOkResponse(res, 'Failed to fetch the current SPARK round')
this.#activity.onHealthy()
const { retrievalTasks, ...round } = await res.json()
const { retrievalTasks, maxTasksPerNode, ...round } = await res.json()
console.log('Current SPARK round:', round)
console.log(' %s max tasks per node', maxTasksPerNode ?? '<n/a>')
console.log(' %s retrieval tasks', retrievalTasks.length)
if (maxTasksPerNode) this.#maxTasksPerNode = maxTasksPerNode

const retrieval = retrievalTasks[Math.floor(Math.random() * retrievalTasks.length)]
console.log({ retrieval })
Expand Down Expand Up @@ -161,6 +164,7 @@ export default class Spark {

async run () {
while (true) {
const started = Date.now()
try {
await this.nextRetrieval()
this.#activity.onHealthy()
Expand All @@ -172,7 +176,13 @@ export default class Spark {
}
console.error(err)
}
await sleep(DELAY_BETWEEN_RETRIEVALS)
const duration = Date.now() - started
const baseDelay = APPROX_ROUND_LENGTH_IN_MS / this.#maxTasksPerNode
juliangruber marked this conversation as resolved.
Show resolved Hide resolved
const delay = baseDelay - duration
if (delay > 0) {
console.log('Sleeping for %s seconds before starting the next task...', Math.round(delay / 1000))
await sleep(delay)
}
}
}
}
Expand Down