Task scheduling support on AppFog with standalone apps

September 20, 2012

Since we launched AppFog a few months back, a lot of users have had questions about cron job support, which is often essential to the basic functioning of an app.

Fortunately, it is already possible to run cron jobs on AppFog, and there are a few ways of doing so in just about any language that runs on our platform.

Using Background Workers on AppFog

Up until a few months ago, all applications running on Cloud Foundry (and thereby on Cloud Foundry-based systems like AppFog) were required to be “normal” web applications responding to HTTP requests.

But a while back, the CloudFoundry.org project (on which AppFog is based) announced that they were introducing background processing support through Cloud Foundry, and, by extension through AppFog. This is a bigger win for developers than many may realize.

Adding background processing support means that you can now run container-less, “standalone” apps on AppFog that do, well, pretty much anything you want!

One of my favorite examples of using background workers on AppFog comes from one of our developers, Tim Santeford, who is using them as part of a personal project. Tim is building a financial information app that relies on up-to-date stock market information. In order to implement this, Tim has built a standalone app using the node-cron module for node.js that gathers information from the TradeKing API once an hour and caches that information for use by his app. Very cool stuff.

Example: the Whenever gem

Node-cron is a great tool but it is not alone. In Ruby, the Whenever gem allows you to run cron jobs using an incredibly simple syntax. One of the examples given in the GitHub repo demonstrates this:

every 3.hours do
    runner “MyModel.someProcess”
    rake “my:rake:task”
    command “/usr/bin/my_great_command”

Or you can designate specific times to run tasks:

every 1.day, :at => '4:30 am' do
    runner "MyModel.my_incredibly_robust_and_sophisticated_task"

The commands you run could involve, for example, pinging a URL in one of your other apps that induces a database backup. Or maybe you run a command that clears a cache or performs web scraping within a set of domains or sends out e-mails to a particular subset of your users. Or all of the above. The possibilities are vast.

Building your own standalone apps

The brief standalone app tutorial on the Cloud Foundry post mentioned above is a great place to start for learning how to build your own headless apps. Here is an equivalent to the very basic Ruby example that they provide there, but with a few AppFog-specific modifications.

First, we’ll make a simple Ruby file (portland.rb) that outputs “Portland: life is just better here” every 5 seconds. Here’s what that file looks like:

loop {
    puts "Portland: life is just better here"
    sleep 5

Now, we want this file and the process associated with it running on AppFog. Fortunately, standalone apps are just as easy to deploy on AppFog as non-standalone apps.

Just run af push from the app’s directory (let’s call the app portlandrules), and if it asks you if you’re deploying a Django app or a Rails app or whatever, just respond “no.” When it asks you if you’re deploying a standalone app, respond “yes,” and you can then specify (a) which runtime the app is in, and (b) precisely which start command you want to use with the app.

The command that begins the application will be the same that would start the application in the Cloud Foundry environment. In this case, that ends up being ruby portland.rb (which would run the file in just about any *nix environment that I can imagine). Once the app is initiated, it will do precisely what it did in your local environment: it will display “Portland: life is just better here” every 5 seconds until you tell it to stop.

You may be wondering at this point: “How do I know it’s actually doing that if I can’t access it via HTTP or another method?” Well, it’s actually incredibly simple. Just run af logs portlandrules and it will give you the “output” of the app. In this case, the logs will show that “Portland: life is just better here” as many times as the app has up to that point––and so if you start the application and wait an hour, it will spit out the string 720 times!

This is a really basic example, but hopefully it illustrates how standalone apps function in conjunction with our platform. And it should also be clear how something like the Whenever gem could easily be used as the basis for a standalone app.

More on the way

In the coming months, we’ll be expanding our cron support by adding Celery and other tools. But fortunately, the tools currently available for Ruby, Node, and other runtimes are incredibly powerful and already at your fingertips on AppFog. So schedule away and let us know if you have any issues at support@appfog.com.


Read from Source


Paasmag Logo


Paasmag Twitter