3

I am running this code to send a sms message with Twilio...

client.messages.create(
        to=form.phone.data, 
        from_="+1xxxxxxxxxx",
        body="This is a text message"

My application is hosted on AWS Lambda using Python's Zappa. The problem is that I need to be able to schedule this message to be sent 10 minutes in the future.

Zappa offers task execution but their documentation is unclear for how something like this should be done.

Thanks for the help.

freefly0313
  • 75
  • 2
  • 12
  • Zappa now supports asynchronous task execution: https://github.com/Miserlou/Zappa#asynchronous-task-execution – pfcodes Oct 05 '20 at 03:35

2 Answers2

5

This isn't something Zappa directly supports at this time. You'll need to perform a hack of some sort around the available scheduling system.

Schedule an event to run every minute:

{
    "production": {
       ...
       "events": [{
           "function": "your_module.send_msg", // The function to execute
           "expression": "rate(1 minute)" // When to execute it (in cron or rate format)
       }],
       ...
    }
}

Your code can be along these lines.

from datetime import datetime

def send_msg():
    form = get_form()
    elapsed = datetime.now() - form.date_created 
    if 10 < abs(elapsed.total_seconds())/60) < 11: # this is naive
        client.messages.create(...)
Oluwafemi Sule
  • 27,776
  • 1
  • 40
  • 64
  • Interesting. Would this require the server to be running the whole time? Lambda dies after a period of time. – freefly0313 May 26 '17 at 20:51
  • Cloudwatch will invoke the function every minute. The deal is when the function is invoked, a naive check is done with the elapsed time. If the elapsed time passes the check, the message is sent. – Oluwafemi Sule May 26 '17 at 21:07
  • why not use "expression": "rate(10 minutes)" instead? – Shital Jun 06 '20 at 16:46
  • Then you couldn't be sure that the worker will run your task in the next ten minutes after its submitted. The schedule checks for task that way every 10 minutes. You may have sent your task in sometime within that period. It can be that way if one can live with some appreciable delay. – Oluwafemi Sule Jun 06 '20 at 18:19
1

I made a db driven task queue for zappa. https://github.com/andytwoods/zappa-call-later . Early days, but we are using it in production.

Every X minutes, (as suggested in @Oluwafemi Sule's answer) a Zappa event pings a function that checks for tasks. Tasks can be delayed Y minutes, repeated Z times etc.

My solution is crude in that it has low time resolution, and is quite low level currently.

andyw
  • 2,309
  • 1
  • 24
  • 38