regluit/core/management/commands/random_tasks.py

59 lines
1.9 KiB
Python
Raw Normal View History

"""
a command that creates a given number of random tasks to test out celery
"""
2013-06-03 16:31:39 +00:00
import random
from django.core.management.base import BaseCommand
2013-06-03 16:31:39 +00:00
from regluit.core import tasks
from regluit.core.models import CeleryTask
random.seed()
class Command(BaseCommand):
help = "create random tasks"
args = "<num_tasks action>"
def handle(self, num_tasks, action, **options):
"""
actions:
c: create num_tasks tasks
s: print state of existing tasks
d: delete all tasks
an integer: compute factorial of the integer -- can then follow up with s to find the state
"""
import django
django.db.transaction.enter_transaction_management()
if action == 'c':
for i in xrange(int(num_tasks)):
n = random.randint(1,1000)
task_id = tasks.fac.delay(n)
ct = CeleryTask()
ct.task_id = task_id
ct.function_name = 'fac'
ct.function_args = n
2011-11-10 17:33:22 +00:00
ct.description = "Factorial of %d" % (n)
ct.save()
elif action == 's':
for (i, ct) in enumerate(CeleryTask.objects.all()):
2011-11-10 17:33:22 +00:00
print i, ct.function_args, ct.state, ct.info
elif action == 'd':
CeleryTask.objects.all().delete()
else:
try:
action = int(action)
print 'action: %d' % (int(action))
2011-11-10 17:33:22 +00:00
task_id = tasks.fac.delay(int(action),sleep_interval=0.001)
ct = CeleryTask()
ct.task_id = task_id
ct.function_name = 'fac'
ct.function_args = action
2011-11-10 17:33:22 +00:00
ct.description = "Factorial of %d" % (action)
ct.save()
except Exception, e:
print e
django.db.transaction.commit()