michaelkri commited on
Commit
a2d105f
·
1 Parent(s): 04564e0

Update news at set hours

Browse files
Files changed (1) hide show
  1. app/main.py +10 -11
app/main.py CHANGED
@@ -12,7 +12,7 @@ from .database import get_session, retrieve_articles, Article
12
  from .update_news import update_news
13
 
14
 
15
- DEFAULT_UPDATE_INTERVAL = 6 # in hours
16
 
17
  if os.getenv('DEBUG') == 'true':
18
  root_logger = logging.getLogger()
@@ -20,15 +20,7 @@ if os.getenv('DEBUG') == 'true':
20
 
21
 
22
  # set update interval from environment variable if available
23
- try:
24
- update_interval_str = os.getenv('UPDATE_INTERVAL')
25
- if update_interval_str is not None:
26
- update_interval = int(update_interval_str)
27
- else:
28
- update_interval = DEFAULT_UPDATE_INTERVAL
29
- except (ValueError, TypeError):
30
- update_interval = DEFAULT_UPDATE_INTERVAL
31
-
32
 
33
  # for updating the news feed periodically
34
  scheduler = BackgroundScheduler()
@@ -51,7 +43,14 @@ def safe_update_news():
51
  @asynccontextmanager
52
  async def lifespan(app: FastAPI):
53
  # update news periodically
54
- scheduler.add_job(safe_update_news, 'interval', hours=update_interval)
 
 
 
 
 
 
 
55
  scheduler.start()
56
  yield
57
  # stop the scheduler when closing the server
 
12
  from .update_news import update_news
13
 
14
 
15
+ DEFAULT_UPDATE_HOURS = '0,6,12,18' # UTC time
16
 
17
  if os.getenv('DEBUG') == 'true':
18
  root_logger = logging.getLogger()
 
20
 
21
 
22
  # set update interval from environment variable if available
23
+ update_schedule_hours = os.environ.get('UPDATE_HOURS', DEFAULT_UPDATE_HOURS)
 
 
 
 
 
 
 
 
24
 
25
  # for updating the news feed periodically
26
  scheduler = BackgroundScheduler()
 
43
  @asynccontextmanager
44
  async def lifespan(app: FastAPI):
45
  # update news periodically
46
+ scheduler.add_job(
47
+ safe_update_news,
48
+ 'cron',
49
+ hour=update_schedule_hours,
50
+ minute=0,
51
+ id='update_task',
52
+ replace_existing=True
53
+ )
54
  scheduler.start()
55
  yield
56
  # stop the scheduler when closing the server