def test_schedule_batch(patch_now): now = get_now() tasks = Tasks() tasks.add(print, 'foo_task') tasks.add(print, 'bar_task') broker = Mock() s = Engine(broker, namespace='tests') s.attach_tasks(tasks) batch = Batch() batch.schedule('foo_task', 1, 2) batch.schedule_at('bar_task', now, three=True) s.schedule_batch(batch) broker.enqueue_jobs.assert_called_once_with([ANY, ANY]) foo_job = broker.enqueue_jobs.call_args[0][0][0] assert foo_job.task_name == 'foo_task' assert foo_job.at == now assert foo_job.task_args == (1, 2) assert foo_job.task_kwargs == {} bar_job = broker.enqueue_jobs.call_args[0][0][1] assert bar_job.task_name == 'bar_task' assert bar_job.at == now assert bar_job.task_args == () assert bar_job.task_kwargs == {'three': True}
def spin(): tasks = Tasks() @tasks.task(name='success') def success(): return @tasks.task(name='fail') def fail(): raise RuntimeError('failing task') s = Engine(MemoryBroker(), namespace='tests-datadog') s.attach_tasks(tasks) s.start_workers(number=1, block=False) yield s s.stop_workers()
import importlib from django.apps import AppConfig from django.utils.module_loading import module_has_submodule from spinach import Tasks, Engine from .settings import SPINACH_BROKER, SPINACH_NAMESPACE spin = Engine(broker=SPINACH_BROKER, namespace=SPINACH_NAMESPACE) class SpinachdConfig(AppConfig): name = 'spinach.contrib.spinachd' verbose_name = 'Django Spinach' def ready(self): from . import signals # noqa for discovered_module in autodiscover_modules('tasks'): try: module_tasks = discovered_module.tasks except AttributeError: continue if isinstance(module_tasks, Tasks): spin.attach_tasks(module_tasks) def autodiscover_modules(*args): from django.apps import apps
def test_start_workers_blocking(): spin = Engine(MemoryBroker(), namespace='tests') spin.start_workers(number=1, block=True, stop_when_queue_empty=True) assert not spin._must_stop.is_set()
def spin(): s = Engine(MemoryBroker(), namespace='tests') s.start_workers(number=1, block=False) yield s s.stop_workers()
from datetime import timedelta from spinach import Engine, MemoryBroker spin = Engine(MemoryBroker()) every_5_sec = timedelta(seconds=5) @spin.task(name='make_coffee', periodicity=every_5_sec) def make_coffee(): print("Making coffee...") print('Starting workers, ^C to quit') spin.start_workers()
import time import logging from spinach import Engine, MemoryBroker logging.basicConfig( format='%(asctime)s - %(threadName)s %(levelname)s: %(message)s', level=logging.DEBUG ) spin = Engine(MemoryBroker()) @spin.task(name='fast', queue='high-priority') def fast(): time.sleep(1) @spin.task(name='slow', queue='low-priority') def slow(): time.sleep(10) spin.schedule(slow) spin.schedule(fast) spin.start_workers(number=1, queue='high-priority', stop_when_queue_empty=True)
from spinach import Engine, MemoryBroker spin = Engine(MemoryBroker()) @spin.task(name='compute') def compute(a, b): print('Computed {} + {} = {}'.format(a, b, a + b)) # Schedule a job to be executed ASAP spin.schedule(compute, 5, 3) print('Starting workers, ^C to quit') spin.start_workers()