Spaces:
Sleeping
Sleeping
Download lib/scheduler.js from Ethscriptions/cut: direct link, hf CLI and curl.
- Browser
- Download file 3.52 kB
-
https://huggingface.co/spaces/Ethscriptions/cut/resolve/main/lib/scheduler.js
- Command line
-
hf download hf://spaces/Ethscriptions/cut/lib/scheduler.js
-
curl -L -o scheduler.js https://huggingface.co/spaces/Ethscriptions/cut/resolve/main/lib/scheduler.js
3.52 kB
| /** | |
| * All background work, as independent loops. A loop never overlaps itself and | |
| * never blocks another loop (the old version queued everything behind one | |
| * lock, so a slow X scan delayed Telegram and seat checks). | |
| */ | |
| const log = require('./log') | |
| const invites = require('./invites') | |
| const alerts = require('./alerts') | |
| const xscan = require('./x/scan') | |
| const tg = require('./tg') | |
| const pipeline = require('./pipeline') | |
| const loops = {} | |
| function every(name, ms, fn, initialDelay = 5000) { | |
| const loop = (loops[name] = { name, everyMs: ms, running: false, lastStart: null, lastEnd: null }) | |
| const tick = async () => { | |
| if (loop.running) return | |
| loop.running = true | |
| loop.lastStart = log.now() | |
| try { | |
| await fn() | |
| } catch (err) { | |
| log.fail(name, err) | |
| } finally { | |
| loop.running = false | |
| loop.lastEnd = log.now() | |
| } | |
| } | |
| setTimeout(() => { | |
| tick() | |
| setInterval(tick, ms) | |
| }, initialDelay) | |
| } | |
| /** Free Spaces sleep after 48 h without visitors; a request through the public URL counts as one. */ | |
| async function keepAlive() { | |
| const host = process.env.SPACE_HOST | |
| if (!host) return | |
| await fetch(`https://${host}/api/ping?from=self`, { signal: AbortSignal.timeout(20000) }).catch(() => {}) | |
| } | |
| function start() { | |
| every('invites', 15000, () => invites.checkDue(40), 4000) | |
| every('push', 60000, () => alerts.pushPending(), 8000) | |
| every('x-search', 60000, () => xscan.searchTick(), 12000) | |
| every('x-profiles', 180000, () => xscan.profileTick(), 40000) | |
| every('tg-watchdog', 60000, () => tg.watchdog(), 60000) | |
| every('tg-poll', 30000, () => tg.pollDialogs(), 60000) | |
| every('tg-catchup', 20000, () => tg.catchup(), 70000) | |
| every('tg-global', 180000, () => tg.globalSearch(), 130000) | |
| every('keepalive', 20 * 60000, keepAlive, 60000) | |
| tg.start().catch((err) => log.fail('telegram', err)) | |
| } | |
| let scanning = null | |
| let lastScan = null | |
| /** The "抓取最新" button: run every source once, right now. */ | |
| function scanNow() { | |
| if (scanning) return scanning | |
| if (lastScan && Date.now() - lastScan.finishedMs < 20000) return Promise.resolve({ ...lastScan, cached: true }) | |
| scanning = (async () => { | |
| const started = Date.now() | |
| const before = { posts: pipeline.stats.newPosts, codes: pipeline.stats.newCodes } | |
| const [xs, xp, tc, tp, tgl] = await Promise.all([ | |
| xscan.searchTick({ all: true }).catch((err) => ({ error: err.message })), | |
| xscan.profileTick({ count: 8 }).catch((err) => ({ error: err.message })), | |
| tg.catchup().catch((err) => ({ error: err.message })), | |
| tg.pollDialogs().catch((err) => ({ error: err.message })), | |
| tg.globalSearch().catch((err) => ({ error: err.message })), | |
| ]) | |
| const inv = await invites.checkDue(80).catch((err) => ({ error: err.message })) | |
| const push = await alerts.pushPending().catch((err) => ({ error: err.message })) | |
| lastScan = { | |
| finishedMs: Date.now(), | |
| seconds: Math.round((Date.now() - started) / 100) / 10, | |
| newPosts: pipeline.stats.newPosts - before.posts, | |
| newCodes: pipeline.stats.newCodes - before.codes, | |
| x: xs, | |
| profiles: xp, | |
| telegram: tc, | |
| telegramPoll: tp, | |
| telegramSearch: tgl, | |
| invites: inv, | |
| pushed: push.pushed || [], | |
| } | |
| return lastScan | |
| })().finally(() => (scanning = null)) | |
| return scanning | |
| } | |
| function status() { | |
| return Object.values(loops).map(({ name, everyMs, running, lastStart, lastEnd }) => ({ name, everyMs, running, lastStart, lastEnd })) | |
| } | |
| module.exports = { start, scanNow, status } | |