Apache2 + WSGI auto restart

The one thing I loved about Passenger and Ruby (from a time when I was working on it) was how easy it was to reload your code. I still had to touch a file to do it but that gave me an idea how to solve my problem when working on Python code that runs via Apache2 and mod WSGI.

I use Coda 2 to develop web projects and when I have something to publish I just hit Ctrl+Shift+P, my files are uploaded and I have to reload Apache to see my changes (if changes were in Python code).

Aint Nobody Got Time for That

There is a proper way to do this which is described here (I should re-use ^^^ here). I come up with a bit quicker solution that fits my needs.

Here comes the monit. The idea is to monitor the WSGI Script and restart Apache on change. On Debian/Ubuntu it can be installed with APT:

user@computer:$ sudo apt-get install monit

Enable it:

user@computer:$ sudo vim /etc/default/monit

Just change startup=0 to startup=1:

 Defaults for monit initscript
# sourced by /etc/init.d/monit
# installed at /etc/default/monit by maintainer scripts
# Stefan Alfredsson <alfs@debian.org>

# You must set this variable to for monit to start

# You can change the location of the state file here
# It can also be set in monitrc
# STATEFILE="/var/lib/monit/monit.state"

# To change the intervals which monit should run,
# edit the configuration file /etc/monit/monitrc
# It can no longer be configured here.

Next thing I needed was some basic config for monit:

user@computer:$ sudo vim /etc/monit/conf.d/monit.conf

Something like this works for me:

set daemon 60
set logfile syslog facility log_daemon

set alert foo@bar.com

set httpd port 2812 and
use address
allow admin:monit

check process apache2 with pidfile /var/run/apache2.pid
    start program "/etc/init.d/apache2 start"
    stop program "/etc/init.d/apache2 stop"
    depends on app

check file app
    with path /home/artur/app/app.py
    if changed timestamp then restart

Restarted monit and Apache was restarted on my next change:

user@computer:$ sudo /etc/init.d/monit restart

From /var/log/daemon.log I can see it worked:

Aug 27 07:10:46 server monit[24126]: Starting monit daemon with http interface at [localhost:2812]
Aug 27 07:10:46 server monit[24128]: Starting monit HTTP server at [localhost:2812]
Aug 27 07:10:46 server monit[24128]: monit HTTP server started
Aug 27 07:10:46 server monit[24128]: 'localhost' Monit started
Aug 27 07:10:46 server monit[24128]: No mail servers are defined -- see manual for 'set mailserver' statement
Aug 27 07:10:46 server monit[24128]: Aborting event
Aug 27 07:12:46 server monit[24128]: 'app' timestamp was changed for /home/artur/app/app.py
Aug 27 07:12:46 server monit[24128]: No mail servers are defined -- see manual for 'set mailserver' statement
Aug 27 07:12:46 server monit[24128]: Aborting event
Aug 27 07:12:46 server monit[24128]: 'app' trying to restart
Aug 27 07:12:46 server monit[24128]: 'apache2' stop: /etc/init.d/apache2
Aug 27 07:12:48 server monit[24128]: 'apache2' start: /etc/init.d/apache2
Aug 27 07:13:50 server monit[24128]: 'app' timestamp was not changed for /home/artur/app/app.py

I changed the config to lower down interval to 5 seconds and disabled logs.

Plotting power usage with Current Cost and Graphite

I’ve recently upgraded my free power meter to something more hack friendly. Current Cost EnviR power meter outputs current power usage in Watts + current temperature (where the base station is) every 6 seconds in XML format over serial connection. I could get a Current Cost bridge that will post this data to Current Cost website but I though Graphite is much better for this. Here is a quick post on how to get this date into Graphite.
Continue reading

Alfred Workflows

If you ever thought why I should buy a Mac the answer is simple, Alfred ! Can’t think of my work day without it. Soon to be release V2 of Alfred will include a new feature called “Workflows” and as a Mega Supporter I have access to beta version of the app and let me tell you this, if you’re a Power Pack user, prepare for your mind to be blown !
Continue reading

Dashing – The exceptionally handsome dashboard framework

I love dashboards, or actually monitoring dashboards, especially small simple scripts giving you a nice view into what’s important for you.

In my time at Rackspace I have build a Bootsrap PHP polling monster showing a lot of details about Rackspace Cloud (metrics, graphs). This help me and other admins to have a easy way to look into our infrastructure. The main thing is, all of it is read only. However, as much as I love Bootstrap it’s not ideal solution for large TVs on the wall were data needs to be pushed and things are BIG. Here comes Dashing Continue reading

Can’t unlock System Preferences padlock

Another problem I had after upgrading to Mountain Lion was that every time I tried to open the padlock in System Preferences my password was not accepted, here’s a quick fix:

user@computer:$ sudo rm -r ~/Library/Caches/com.apple.systempreferences

then reboot your system and you should be good.