Derek Cutting
Derek Cutting

Reputation: 11

Scheduling a method to run at specific times

I'm making an app in python to send texts via Twilio. I'm using flask and it's hosted on Google App Engine. I have a list of messages that need to be sent at a specific date and time, by calling my message function. What's a simple way to go about creating this? I'm relatively new to all this.

I tried apscheduler, but it only worked on my local and not on the app engine. I've read about cron jobs, but can't find anything about specific dates/times or how to pass args when the job runs.

Upvotes: 1

Views: 771

Answers (3)

minou
minou

Reputation: 16573

Another option is to use a taskqueue task where you specify the time that the task should be run using the eta option (estimated time of arrival).

The task will sit in the queue until its time of execution arrives, and then GAE will cause the task to be launched to do whatever processing you need such as sending a text message.

The tasks may not be executed at the precise time you specify but in my experience it is generally quite close. Certainly far more accurate than running a CRON job every 10 minutes.

This will also be much more efficient than using a CRON job because a CRON job will cause a request to your app every 10 minutes but the task will only execute when needed. If you have a low volume app this may help you stay within the free quota.

Upvotes: 0

Bravin Balasubramaniam
Bravin Balasubramaniam

Reputation: 494

This is where Google app engine come in handy. You can use cron jobs from app engine. Create a cron.yaml file in your project.In this file you can make all kind of scheduling option every day to one day in a week in a particular time. The following is an example cron.yaml file

cron:
- description: "daily summary job"
  url: /tasks/summary
  schedule: every 24 hours
- description: "monday morning mailout"
  url: /mail/weekly
  schedule: every monday 09:00
  timezone: Australia/NSW
- description: "new daily summary job"
  url: /tasks/summary
  schedule: every 24 hours
  target: beta

Cron schedules are specified using a simple English-like format.

every 12 hours
every 5 minutes from 10:00 to 14:00
every day 00:00
every monday 09:00
2nd,third mon,wed,thu of march 17:00
1st monday of sep,oct,nov 17:00
1 of jan,april,july,oct 00:00

for more well-explained scheduling format please refer this documentation.

Upvotes: 1

Eric L.
Eric L.

Reputation: 89

As mention in the comments by Fabio, you could make a cron task to run every 10 min (or every minute). I would look into a folder for messages to send. If you would make a filename format in that folder to start with the date and time, you could do something like that :

folder content:

201707092205_<#message_id>

pseudo-code for sending the message:

intant_when_the_script_is_ran = datetime.now().strftime(format_to_the_minute)

for file in folder:
    if intant_when_the_script_is_ran in file
       with open(file, 'rw') as fh:
         destination = fh.readline() #reading the fisrt line
         message = fh.readlines() #reading the rest of the message
         twilioapi.sendmessage(destination, message)
       os.remove(file)  #the remove could be done in another script to leave some traces 

Upvotes: 1

Related Questions