Skip to main content

A scheduler backed by Redis with a very simple interface

Project description

Build Status

A scheduler backed by Redis with a very simple interface.

RACHE doesn’t handle job execution. It only maintains a list of jobs and their theoretical execution time. It’s up to you to monitor pending jobs and send them to an actual task queue.

Installation

RACHE works with any Python version from 2.6 to 3.3. You only need a working Redis server.

pip install rache

Configuration

By default RACHE connects to Redis on localhost, port 6379, database 0. To override this, set a REDIS_URL environment variable:

REDIS_URL=redis://redis.example.com:6379/2

RACHE prefixes all its Redis keys with rache:. You can override this by setting the RACHE_REDIS_PREFIX environment variable.

Usage

import rq

from rache import schedule_job, pending_jobs

# Schedule a job now
schedule_job('http://github.com/brutasse/rache', schedule_in=0, timeout=10)

# Get pending jobs
jobs = pending_jobs()

# Send them to the task queue for immediate execution
for job in jobs:
    rq.enqueue_job(...)

schedule_job

schedule_job('job id', schedule_in=<seconds>, **kwargs)

A given job ID is unique from the scheduler perspective. Scheduling it twice results in it being scheduled at the time decided in the last call.

**kwargs can be used to attach data to your jobs. For instance, if you have jobs to fetch URLs and want to attach a timeout to these jobs:

schedule_job('http://example.com/test', schedule_in=3600, timeout=10)

The job data is persistent. To remove a key from the data, call schedule_job() with that key set to None:

schedule_job('http://example.com/test', schedule_in=3600, timeout=None)

schedule_in is mandatory. This means you can’t update an existing job without rescheduling it.

pending_jobs

jobs = pending_jobs(reschedule_in=None)

(the returned value is a generator)

Fetches the pending jobs and returns a list of jobs. Each job is a dictionnary with an id key and its additional data.

reschedule_in controls whether to auto-reschedule jobs in a given time. This is useful if you have periodic jobs but also want to special-case some jobs according to their results (enqueue is rq-style syntax):

jobs = pending_jobs(reschedule_in=3600)

for job in jobs:
    enqueue(do_something, kwargs=job)

def do_something(**kwargs):
    # … do some work

    if some_condition:
        # re-schedule in 30 days
        schedule_job(kwargs['id'], schedule_in=3600 * 24 * 30)

delete_job

delete_job('<job id>')

Removes a job completely from the scheduler.

scheduled_jobs

scheduled_jobs(with_times=False)

(the returned value is a generator)

Fetches all the job IDs stored in the scheduler. This returns a list of IDs or a list of (job_id, timestamp) tuples if with_times is set to True.

This is useful for syncing jobs between the scheduler and a database, for instance.

Contributing

Create a local environment:

virtualen env
source env/bin/activate
pip install -e .

Run the tests:

python setup.py test

Or for all supported python versions:

tox

Hack, fix bugs and submit pull requests!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rache-0.1.tar.gz (5.7 kB view details)

Uploaded Source

File details

Details for the file rache-0.1.tar.gz.

File metadata

  • Download URL: rache-0.1.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for rache-0.1.tar.gz
Algorithm Hash digest
SHA256 91820bbd00b4e8ed7b46b68c1e85d1092ab0c279bae85e6e368810e3cde9a852
MD5 727de401792e2a5ffc75f228cff99fda
BLAKE2b-256 ce235a8d66c013be633b52e3f8e4ee147fff9cef5824218161ed29661cea4916

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page