Skip to content

Getting Started with Octobot

cscotta edited this page Sep 22, 2010 · 9 revisions

Writing a task and firing up Octobot in 10 minutes or less:

Here are a few quick steps to help you get started with a fresh Octobot setup,
including everything you need to download and set it up, write a sample task,
and run it from a language like Ruby or Python.


  1. Download the Octobot binary from here and extract it:
    http://octobot.taco.cat/downloads/octobot.alpha3.tar.gz
  2. Modify the example-config.yml file to match your configuration. For the
    purposes of this example, we’ll use a Beanstalk queue running on your local
    machine on the default port. Just erase what’s in there and replace it with this:
    Octobot:
      queues:
       - { name: stalky,
           protocol: Beanstalk,
           host: localhost,
           port: 11300,
           priority: 5,
           workers: 1
         }
  3. Type “mkdir -p tasks/src && mkdir -p tasks/build” to create a “tasks”
    directory, along with the “src” and “build” directories under it. Create a new
    file under tasks/src called MakeBurrito.java, and drop this into it:
    import org.json.simple.JSONObject;
    import org.json.simple.JSONArray;
    import org.apache.log4j.Logger;
    
    public class MakeBurrito {
        
        private static Logger log = Logger.getLogger("Burritobot");
    
        public static void run(JSONObject task) {
            JSONArray args =  (JSONArray) task.get("args");
    
            String meat  = (String) args.get(0);
            String beans = (String) args.get(1);
            String salsa = (String) args.get(2);
            
            log.info("Making a " + meat + " burrito with " +
                beans + " beans and " + salsa + " salsa!");
        }
    
    }
  4. From the base Octobot directory, compile the task by typing:
    “javac -cp jar/octobot.jar -d tasks/build tasks/src/*.java”

Then, package up the tasks into a self-contained jar by typing:
“jar -cf tasks.jar -C tasks/build/ .” (including the period, but not quotes)

You should now have a file called “tasks.jar” in the base directory containing
your sample task.

Assuming “beanstalkd” is running on your system, you should be able to type
“./octobot” to launch Octobot. If everything worked, you’ll see a message
saying “Queue Consumer – Connected to Beanstalk; waiting for jobs.”


Running Your Task [Ruby]:

The easiest way to test a task is by running it! Here’s an example using Ruby
to enqueue and run a task with Octobot. Fire up Octobot, and try the following:

  1. If you don’t have the Beanstalk and/or JSON gems installed, type:
    “sudo gem install beanstalk-client json”
  2. Type “irb” to launch the Ruby interpreter, and type the following:
    require 'json'
    require 'beanstalk-client'
    task = {:task => 'MakeBurrito', :args => ['chicken', 'pinto', 'habanero'], :retries => 0}
    beanstalk = Beanstalk::Pool.new(['localhost:11300'])
    beanstalk.use('stalky')
    beanstalk.put(task.to_json)
  3. Octobot should log out the following:
    13408 [Burritobot] INFO Sample Task – Making a chicken burrito with pinto beans and habanero salsa!

Running Your Task [Python]:

The easiest way to test a task is by running it! Here’s an example using Python
to enqueue and run a task with Octobot. Fire up Octobot, and try the following:

  1. If you don’t have the Beanstalk module installed, type:
    “sudo easy_install beanstalkc”
  2. Type “python” to launch the Python interpreter, and type the following:
    import beanstalkc
    import json
    task = {'task': 'MakeBurrito', 'args': ['chicken', 'pinto', 'habanero'], 'retries': 0}
    beanstalk = beanstalkc.Connection()
    beanstalk.use('stalky')
    beanstalk.put(json.dumps(task))
  3. Octobot should log out the following:
    13408 [Burritobot] INFO Sample Task – Making a chicken burrito with pinto beans and habanero salsa!

These are about the simplest possible examples and are pretty boring. Keep in
mind that anything can be done in the language you choose to write tasks can
happen here, and that it can happen very quickly.

Happy hacking!

- Scott