-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunscript.py
More file actions
116 lines (97 loc) · 3.72 KB
/
Copy pathrunscript.py
File metadata and controls
116 lines (97 loc) · 3.72 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from threading import Thread
from time import sleep
from src.models import Utilities as Utils
from src.controllers import ApplicationController as App
from src.views import Logger
from src.models import Emails
import pid
import os, pwd
import traceback
is_active = True
def start(timer, user):
timer.start()
user.start()
join(timer, user)
def join(timer, user):
timer.join()
user.join()
def countdown(count):
global is_active
while is_active:
m, s = divmod(count, 60)
min_sec_format = "{:02d}:{:02d}".format(m, s)
print(min_sec_format, end="\r")
sleep(1)
count -= 1
if count < 0:
is_active = False
auto_run_program()
break
def await_user_input():
global is_active
while is_active:
input("")
is_active = False
manual_run_program()
break
def manual_run_program():
print("\n -- Manual run initiated -- \n")
user_in = input("\n Enter number of weeks or 'p' for preview: ")
try:
user_in = int(user_in)
start_date = Utils.set_search_start_date(user_in)
App.run_application(start_date)
except:
if user_in == "p" or user_in == "P":
user_in = int(input("\n Enter number of weeks for preview: "))
start_date = Utils.set_search_start_date(user_in)
App.run_preview(start_date)
def auto_run_program():
print("\n >> Automatic run initiated \n")
start_date = Utils.set_search_start_date(14)
App.run_application(start_date)
if __name__ == "__main__":
global LOGGER
try:
LOGGER = Logger.init_logger(__name__)
LOGGER.info("Launching application")
pidfile = None
try:
username = pwd.getpwuid(os.getuid()).pw_name
pid_file_name = "runscript.py.pid"
pid_dir = f"/home/{username}/tmp/"
pid_file_path = os.path.join(pid_dir, pid_file_name)
# Manual overrun. Without this, PidFile will create a new pidfile even if one exists but that process isn't running
# We want to raise an exception in this case because it means an ungraceful crash happened
if os.path.isfile(pid_file_path):
pid_number = str(open(pid_file_path, "r").readline().strip())
try:
# Don't actually kill process, just check if it's there
os.kill(int(pid_number), 0)
except ProcessLookupError:
# Process is terminated
raise Exception(
f"PID File found but no process to claim that PID. This program has previously had an ungraceful crash. PID File location: {pid_file_path}"
)
pidfile = pid.PidFile(
piddir=pid_dir,
pidname=pid_file_name,
register_atexit=False,
register_term_signal_handler=None,
)
with pidfile:
print("\n -- PRESS ENTER FOR MANUAL RUN -- \n")
timer = Thread(target=countdown, args=(5,), daemon=True)
user = Thread(target=await_user_input, daemon=True)
start(timer, user)
except pid.PidFileError as e:
pid_fname = pidfile.filename
pid_number = str(open(pid_fname, "r").readline().strip())
msg = f"An instance of this program is already running with PID {pid_number}. The locking file is at {pid_fname}"
LOGGER.critical(msg)
raise pid.PidFileAlreadyLockedError(msg)
except Exception as e:
trace_back = traceback.format_exc()
message = Emails.create_alert_message(trace_back)
Emails.send_alert_email(message)
raise e