-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
run.py
104 lines (89 loc) · 3.74 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
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
from backend.api.tasks import scheduler, update_playlists, map_new_tvh_services, update_epgs, rebuild_custom_epg, \
update_tvh_muxes, configure_tvh_with_defaults, update_tvh_channels, update_tvh_networks, update_tvh_epg, \
TaskQueueBroker
from backend import create_app, config
import asyncio
# Create app
app = create_app()
if config.enable_app_debugging:
app.logger.info(' DEBUGGING = ' + str(config.enable_app_debugging))
app.logger.debug('DBMS = ' + config.sqlalchemy_database_uri)
app.logger.debug('ASSETS_ROOT = ' + config.assets_root)
task_logger = app.logger.getChild('tasks')
TaskQueueBroker.initialize(task_logger)
@scheduler.scheduled_job('interval', id='background_tasks', seconds=10)
async def background_tasks():
async with app.app_context():
task_broker = await TaskQueueBroker.get_instance()
await task_broker.execute_tasks()
@scheduler.scheduled_job('interval', id='do_5_mins', minutes=5, misfire_grace_time=60)
async def every_5_mins():
async with app.app_context():
task_broker = await TaskQueueBroker.get_instance()
await task_broker.add_task({
'name': 'Mapping all TVH services',
'function': map_new_tvh_services,
'args': [app],
}, priority=10)
@scheduler.scheduled_job('interval', id='do_60_mins', minutes=60, misfire_grace_time=300)
async def every_60_mins():
async with app.app_context():
task_broker = await TaskQueueBroker.get_instance()
await task_broker.add_task({
'name': 'Configuring TVH with global default',
'function': configure_tvh_with_defaults,
'args': [app],
}, priority=11)
await task_broker.add_task({
'name': 'Configuring TVH networks',
'function': update_tvh_networks,
'args': [app],
}, priority=12)
await task_broker.add_task({
'name': 'Configuring TVH channels',
'function': update_tvh_channels,
'args': [app],
}, priority=13)
await task_broker.add_task({
'name': 'Configuring TVH muxes',
'function': update_tvh_muxes,
'args': [app],
}, priority=14)
await task_broker.add_task({
'name': 'Triggering an update in TVH to fetch the latest XMLTV',
'function': update_tvh_epg,
'args': [app],
}, priority=30)
@scheduler.scheduled_job('cron', id='do_job_twice_a_day', hour='0/12', minute=1, misfire_grace_time=900)
async def every_12_hours():
async with app.app_context():
task_broker = await TaskQueueBroker.get_instance()
await task_broker.add_task({
'name': f'Updating all playlists',
'function': update_playlists,
'args': [app],
}, priority=100)
await task_broker.add_task({
'name': f'Updating all EPGs',
'function': update_epgs,
'args': [app],
}, priority=100)
await task_broker.add_task({
'name': 'Recreating static XMLTV file',
'function': rebuild_custom_epg,
'args': [app],
}, priority=200)
if __name__ == "__main__":
# Create a custom loop
loop = asyncio.get_event_loop()
# Start scheduler
app.logger.info("Starting scheduler...")
scheduler.start()
app.logger.info("Scheduler started.")
# Start Quart server
app.logger.info("Starting Quart server...")
app.run(loop=loop, host=config.flask_run_host, port=config.flask_run_port,
debug=config.enable_app_debugging, use_reloader=config.enable_app_debugging)
app.logger.info("Quart server completed.")