Search code examples
apscheduler

APScheduler triggers once then errors


I read Apscheduler runs once then throws TypeError and it did not help in my case.

I have an app that pulls the date and time and phone number from an sqlite db. Using apscheduler I want it to create a sms message to go out on that date at that time. Right now, it fires the text no matter what then throws an error.

from apscheduler.schedulers.background import BackgroundScheduler


scheduler = BackgroundScheduler()


def send_sms(x):
    client.messages.create(from_='18595543343',
                           to="1"+str(x),
                           body='This is a scheduled sms test.')


def build_sms():
    working_data = Customer.query.all()
    for each in working_data:
        #Check if text has been sent
        status = each.verified
        if status == 'no':
            send_to = each.phone
            verified_update = each.verified
            verified_update = 'yes'
            db.session.commit()
            #Get info to build date string
            send_date = each.order_date
            send_date = str(send_date)
            send_time = each.order_time
            send_dt_string = send_date + ' ' + send_time + ":00"
            #add this info to job queue
            scheduler.add_job(send_sms(), 'date', run_date=send_dt_string)
        elif status == 'yes':
            print("Verified status confirmed")
        else:
            print('Unknown error')


        if __name__ == '__main__':
            build_sms()
            scheduler.start()
            app.run(debug=True)

Traceback:

Traceback (most recent call last):
  File "C:/Users/Corey/PycharmProjects/core_x/app.py", line 246, in <module>
    build_sms()
  File "C:/Users/Corey/PycharmProjects/core_x/app.py", line 44, in build_sms
    scheduler.add_job(send_sms(x), 'date', run_date=send_dt_string)
  File "C:\Users\Corey\AppData\Local\Programs\Python\Python36-32\lib\site-packages\apscheduler\schedulers\base.py", line 427, in add_job
    job = Job(self, **job_kwargs)
  File "C:\Users\Corey\AppData\Local\Programs\Python\Python36-32\lib\site-packages\apscheduler\job.py", line 44, in __init__
    self._modify(id=id or uuid4().hex, **kwargs)
  File "C:\Users\Corey\AppData\Local\Programs\Python\Python36-32\lib\site-packages\apscheduler\job.py", line 165, in _modify
    raise TypeError('func must be a callable or a textual reference to one')
TypeError: func must be a callable or a textual reference to one

Why is it doing this?


Solution

  • Because you're calling send_sms() and then passing its return value to scheduler.add_job(), instead of the function itself.