mirror of
https://github.com/django-q2/django-q2.git
synced 2026-10-06 06:48:12 +08:00
Added schedule command
* improved schedule tests * updated README * bumped up to version 0.1.3
This commit is contained in:
@@ -3,22 +3,29 @@ def countdown(n):
|
||||
while n > 0:
|
||||
n -= 1
|
||||
|
||||
|
||||
def multiply(x, y):
|
||||
return x * y
|
||||
|
||||
|
||||
def count_letters(tup):
|
||||
total = 0
|
||||
for word in tup:
|
||||
total += len(word)
|
||||
return total
|
||||
|
||||
|
||||
def count_letters2(obj):
|
||||
return count_letters(obj.get_words())
|
||||
|
||||
|
||||
def word_multiply(x, word=''):
|
||||
return len(word) * x
|
||||
|
||||
|
||||
def get_task_name(task):
|
||||
return task.name
|
||||
|
||||
|
||||
def result(obj):
|
||||
print('RESULT HOOK {} : {}'.format(obj.name, obj.result))
|
||||
|
||||
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
import pytest
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_admin_view(admin_client):
|
||||
response = admin_client.get('/admin/django_q/')
|
||||
assert response.status_code == 200
|
||||
|
||||
@@ -106,6 +106,8 @@ def test_async(r):
|
||||
f = async(multiply, 753, 2, hook=assert_result, list_key=list_key)
|
||||
# model as argument
|
||||
g = async('django_q.tests.tasks.get_task_name', Task(name='John'), list_key=list_key)
|
||||
# args and kwargs
|
||||
h = async('django_q.tests.tasks.word_multiply', 2, word='django', list_key=list_key, redis=r)
|
||||
# check if everything has a task name
|
||||
assert isinstance(a, str)
|
||||
assert isinstance(b, str)
|
||||
@@ -114,8 +116,9 @@ def test_async(r):
|
||||
assert isinstance(e, str)
|
||||
assert isinstance(f, str)
|
||||
assert isinstance(g, str)
|
||||
assert isinstance(h, str)
|
||||
# run the cluster to execute the tasks
|
||||
task_count = 7
|
||||
task_count = 8
|
||||
assert r.llen(list_key) == task_count
|
||||
task_queue = Queue()
|
||||
stop_event = Event()
|
||||
@@ -168,6 +171,11 @@ def test_async(r):
|
||||
assert result_g is not None
|
||||
assert result_g.success is True
|
||||
assert result(g) == 'John'
|
||||
# task h
|
||||
result_h = get_task(h)
|
||||
assert result_h is not None
|
||||
assert result_h.success is True
|
||||
assert result(h) == 12
|
||||
r.delete(list_key)
|
||||
|
||||
|
||||
|
||||
@@ -1,22 +1,25 @@
|
||||
from multiprocessing import Queue, Event
|
||||
|
||||
import pytest
|
||||
from django_q.core import scheduler, pusher, worker, monitor, redis_client
|
||||
|
||||
from django_q.core import scheduler, pusher, worker, monitor, redis_client, schedule as create_schedule
|
||||
from django_q import Schedule, get_task
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def r():
|
||||
return redis_client
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_scheduler(r):
|
||||
list_key = 'scheduler_test:q'
|
||||
r.delete(list_key)
|
||||
schedule = Schedule.objects.create(func='math.copysign',
|
||||
args='1, -1',
|
||||
schedule_type=Schedule.ONCE,
|
||||
repeats=1,
|
||||
hook='django_q.tests.tasks.result'
|
||||
)
|
||||
schedule = create_schedule('math.copysign',
|
||||
1, -1,
|
||||
hook='django_q.tests.tasks.result',
|
||||
schedule_type=Schedule.HOURLY,
|
||||
repeats=1)
|
||||
assert schedule.last_run() is None
|
||||
# run scheduler
|
||||
scheduler(list_key=list_key)
|
||||
@@ -39,8 +42,19 @@ def test_scheduler(r):
|
||||
assert result_queue.qsize() == 0
|
||||
schedule.refresh_from_db()
|
||||
assert schedule.repeats == 0
|
||||
assert schedule.last_run() is not None
|
||||
assert schedule.success() is True
|
||||
task = get_task(schedule.task)
|
||||
assert task is not None
|
||||
assert task.success is True
|
||||
assert task.result < 0
|
||||
for t in Schedule.TYPE:
|
||||
schedule = create_schedule('django_q.tests.tasks.word_multiply',
|
||||
2,
|
||||
word='django',
|
||||
schedule_type=t[0],
|
||||
repeats=1,
|
||||
hook='django_q.tests.tasks.result'
|
||||
)
|
||||
assert schedule is not None
|
||||
assert schedule.last_run() is None
|
||||
|
||||
Reference in New Issue
Block a user