Coder Social home page Coder Social logo

jonike / celery-once Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cameronmaske/celery-once

0.0 2.0 0.0 155 KB

Celery Once allows you to prevent multiple execution and queuing of celery tasks.

Home Page: https://pypi.python.org/pypi/celery_once/

License: BSD 2-Clause "Simplified" License

Python 99.65% Dockerfile 0.35%

celery-once's Introduction

Celery Once

Build Status Coverage Status

Celery Once allows you to prevent multiple execution and queuing of celery tasks.

Installation

Installing celery_once is simple with pip, just run:

pip install -U celery_once

Requirements

  • Celery. Built to run with Celery 4.0. Older versions may work, but are not officially supported.

Usage

To use celery_once, your tasks need to inherit from an abstract base task called QueueOnce.

Once installed, you'll need to configure a few options a ONCE key in celery's conf.

The exact configuration, depends on which locking backend you want to use. See Backends.

Behind the scenes, this overrides apply_async and delay. It does not affect calling the tasks directly.

When running the task, celery_once checks that no lock is in place (against a Redis key). If it isn't, the task will run as normal. Once the task completes (or ends due to an exception) the lock will clear. If an attempt is made to run the task again before it completes an AlreadyQueued exception will be raised.

graceful

Optionally, instead of raising an AlreadyQueued exception, the task can return None if once={'graceful': True} is set in the task's options or when run through apply_async.

keys

By default celery_once creates a lock based on the task's name and its arguments and values. Take for example, the following task below...

Running the task with different arguments will default to checking against different locks.

If you want to specify locking based on a subset, or no arguments you can adjust the keys celery_once looks at in the task's options with once={'keys': [..]}

timeout

As a fall back, celery_once will clear a lock after 60 minutes. This is set globally in Celery's configuration with ONCE_DEFAULT_TIMEOUT but can be set for individual tasks using...

unlock_before_run

By default, the lock is removed after the task has executed (using celery's after_return). This behaviour can be changed setting the task's option unlock_before_run. When set to True, the lock will be removed just before executing the task.

Caveats:
  • Any retry of the task won't re-enable the lock!
  • This can only be set when defining the task, it cannot be passed dynamically to apply_async

Backends

Redis Backend

Requires:

Configuration:

  • backend - celery_once.backends.Redis
  • settings
  • default_timeout - how many seconds after a lock has been set before it should automatically timeout (defaults to 3600 seconds, or 1 hour).
  • url - should point towards a running Redis instance (defaults to redis://localhost:6379/0). See below for the format options supported
  • blocking (boolean value: default False) - If set to True, scheduling a task (by .delay/.apply_async) will block for X seconds to acquire the lock (see: blocking_timeout below). If no lock could be acquired after X seconds, will raise an AlreadyQueued exception. This is a very specific use-case scenario and by default is disabled.
  • blocking_timeout (int or float value: default 1) - How many seconds the task will block trying to acquire the lock, if blocking is set to True. Setting this to None set's no timeout (equivalent to infinite seconds).

The URL parser supports three patterns of urls:

  • redis://host:port[/db][?options]: redis over TCP
  • rediss://host:port[/db][?options]: redis over TCP with SSL enabled.
  • redis+socket:///path/to/redis.sock[?options]: redis over a UNIX socket

    The options query args are mapped to the StrictRedis keyword args. Examples:
    • redis://localhost:6379/1
    • redis://localhost:6379/1?ssl=true
    • rediss://localhost:6379/1
    • redis+socket:///var/run/redis/redis.sock?db=1

Example Configuration:

Minimal:

Advanced: Scheduling tasks blocks up to 30 seconds trying to acquire a lock before raising an exception.

File Backend

Configuration:

  • backend - celery_once.backends.File
  • settings
  • location - directory where lock files will be located. Default is temporary directory.
  • default_timeout - how many seconds after a lock has been set before it should automatically timeout (defaults to 3600 seconds, or 1 hour).

Example Configuration:

Flask Integration

To avoid RuntimeError: Working outside of application context errors when using celery_once with Flask, you need to make the QueueOnce task base class application context aware. If you've implemented Celery following the Flask documentation you can extend it like so.

Now, when instead of importing the QueueOnce base, you can use the context aware base on the celery object.

Custom Backend

If you want to implement a custom locking backend, see BACKEND\_GUIDE.rst.

Support

  • Tests are run against Python 2.7, 3.4 and 3.5. Other versions may work, but are not officially supported.

Contributing

Contributions are welcome, and they are greatly appreciated! See contributing guide for more details.

celery-once's People

Contributors

abildin avatar cameronmaske avatar gustavoalmeida avatar imomaliev avatar jwpe avatar lologhi avatar philipgarnero avatar pkariz avatar snake575 avatar sobolevn avatar streeter avatar xuhcc 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.