| 15176 |
kshitij.so |
1 |
from shop2020.utils.daemon import Daemon
|
| 15216 |
kshitij.so |
2 |
from dtr.utils.utils import to_java_date
|
|
|
3 |
from dtr.utils.FetchLivePrices import get_mongo_connection, updatePriceForNotificationBundles
|
| 15176 |
kshitij.so |
4 |
import optparse
|
|
|
5 |
import sys
|
|
|
6 |
from datetime import datetime
|
|
|
7 |
from time import sleep
|
|
|
8 |
|
|
|
9 |
parser = optparse.OptionParser()
|
|
|
10 |
parser.add_option("-l", "--logfile", dest="logfile",
|
|
|
11 |
type="string",
|
| 15177 |
kshitij.so |
12 |
help="Log all output to LOG_FILE"
|
| 15176 |
kshitij.so |
13 |
)
|
|
|
14 |
parser.add_option("-i", "--pidfile", dest="pidfile",
|
|
|
15 |
type="string",
|
|
|
16 |
help="Write the PID to pidfile")
|
|
|
17 |
(options, args) = parser.parse_args()
|
|
|
18 |
|
|
|
19 |
class NotificationDaemon(Daemon):
|
| 15178 |
kshitij.so |
20 |
def __init__(self, logfile='/var/log/services/NotificationDaemon.log', pidfile='/var/run/NotificationDaemon.pid'):
|
| 15176 |
kshitij.so |
21 |
Daemon.__init__(self, pidfile, stdout=logfile, stderr=logfile)
|
|
|
22 |
|
|
|
23 |
def run(self):
|
| 15177 |
kshitij.so |
24 |
start()
|
| 15176 |
kshitij.so |
25 |
|
|
|
26 |
def start():
|
|
|
27 |
while True:
|
|
|
28 |
try:
|
|
|
29 |
notificationBundles = list(get_mongo_connection().Catalog.Notifications.find({'startDate':{'$lte':to_java_date(datetime.now())},'endDate':{'$gte':to_java_date(datetime.now())}}))
|
|
|
30 |
for notificationBundle in notificationBundles:
|
|
|
31 |
print notificationBundle['skuBundleId']
|
|
|
32 |
updatePriceForNotificationBundles(notificationBundle['skuBundleId'])
|
|
|
33 |
print "==============SLEEPING============================"
|
|
|
34 |
sleep(120)
|
|
|
35 |
except Exception as e:
|
|
|
36 |
print e
|
|
|
37 |
|
|
|
38 |
|
|
|
39 |
if __name__ == "__main__":
|
|
|
40 |
daemon = NotificationDaemon(options.logfile, options.pidfile)
|
|
|
41 |
if len(args) == 0:
|
|
|
42 |
daemon.run()
|
|
|
43 |
elif len(args) == 1:
|
|
|
44 |
if 'start' == args[0]:
|
|
|
45 |
daemon.start()
|
|
|
46 |
elif 'stop' == args[0]:
|
|
|
47 |
daemon.stop()
|
|
|
48 |
elif 'restart' == args[0]:
|
|
|
49 |
daemon.restart()
|
|
|
50 |
else:
|
|
|
51 |
print "Unknown command"
|
|
|
52 |
sys.exit(2)
|
|
|
53 |
sys.exit(0)
|
|
|
54 |
else:
|
|
|
55 |
print "usage: %s start|stop|restart" % sys.argv[0]
|
|
|
56 |
sys.exit(2)
|