fix scheduler: use apscheduler instead of schedule
This commit is contained in:
@@ -4,9 +4,9 @@ Scheduler script for Flat Scraper
|
|||||||
Runs every 6 hours and sends notifications for new results
|
Runs every 6 hours and sends notifications for new results
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import schedule
|
|
||||||
import time
|
import time
|
||||||
import logging
|
import logging
|
||||||
|
from apscheduler.schedulers.background import BackgroundScheduler
|
||||||
from src.main import FlatScraper
|
from src.main import FlatScraper
|
||||||
|
|
||||||
# Configure logging
|
# Configure logging
|
||||||
@@ -28,24 +28,30 @@ def main():
|
|||||||
print('🕐 Scheduler started. Running every 6 hours.')
|
print('🕐 Scheduler started. Running every 6 hours.')
|
||||||
print('🏠 Flat Scraper will check for new apartments automatically.')
|
print('🏠 Flat Scraper will check for new apartments automatically.')
|
||||||
|
|
||||||
|
# Create scheduler
|
||||||
|
scheduler = BackgroundScheduler()
|
||||||
|
|
||||||
# Schedule every 6 hours
|
# Schedule every 6 hours
|
||||||
schedule.every(6).hours.do(run_scraper)
|
scheduler.add_job(run_scraper, 'interval', hours=6)
|
||||||
|
|
||||||
|
# Start scheduler
|
||||||
|
scheduler.start()
|
||||||
|
|
||||||
# Run immediately on start
|
# Run immediately on start
|
||||||
print('🚀 Running initial scrape...')
|
print('🚀 Running initial scrape...')
|
||||||
run_scraper()
|
run_scraper()
|
||||||
|
|
||||||
# Keep running
|
# Keep running
|
||||||
while True:
|
try:
|
||||||
try:
|
while True:
|
||||||
schedule.run_pending()
|
|
||||||
time.sleep(60) # Check every minute
|
time.sleep(60) # Check every minute
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print('\n👋 Scheduler stopped by user.')
|
print('\n👋 Scheduler stopped by user.')
|
||||||
break
|
scheduler.shutdown()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"Scheduler error: {e}")
|
logging.error(f"Scheduler error: {e}")
|
||||||
time.sleep(300) # Wait 5 minutes on error
|
scheduler.shutdown()
|
||||||
|
raise
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user