-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcron_worker.py
49 lines (39 loc) · 1.31 KB
/
cron_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
40
41
42
43
44
45
46
47
48
49
# Setup Django settings and models
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "acacia_main.settings")
import django
django.setup()
# Begin program
import time
import threading
from apscheduler.schedulers.background import BackgroundScheduler
import logging
logging.basicConfig()
from prices.interface import prices_append
from exchanges.views import EXCHANGE_APIS
from billing.collect import batch_update_customers
def batch_fetch_prices():
print "fetching prices..."
for exchange_name, exchange in EXCHANGE_APIS.items():
for pair in exchange.PAIRS:
thr = threading.Thread(
target=store_price,
args=(exchange, exchange_name, pair,),
kwargs={}
)
thr.start()
def store_price(exchange, exchange_name, pair):
price = exchange.pair_ticker(pair).bid
prices_append(exchange_name, pair, price)
# http://goo.gl/ZqzB49
scheduler = BackgroundScheduler()
scheduler.add_job(batch_fetch_prices, 'interval', seconds=60)
scheduler.add_job(batch_update_customers, 'interval', hours=6)
scheduler.start()
try:
# keeps the main thread alive
while True:
time.sleep(2)
except (KeyboardInterrupt, SystemExit):
# Not strictly necessary but should be done if possible
scheduler.shutdown()