-
Notifications
You must be signed in to change notification settings - Fork 28
/
run.py
202 lines (172 loc) · 6.21 KB
/
run.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env python3
import importlib
import logging
import pkgutil
import sys
import time
from types import ModuleType
from typing import Any
import click
from traceback_with_variables import activate_by_import # noqa: F401
from shared import configuration, decorators, repo, sentry
logging.basicConfig(level=logging.INFO)
def wait_for_db(_: Any, __: Any, value: bool) -> None:
if not value:
return
print('waiting for db')
def attempt(interval: int = 1) -> bool:
from shared import database, pd_exception
try:
database.get_database(configuration.get_str('magic_database'))
return True
except pd_exception.DatabaseConnectionRefusedException:
print(f'DB not accepting connections. Sleeping for {interval}.')
time.sleep(interval)
return False
i = 1
while not attempt(i) and i < 60:
i = i + 1
@click.group()
@click.option('--wait-for-db', is_flag=True, callback=wait_for_db, expose_value=False, help='Idle until the mySQL server starts accepting connections')
def cli() -> None:
pass
@cli.command()
def discordbot() -> None:
from discordbot import bot
bot.init()
@cli.command()
def decksite() -> None:
from decksite import main
main.init(port=configuration.get_int('decksite_port'))
@cli.command()
def profiler() -> None:
from werkzeug.middleware.profiler import ProfilerMiddleware
from decksite import main
main.APP.config['PROFILE'] = True
main.APP.wsgi_app = ProfilerMiddleware(main.APP.wsgi_app, restrictions=[30]) # type: ignore
main.init(port=configuration.get_int('decksite_port'))
@cli.command()
def price_grabber() -> None:
from price_grabber import price_grabber as grabber
sentry.init()
grabber.run()
@cli.command()
def srv_price() -> None:
from price_grabber import srv_prices
srv_prices.init()
@cli.command()
@click.argument('source')
def scraper(source: str) -> None:
task(['scraper', source])
@cli.command()
@click.argument('eventname')
def scrape_event(eventname: str) -> None:
task(['scraper', 'gatherling', eventname])
@cli.command()
@click.argument('script')
def maintenance(script: str) -> None:
task(['maintenance', script])
@cli.command()
def rotation() -> None:
from rotation_script import rotation_script
sentry.init()
rotation_script.run()
@cli.command()
def logsite() -> None:
import logsite as site
site.APP.run(host='0.0.0.0', port=5001, debug=True) # type: ignore
@cli.command()
@click.argument('argv', nargs=-1)
def modo_bugs(argv: tuple[str]) -> None:
try:
from modo_bugs import main
sentry.init()
main.run(argv)
except Exception as e:
repo.create_issue(f'Exception running modo_bugs: {e}', 'modo-bugs', 'run.py', 'PennyDreadfulMTG/perf-reports', e)
@cli.command()
@click.option('--force', is_flag=True, help='Force a rebuild of the database')
def init_cards(force: bool = False) -> None:
from magic import multiverse
success = multiverse.init(force=force)
multiverse.rebuild_cache()
sys.exit(0 if success else 1)
@decorators.interprocess_locked('.task.lock')
def task(args: list[str]) -> None:
try:
module = args[0]
if module == 'scraper':
module = 'scrapers'
if module == 'scrapers':
module = 'decksite.scrapers'
name = args[1].replace('-', '_')
sentry.init()
from magic import multiverse, oracle
multiverse.init()
if name != 'reprime_cache':
oracle.init()
if name == 'all':
run_all_tasks(module)
elif name == 'hourly':
run_all_tasks(module, 'HOURLY')
elif name == 'daily':
run_all_tasks(module, 'DAILY')
elif name == 'weekly':
run_all_tasks(module, 'WEEKLY')
else:
s = importlib.import_module(f'{module}.{name}')
use_app_context = getattr(s, 'REQUIRES_APP_CONTEXT', True)
exitcode = None
if use_app_context:
from decksite.main import APP
APP.logger.setLevel(logging.INFO) # Override app setting of WARN.
APP.config['SERVER_NAME'] = configuration.server_name()
with APP.app_context():
exitcode = call(args, s)
else:
exitcode = call(args, s)
if exitcode is not None:
sys.exit(exitcode)
except Exception as c:
from shared import repo
repo.create_issue(f'Error running task {args}', 'CLI', 'CLI', 'PennyDreadfulMTG/perf-reports', exception=c)
raise
def call(args: list[str], s: ModuleType) -> int:
exitcode = -99
if getattr(s, 'scrape', None) is not None:
exitcode = s.scrape(*args[2:])
elif getattr(s, 'run', None) is not None:
exitcode = s.run()
# Only when called directly, not in 'all'
elif getattr(s, 'ad_hoc', None) is not None:
exitcode = s.ad_hoc()
return exitcode
def run_all_tasks(module: Any, with_flag: str | None = None) -> None:
error = None
m = importlib.import_module(f'{module}')
from decksite import APP
APP.config['SERVER_NAME'] = configuration.server_name()
with APP.app_context():
for _importer, modname, _ispkg in pkgutil.iter_modules(m.__path__):
try:
s = importlib.import_module(f'{module}.{modname}')
if with_flag and not getattr(s, with_flag, False):
continue
if getattr(s, 'scrape', None) is not None:
timer = time.perf_counter()
s.scrape()
t = time.perf_counter() - timer
print(f'{s.__name__} completed in {t}')
elif getattr(s, 'run', None) is not None:
timer = time.perf_counter()
s.run()
t = time.perf_counter() - timer
print(f'{s.__name__} completed in {t}')
except Exception as c:
from shared import repo
repo.create_issue(f'Error running task {s.__name__}', 'CLI', 'CLI', 'PennyDreadfulMTG/perf-reports', exception=c)
error = c
if error:
raise error
if __name__ == '__main__':
cli()