Skip to content

Instantly share code, notes, and snippets.

@HanSooloo
Created August 6, 2022 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HanSooloo/b3e85171b20d031a4f5dd8621786549a to your computer and use it in GitHub Desktop.
Save HanSooloo/b3e85171b20d031a4f5dd8621786549a to your computer and use it in GitHub Desktop.
from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.jobstores.redis import RedisJobStore
from apscheduler.executors.pool import ThreadPoolExecutor
from datetime import datetime, timedelta
import os
import sys
from logger import log
REDIS_HOST = os.environ['REDIS_HOST']
REDIS_PORT = os.environ['REDIS_PORT']
REDIS_PASSWORD = os.environ['REDIS_PASSWORD']
jobstores = {
'default': RedisJobStore(host=REDIS_HOST, port=REDIS_PORT, password=REDIS_PASSWORD,
ssl_cert_reqs='optional',
ssl=True,
jobs_key='apscheduler.jobs',
run_times_key='apscheduler.run_times')
}
executors = {
'default': ThreadPoolExecutor(20)
}
scheduler_timezone = 'UTC'
sched = BlockingScheduler(jobstores=jobstores, executors=executors, timezone=scheduler_timezone)
def schedule_function_1():
scheduled_job = sched.add_job(func='functions:function_1',
trigger='date',
run_date=datetime.utcnow() + timedelta(minutes=1),
timezone=scheduler_timezone,
misfire_grace_time=None
)
log.info(f'scheduled_job: {scheduled_job}')
def schedule_function_2():
scheduled_job = sched.add_job(func='functions:function_2',
trigger='date',
run_date=datetime.utcnow() + timedelta(minutes=1),
timezone=scheduler_timezone,
misfire_grace_time=None
)
log.info(f'scheduled_job: {scheduled_job}')
log.info(f'Scheduler running? {sched.running}')
if __name__ == '__main__':
schedule_function_1()
try:
log.info(f'Starting main scheduling loop')
sched.start()
except (KeyboardInterrupt, SystemExit):
log.warning(f'Interrupted, shutting down')
sched.shutdown()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment