-
Notifications
You must be signed in to change notification settings - Fork 11
/
escalatorApp.py
84 lines (66 loc) · 2.32 KB
/
escalatorApp.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
"""
apps.runEscalatorApp
# This runs the escalatorApp instance.
# This app downloads escalator data from the WMATA API,
# stores escalator statuses in the database, and
# generates tweets for @MetroEscalators.
# This module can be executed directly for local testing
"""
if __name__ == "__main__":
# Local Testing
import test.setup
import os
import sys
import subprocess
from datetime import datetime
import gevent
from gevent import Greenlet
from dcmetrometrics.common.restartingGreenlet import RestartingGreenlet
from dcmetrometrics.eles.EscalatorApp import EscalatorApp as App
from dcmetrometrics.common.globals import DATA_DIR, REPO_DIR
OUTPUT_DIR = DATA_DIR
if OUTPUT_DIR is None:
OUTPUT_DIR = os.getcwd()
if REPO_DIR is None:
SCRIPT_DIR = os.getcwd()
else:
SCRIPT_DIR = os.path.join(REPO_DIR, 'scripts')
SLEEP = 30
##########################################
# Run the Twitter App as a Greenlet.
class EscalatorApp(RestartingGreenlet):
def __init__(self, SLEEP=SLEEP, LIVE=False):
RestartingGreenlet.__init__(self, SLEEP=SLEEP, LIVE=LIVE)
self.LIVE = LIVE # Tweet only if Live
self.SLEEP = SLEEP # Sleep time after each tick
self.logFileName = os.path.join(DATA_DIR, 'runEscalatorApp.log')
def _run(self):
while True:
try:
self.tick()
except Exception as e:
import traceback
logFile = open(self.logFileName, 'a')
logFile.write('EscalatorApp caught Exception: %s\n'%(str(e)))
tb = traceback.format_exc()
logFile.write('Traceback:\n%s\n\n'%tb)
logFile.close()
gevent.sleep(self.SLEEP)
def tick(self):
# Run MetroEsclaators twitter App
with open(self.logFileName, 'a') as logFile:
n = datetime.now()
timeStr = n.strftime('%d-%B-%Y %H:%M:%S')
msg = '*'*50 + '\n'
msg += '%s Escalator App Tick\n'%timeStr
msg += 'App Mode: %s\n'%('LIVE' if self.LIVE else 'NOT LIVE')
logFile.write(msg)
logFile.flush()
app = App(logFile, LIVE=self.LIVE)
app.tick()
if __name__ == "__main__":
print 'Running the escalator app locally....'
escApp = EscalatorApp()
escApp.start()
escApp.join()