I am creating a fairly straightforward little app, and am trying to use apscheduler to send a text to a user at a certain time. It works wonderfully locally, but the clock process, as specified in the Procfile, crashes every time. The only information I get is "Process exited with status code 0. State changed from up to crashed." when I run heroku logs -t -p clock.
My Procfile looks as follows:
web: uvicorn remind_me.main:app --host=0.0.0.0 --port=${PORT:-8000} clock: python remind_me/views/register.py
I also ran the heroku ps:scale clock=1 command.
The code for my "view" is below:
from apscheduler.schedulers.background import BackgroundScheduler
from dateutil.parser import parse`
sched = BackgroundScheduler({'apscheduler.timezone': 'EST'})
def scheduled_job(msg, number, carrier):
send(msg, number, carrier)
@router.post('/')
@template()
async def home(request: Request):
vm = HomeViewModel(request)
await vm.load()
if vm.error:
return vm.to_dict()
msg, number, carrier, run_date = vm.task, vm.number, vm.carrier, vm.date_and_time
run_date = parse(run_date)
sched.add_job(scheduled_job, args=(msg,number,carrier), trigger='date', run_date=run_date)
try:
sched.start()
except:
pass
db_storage.store_events(vm.name, vm.number, vm.carrier, vm.task, vm.date_and_time)
return fastapi.responses.RedirectResponse(url="/", status_code=status.HTTP_302_FOUND)
I obviously left out some imports related to FastAPI and some stuff I was doing with the database, but I left those out for brevity. Everything works great locally, and all the pages work perfectly on Heroku, it's just that the clock process crashes every time.
I have also tried this with the blocking scheduler, but have had no luck, as that doesn't return and obviously I need the page to return/redirect.
I am wondering if it has something to do with the async aspect of the view itself, and if I might need to be APScheduler's async method (I can't remember its name offhand.). Any advice that you folks could give would be much appreciated. Thanks!