-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduler.py
More file actions
47 lines (36 loc) · 1.05 KB
/
scheduler.py
File metadata and controls
47 lines (36 loc) · 1.05 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
"""
Scheduler to run the bot periodically.
"""
import schedule
import time
import logging
from bot import ChargersNewsBot
from config import CHECK_INTERVAL_HOURS, DEBUG
logging.basicConfig(
level=logging.DEBUG if DEBUG else logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
def run_bot():
"""Run the bot once."""
try:
bot = ChargersNewsBot()
bot.run()
except Exception as e:
logger.error(f"Error running bot: {e}")
def main():
"""Schedule and run the bot periodically."""
logger.info(f"Starting scheduler - checking every {CHECK_INTERVAL_HOURS} hours")
# Run immediately on start
run_bot()
# Schedule periodic runs
schedule.every(CHECK_INTERVAL_HOURS).hours.do(run_bot)
# Keep the script running
while True:
schedule.run_pending()
time.sleep(60) # Check every minute
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
logger.info("Scheduler stopped by user")