Cron and Daemons: K2Awards Case Study


Demonstrate creative use of daemons and cron using K2Awards


K2Awards has a complex trophy builder, called jthumb, that uses ImageMagick to dynamically create trophy images. K2Awards has hundreds of thousands of possible trophy combinations, we use the jthumb script to create images representing those trophy combinations. We trade hard disk space, for webpage loading efficiency and browser compatibility.

Image Magick actually is most efficient running in two threads. You get diminishing returns using more threads. Since we use a heavy threaded environment with multiple cores it is key to performance to limit to 2 cores, then run multiple processes. By using this style of programming, we were able to take K2 image processing from a 6 hour job, to a 3 minute job.

In order to run multiple processes, we have to have a master daemon. This master daemon is a python script called k2serverd. It is written in Python.

Periodically, the script checks the WordPress Database to see if there is a job available. The WordPress user marks a trophy object to be processed. If k2serverd sees that there is a trophy to be processed it begins to build the trophies.

It forks 4-16 child processes. Each child handles one of the image combinations. After the php script is forked, k2serverd checks to see when the processes finish. Once finished, it will spawn more child processes until the job is completed and all image combinations are built.

This job is too complicated to run in a browser, some massive jobs like building 3 tier trophies with 5 combinations of 30(3x30x5x15x19) combinations for instance might take as long as 30 minutes to complete! That is why we use a daemon to do the task in the background. We also limit children to not overwhelm the server.

The script also spawns a socket. The socket connection allows WordPress PHP to query the daemon and check the status. This allows K2Awards to go to a status page and see if the daemon is running processes. How long it will take to complete. How long it has been running. How many items are left etc.

The Daemon

k2serverd runs continuously in the background to allow K2Awards to build new trophies at anytime they need to.

When built, we added many system messages to allow the developers to see what the script is currently doing since it is a continuously running process. Originally the script was written to output to STD_OUT since it was not a daemon at first.

Proper daemons push their messages to the UNIX syslog. This is performed in python by:

import syslog

syslog.syslog("My message here")

If you are writing your script in a different programming language such as PERL or PHP, look up online how to implement system messages in your particular language.

Syslog then pipes information to /var/log/syslog

You can check this out by typing tail -f /var/log/syslog which will show you the messages in your CMD as they come in.

Debug Mode

Since we are running the daemon as a thread, it was important to change the syslog messages to have a debug mode

This is because we were piping "updating processes" etc over and over to /var/log/syslog

You don't want to spam the syslog with unnecessary information. I added a flag at the top of the script to allow for debugging. Around spammed text it now reads:

if master.debug:
syslog.syslog("Neat message")

Daemon Startup Script

There is a daemon BASH start script located in /etc/init.d/k2serverd_init

This script is ran in /etc/rc.local on startup, and has been added to check_running_process to make sure that it is running at all times

Comments are closed.