-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.py
executable file
·39 lines (33 loc) · 1.31 KB
/
worker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python
import os
import redis
import schedule
import time
if __name__ == '__main__':
from django import setup
from django.conf import settings
from django.core.management import call_command
from django.utils import timezone
# initialize Django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'alby.settings')
setup()
# schedule jobs
schedule.every().minute.do(call_command, 'send_queued_mail')
rebuild_at = timezone.now() + timezone.timedelta(minutes=6)
schedule.every().hour.at(rebuild_at.strftime(':%M')).do(call_command, 'rebuild_index', interactive=False)
schedule.every().sunday.do(call_command, 'shopcustomers', delete_expired=True)
if hasattr(settings, 'SESSION_REDIS'):
redis_con = dict((key, settings.SESSION_REDIS[key]) for key in ['host', 'port', 'db', 'socket_timeout'])
pool = redis.ConnectionPool(**redis_con)
r = redis.Redis(connection_pool=pool)
pubsub = r.pubsub()
pubsub.subscribe('django-SHOP')
else:
pubsub = type(str('PubSub'), (), {'get_message': lambda s: None})()
while True:
message = pubsub.get_message()
if message:
if message['data'] == 'send_queued_mail':
call_command('send_queued_mail')
schedule.run_pending()
time.sleep(1)