Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Porting the Job Runner

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.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In this lesson, you’ll read your JavaScript port of Priya’s job runner as one file and check it against her 110 lines of Python. Your print-runner folder holds the artwork, the other PC’s lock, two job files, and a package.json set to module.

00:17 Your runner.js grew one lesson at a time, and its outcome handling and summary are set aside for your next exercise.

00:26 Priya’s imports become imports from five of Node’s built-in modules. node:fs has the synchronous lock and copy calls, since a plain dispose method can’t await.

00:38 node:fs/promises has the awaited ones, and node:os and node:path have the folder helpers. node:timers/promises has no sleep, so you import its setTimeout() under that name.

00:53 JobLock keeps Priya’s shape. With no enter step in using, the constructor does __enter__()’s job, so creating one now takes the lock.

01:04 [Symbol.dispose]() deletes it, as __exit__() does. If another runner holds the lock, the constructor throws, and as with a failing __enter__(), nothing is released: the other PC’s lock stays.

01:20 Priya’s @contextmanager becomes a function that returns a disposable. Its body copies the artwork at once, like the code before yield does.

01:30 The object it returns holds the copy’s name, its path, and a dispose method for the part after yield in Priya’s version. There’s no as step, so spool is that object, not a path.

01:45 PrinterQueue’s constructor starts the heartbeat with setInterval(), where __aenter__() starts a task. A constructor can’t be async, any more than __init__ can, so connecting happens in connect(), a static async method.

02:00 send() logs what the press accepted, with the object’s own name where Python used the path’s.

02:08 __aexit__() becomes [Symbol.asyncDispose](), whose promise the cleanup waits for. It clears the interval: unlike asyncio.run(), nothing cancels a leftover timer, and a live one keeps the program running.

02:23 The sleep() after it stands for the disconnect. removeSpool() calls rm() with recursive: true, as shutil.rmtree() does.

02:36 In runJob(), the two with blocks become a bare block with two using lines. It ends where the with blocks end, so the lock is released before the done line.

02:47 The copy, declared last, is released first. In main(), a block opens with await using stack, an AsyncDisposableStack, which stands in for Priya’s AsyncExitStack here.

03:02 new gives the stack, not a promise, so one await, for the cleanup, is enough. mkdtemp() makes the spool folder, like tempfile.mkdtemp() in Python.

03:14 Unlike callback(), defer() passes no arguments, so you defer a closure.

03:21 The queues go in a Map, keyed by press. The first time a press comes up, Priya’s enter_async_context() connects and registers in one call. Here you await connect() for the queue itself, and stack.use() only registers it, returning it for the Map.

03:40 Then runJob() runs the job. Priya’s except clauses, counts and summary are set aside, so they stay folded. import.meta.main is true only when you run this file, like Python’s __name__ check.

03:57 Top-level await takes asyncio.run()’s place, minus the cleanup of leftover tasks.

04:04 Check the file with node –check: no output means no syntax errors. Then run it on jobs-check.json, the two jobs that print. J-311 prints its eight lines, and J-312’s eight follow.

04:23 Both queues connected, so the stack holds three cleanups. Your turn: pause the video and predict the order of the last three lines.

04:37 press-2 closes first, then press-1, and the folder goes last. The stack releases the newest first, as Python’s does.

04:49 Run Priya’s runner.py on the same file: its first nineteen lines match yours. Its last two come from the part you set aside. Your port exits with zero, and the prompt comes back at once, with no heartbeat running.

05:06 locks holds only the other PC’s lock.

05:11 That’s Priya’s runner in JavaScript: classes for the lock and the queue, a function returning a disposable for the spool copy, using blocks as long as her with blocks, and one stack for what comes and goes.

05:23 Next up, Finish the Job Runner, an exercise: bring back the set-aside part, so your port prints Priya’s 27 lines, for this morning and for made-up ones.

Become a Member to join the conversation.