Job Types

There are several Plan built-in cron job types.

Command Job

One command is a common executable program, check out CommandJob. Plan comes with one shortcut for register CommandJob command().

Script Job

One script should be one Python pyfile, check out ScriptJob. Plan comes with one shortcut for register ScriptJob script().

Module Job

One module should be one Python module, check out ModuleJob. Plan comes with one shortcut for register ModuleJob module().

Raw Job

Maybe these job types are not what you want, you can define your job with raw cron syntax:

cron = Plan()
cron.raw('cd /tmp && ruby script.rb > /dev/null 2>&1', every='1.day')

# In this particular case, you should try Job
job = Job('ruby script.rb', every='1.day', path='/tmp', output='null')
cron.job(job)

Define Your Own Job Types

You can define your own job types if you want. Before we talk about how to do that, let’s have a look on what a shortcut like command() do:

plan = Plan()
job = CommandJob(*args, **kwargs)
plan.job(job)

What job() does is registering one job on this plan object. If you want to write one own job type, just define one subclass of Job and override task_template(), let’s see what CommandJob looks like inside Plan:

class CommandJob(Job):

    def task_template(self):
        return '{task} {output}'

The ScriptJob and ModuleJob are almost the same, with different template:

# ScriptJob
return 'cd {path} && {environment} %s {task} {output}' % sys.executable
# ModuleJob
return '{environment} %s -m {task} {output}' % sys.executable

Now I want to have one job type to run ruby script, I can define it like this:

class RubyJob(Job):

    def task_template(self):
        return 'cd {path} && {environment} /usr/bin/ruby {task} {output}'

And use it like this:

plan = Plan()
job = RubyJob(*args, **kwargs)
plan.job(job)

Mostly If CommandJob, ScriptJob and ModuleJob are not what you need, you can just use Job or even RawJob.

Table Of Contents

Related Topics

This Page