Porting the Job Runner
Priya’s runner.py comes to JavaScript as one runner.js, built from the pieces of this course
and run as an ECMAScript (ES) module, with { "type": "module" } in package.json. Its imports come from
five of Node’s built-in modules: node:fs, node:fs/promises, node:os, node:path and
node:timers/promises, whose setTimeout() is imported as sleep. The lock and the copy use node:fs’s synchronous calls, since a plain dispose method
can’t await.
Each resource keeps the shape it had in Python. JobLock takes the lock in its constructor,
because using has no enter step, and releases it in [Symbol.dispose](): creating a
JobLock takes the lock, where Python’s waits for the with. With no try around it, a lock
that another runner holds throws before using has an object, as a failing __enter__()
never reaches __exit__(), so nothing deletes it.
spooled()’s body copies the artwork when it’s called, like the code before yield, and it
returns an object with the copy’s name, its path and a dispose method that removes the copy,
like the code after yield. So spool is that object, not a path, and its name stands in
for the path’s .name in send().
PrinterQueue’s constructor starts the heartbeat with setInterval(), and the connecting
lives in the static async connect(), since a constructor can’t be async, any more than
__init__ can.