time_for_next_iteration_in_seconds = 60 * 5
@app.task(bind=True)
def assign_user(self, hangout_id):
hangout = Hangout.objects.get(id=hangout_id)
if hangout.has_claimed:
app.control.revoke(task_id=self.request.id)
total_tickets = HangoutSubscription.objects.filter(
hangout=hangout, is_processed=False).aggregate(
total_tickets=Sum('entries'))['total_tickets']
if total_tickets > 0:
winner_ticket = random.randint(1, total_tickets)
user = find_alloted_user(hangout, winner_ticket)
alloted_user = ChampUser.objects.get(id=user[1])
# send mail to allotted user
content, subject = allotment_email_content_subject(
alloted_user.id, hangout.id)
Thread(target=send_html_email, args=(
[alloted_user.email], subject, content)).start()
job = assign_user2.apply_async(
(hangout_id, ), countdown=time_for_next_iteration_in_seconds,
expires=time_for_next_iteration_in_seconds + 60)
Hangout.objects.filter(id=hangout_id).update(
celery_task_id=job.task_id)
HangoutAllotmentHistory(
user=alloted_user, hangout=hangout,
celery_task_id=self.request.id).save()
else:
Hangout.objects.filter(id=hangout.id).update(is_active=False)
app.control.revoke(task_id=self.request.id)
@app.task(bind=True)
def assign_user2(self, hangout_id):
hangout = Hangout.objects.get(id=hangout_id)
if hangout.has_claimed:
app.control.revoke(task_id=self.request.id)
else:
failed_users = HangoutAllotmentHistory.objects.filter(
hangout=hangout).values_list('user_id', flat=True)
HangoutSubscription.objects.filter(
hangout=hangout, user_id__in=failed_users).update(is_processed=True)
total_tickets = HangoutSubscription.objects.filter(
hangout=hangout, is_processed=False).aggregate(
total_tickets=Sum('entries'))['total_tickets']
if total_tickets > 0:
winner_ticket = random.randint(1, total_tickets)
user = find_alloted_user(hangout, winner_ticket)
alloted_user = ChampUser.objects.get(id=user[1])
# send mail to allotted user
content, subject = allotment_email_content_subject(
alloted_user.id, hangout.id)
> Blockquote
Thread(target=send_html_email, args=(
[alloted_user.email], subject, content)).start()
job = assign_user3.apply_async(
(hangout_id, ), countdown=time_for_next_iteration_in_seconds,
expires=time_for_next_iteration_in_seconds + 60)
Hangout.objects.filter(id=hangout_id).update(
celery_task_id=job.task_id)
HangoutAllotmentHistory(
user=alloted_user, hangout=hangout,
celery_task_id=self.request.id).save()
else:
Hangout.objects.filter(id=hangout.id).update(is_active=False)
app.control.revoke(task_id=self.request.id)
@app.task(bind=True)
def assign_user3(self, hangout_id):
hangout = Hangout.objects.get(id=hangout_id)
if hangout.has_claimed:
app.control.revoke(task_id=self.request.id)
else:
failed_users = HangoutAllotmentHistory.objects.filter(
hangout=hangout).values_list('user_id', flat=True)
HangoutSubscription.objects.filter(
hangout=hangout, user_id__in=failed_users).update(is_processed=True)
total_tickets = HangoutSubscription.objects.filter(
hangout=hangout, is_processed=False).aggregate(
total_tickets=Sum('entries'))['total_tickets']
if total_tickets > 0:
winner_ticket = random.randint(1, total_tickets)
user = find_alloted_user(hangout, winner_ticket)
alloted_user = ChampUser.objects.get(id=user[1])
# send mail to allotted user
content, subject = allotment_email_content_subject(
alloted_user.id, hangout.id)
Thread(target=send_html_email, args=(
[alloted_user.email], subject, content)).start()
HangoutAllotmentHistory(
user=alloted_user, hangout=hangout,
celery_task_id=self.request.id).save()
else:
Hangout.objects.filter(id=hangout.id).update(is_active=False)
app.control.revoke(task_id=self.request.id)
使用clery创建了3个任务。但它在当地正常运作。但是在服务器中它没有按预期工作。
主管conf:正如https://thomassileo.name/blog/2012/08/20/how-to-keep-celery-running-with-supervisor/
所建议的那样[program:champ]
command=/home/qa/prj/venv/bin/celery -A myworker worker -l info
stdout_logfile=/home/qa/celeryd.log
stderr_logfile=/home/qa/celeryd.log
autostart=true
autorestart=true
startsecs=10
stopwaitsecs=600
我已经通过pip及其版本在3.3.0中安装了supervisor
我的问题是使用 supervisorctl restart champ 重新加载芹菜不能正常工作。但是命令不会抛出任何错误。 (在虚拟环境中)。我究竟做错了什么。我在Django使用芹菜。
我的celery.py
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')
from django.conf import settings # noqa
app = Celery('myworker', broker="redis://localhost:6379")
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))