Esempio n. 1
0
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}
Esempio n. 2
0
def test_execute(spin):
    func = Mock()
    tasks = Tasks()
    tasks.add(func, 'foo_task')
    spin.attach_tasks(tasks)

    spin.execute('foo_task')
    func.assert_called_once_with()
Esempio n. 3
0
def test_attach_tasks(mock_logger, spin, spin_2):
    tasks = Tasks()
    tasks.add(print, 'foo_task')

    spin.attach_tasks(tasks)
    mock_logger.warning.assert_not_called()
    assert tasks._spin is spin
    assert spin._tasks.tasks == tasks.tasks

    spin.attach_tasks(tasks)
    mock_logger.warning.assert_not_called()
    assert tasks._spin is spin
    assert spin._tasks.tasks == tasks.tasks

    spin_2.attach_tasks(tasks)
    mock_logger.warning.assert_called_once_with(ANY)
    assert tasks._spin is spin_2
    assert spin_2._tasks.tasks == tasks.tasks
Esempio n. 4
0
def test_flask_extension_app_factory(app):
    spinach = Spinach()
    tasks = Tasks()

    @tasks.task(name='foo')
    def foo():
        return 'foo'

    spinach.init_app(app)
    spinach.register_tasks(app, tasks)

    with app.app_context():
        assert spinach.execute('foo') == 'foo'
Esempio n. 5
0
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()
Esempio n. 6
0
from collections import defaultdict
from datetime import datetime, timedelta

import pytz
from spinach import Tasks

from ..settings import NOTEBOOK_REVISION_SAVE_INTERVAL_SECS
from .models import NotebookRevision

tasks = Tasks()


@tasks.task(name="notebooks:execute_notebook_revisions_cleanup")
def execute_notebook_revisions_cleanup(notebook_id, now_utc=None):
    """Prune revision hisotry.

    Prune revision history so that there is only one revision in each
    time window, also marking draft revisions that older than window length
    as non-draft. See also: "tumbling windows"

    * Time window: This task groups draft revisions into fixed-size windows
      (also called Tumbling windows).
    """
    draft_revisions = NotebookRevision.objects.filter(notebook_id=notebook_id, is_draft=True)

    # group notebook revisions into fixed-size windows
    revision_groups = defaultdict(list)
    for revision in draft_revisions:
        key = int(revision.created.timestamp()) // NOTEBOOK_REVISION_SAVE_INTERVAL_SECS
        revision_groups[key].append(revision)
    revision_windows = [revision_groups[k] for k in sorted(revision_groups.keys(), reverse=True)]