GithubHelp home page GithubHelp logo

django-apscheduler's Introduction

Django APScheduler

** HELP NEEDED WITH MAINTENANCE **

If anyone feels up to helping maintain this package, please PM me and we can get you some privileges.

Build status codecov PyPI version

APScheduler for Django.

This little wrapper around APScheduler enables storing persistent jobs in the database using Django's ORM rather than requiring SQLAlchemy or some other bloatware.

Features in this project:

  • Work on both python2.* and python3+
  • Manage jobs from Django admin interface
  • Monitor your job execution status: duration, exception, traceback, input parameters.

Installation

pip install django-apscheduler

Usage

  • Add django_apscheduler to INSTALLED_APPS in your Django project settings, You can also specify a different format for displaying runtime timestamps in the Django admin site using APSCHEDULER_DATETIME_FORMAT:

    INSTALLED_APPS = (
      ...
      django_apscheduler,
    )
    
    APSCHEDULER_DATETIME_FORMAT =  "N j, Y, f:s a"  # Default
  • Run migrations:

    ./manage.py migrate
  • Instantiate a new scheduler as you would with APScheduler. For example:

    from apscheduler.schedulers.background import BackgroundScheduler
    
    scheduler = BackgroundScheduler()
  • Instruct the scheduler to use DjangoJobStore:

    from django_apscheduler.jobstores import DjangoJobStore
    
    # If you want all scheduled jobs to use this store by default,
    # use the name 'default' instead of 'djangojobstore'.
    scheduler.add_jobstore(DjangoJobStore(), 'djangojobstore')
  • If you want per-execution monitoring, call register_events on your scheduler:

      from django_apscheduler.jobstores import register_events
      register_events(scheduler)

    It provides the following interface:

  • Old job executions can be deleted with:

  DjangoJobExecution.objects.delete_old_job_executions(604_800)  # Delete job executions older than 7 days
  • Register any jobs as you would normally. Note that if you haven't set DjangoJobStore as the 'default' job store, you'll need to include jobstore='djangojobstore' in your scheduler.add_job calls.

  • Don't forget to give each job a unique id. For example:

    @scheduler.scheduled_job("interval", seconds=60, id="job")
    def job():
      ...

    or use custom decorator for job registration. It will give id automatically:

    from django_apscheduler.jobstores import register_job
    
    @register_job("interval", seconds=60)
    def job():
      ...
  • Start the scheduler:

    scheduler.start()

A full example project can be found in the example dir. Code snippet:

import time

from apscheduler.schedulers.background import BackgroundScheduler
from django_apscheduler.jobstores import DjangoJobStore, register_events, register_job

scheduler = BackgroundScheduler()
scheduler.add_jobstore(DjangoJobStore(), "default")

@register_job(scheduler, "interval", seconds=1)
def test_job():
    time.sleep(4)
    print("I'm a test job!")
    # raise ValueError("Olala!")

register_events(scheduler)

scheduler.start()
print("Scheduler started!")

django-apscheduler's People

Contributors

bnjmnhndrsn avatar jarekwg avatar jcass77 avatar jedore avatar joaodaher avatar nialllo avatar sallyruthstruik avatar soon avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.