Skip to main content

Package to call celery tasks only once per unique key.

Project description

WebCase celery one task caller

Package to call celery tasks only once per unique key.

Installation

pip install wc-django-celery-unicall

In settings.py:

INSTALLED_APPS += [
  'wcd_celery_unicall',
]

Usage

from celery import Celery
from wcd_celery_unicall.task import CACHE_KEY_PREFIX
from wcd_celery_unicall.shortcuts import make_unicall, task

app = Celery('app')

# First after defining and configuring celery app you should change
# the default `Task` class.
# It has a small improvement in `apply_async` method, that will revoke
# other pending tasks that have the same uniqueness key.
app.Task = make_unicall(app.Task)

# ...

from django.core.cache import cache

# All other tasks that you want to check for uniqueness have to be defined
# using a `task` shortcut decorator.
# So instead of writing `@app.task()` write `@task(app)`.
# But that's not all. To enforce uniqueness check mechanics you must
# provide at least 2 additional parameters: `unicall_key` and `unicall_cache`.
@task(
  # Your celery app:
  app,
  # Function that receives the same arguments that your task, and generates
  # unique key to track task execution.
  unicall_key=lambda user_id, **_: f'sync_user {user_id}',
  # And a django cache backend implementation. This could be anything you
  # wish, but `LockMem` cache backend, for example will be useless.
  # So this should be "global" cache backend, like redis or memcached.
  unicall_cache=cache,
  # Optional. Additional TTL, that will be added to key expirity.
  unicall_additional_ttl=None,
  # Optional. Cache key prefix, if you don't like the default one.
  unicall_key_prefix=CACHE_KEY_PREFIX,
)
def do_something(user_id: int, param2: bool = True, param_3=None):
  pass

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

[Unreleased]

[0.1.1]

Fixed

  • Latest task identifier storage and check.

Changed

  • Increased additional ttl to remember last task id for longer.

[0.1.0]

Initial version.

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

wc_django_celery_unicall-0.1.1.tar.gz (6.8 kB view details)

Uploaded Source

File details

Details for the file wc_django_celery_unicall-0.1.1.tar.gz.

File metadata

File hashes

Hashes for wc_django_celery_unicall-0.1.1.tar.gz
Algorithm Hash digest
SHA256 df596e6187a44dc41dbdd7d6fc509dfce23c1fe1c85b866cd8a2bdc53f518a71
MD5 1c97c2e2b7892bbca46cbc858c3b69b2
BLAKE2b-256 cee9a129fa58c2f4a24a1bfcee8c2bc4831160779a9e1aae69066acce6cac1d1

See more details on using hashes here.

Supported by

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