Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zappa scheduling with Python

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.

like image 495
freefly0313 Avatar asked Aug 11 '26 05:08

freefly0313


1 Answers

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(...)
like image 162
Oluwafemi Sule Avatar answered Aug 13 '26 06:08

Oluwafemi Sule



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!