Schedule periodic tasks with APScheduler using a SQLAlchemyJobStore so jobs persist across restarts. Create a job that runs every 2nd Friday of the month using a cron trigger expression.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a Python example using APScheduler with SQLAlchemyJobStore that schedules a job every 2nd Friday of the month at 10:00 AM by specifying day='8-14' and day_of_week='fri' in a cron trigger:
This setup ensures job persistence across restarts using an SQLite database. Adjust the connection string as needed for production environments.
Below is a minimal example showing how to configure APScheduler with a SQLAlchemyJobStore so jobs persist across restarts, and how to schedule a job that runs every 2nd Friday of the month using a cron trigger:
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore
from apscheduler.triggers.cron import CronTrigger
import time
# Job function to run
def my_job():
print("Running job...")
if __name__ == "__main__":
# Configure the job store (here using SQLite for demonstration)
jobstores = {
'default': SQLAlchemyJobStore(url='sqlite:///jobs.sqlite')
}
# Create the scheduler and start it
scheduler = BackgroundScheduler(jobstores=jobstores)
scheduler.start()
# Add a job that runs every 2nd Friday of the month at 10:00 AM
# Cron expression explanation:
# - day='8-14' limits it to dates 8–14
# - day_of_week='fri' ensures it’s Friday
# Together, that targets the second Friday each month.
scheduler.add_job(
my_job,
trigger='cron',
day='8-14',
day_of_week='fri',
hour=10,
minute=0,
id='second_friday_job',
replace_existing=True
)
try:
# Keep the main thread alive to let the scheduler run
while True:
time.sleep(2)
except (KeyboardInterrupt, SystemExit):
scheduler.shutdown()
Adjust the database URL and other settings for your environment as needed.