image/svg+xml

Signal Handling

Jun 10, 2020

Since AutoComic can take quite a while to run (downloading thousands of webpages and images can be slow), I designed it to be able to be stopped at any time. If the script is stopped, it will resume where it left off without adding duplicate images or skipping images.

In order to make this work correctly, the main loop looks something like this:

while validURL and not killed: pdf.addComic(comic) comic.save() pdf.save() comic.advance() pdf.finish() 

If the script is killed at any point along the process, it will have saved its progress. No matter when the script dies, a pdf is output.

Although it is unlikely, a SIGINT (^C) command can be sent after adding the image but before saving. To prevent this behavior, I added a signal handler for SIGINT (see signal handling in Python) that simply sets the global killed variable to true. This will make the current loop finish its iteration before exiting, which allows a graceful exit.

The same is not true if unexpected exceptions are raised. Handling exceptions in a way that will not break the output is a priority for future development. Currently, most exceptions do not create invalid output. This means that the script can be resumed when the cause is fixed.

How data must be saved

This problem is made more complex by the fact that many webcomics update, so a script may also be stopped because there are no more comics. In this case, it should still try to run when started again because the website may have updated.

To solve this problem, the script stores the previous URL rather than the current URL in a file. When the script is started, it will start at the page before the last page it did. Then if a comic adds a new page, the script can find it.

Running javascript throws a wrench into the works

A recent addition to AutoComic was the ability to run javascript for client side rendered websites. The library used render javascript does not allow signals while rendering. Thus, the signal handler is not called while the webpage is rendering and the script may not exit gracefully. Although using a simple try-catch block does not allow for as graceful exits as the signal handler, adding the try-catch block allows the script to clean up.