Scheduled Django management commands using Zappa & Lamda

441 views Asked by At

I've got my Django site working on Lambda using Zappa. It was very simple. I'm now searching to find out how I set up scheduled Django management commands. From what I've read the work around is to create Python functions that execute the management commands and then schedule the functions to run using the Zappa settings file. Is this still the right method as the help manual doesn't say anything?

1

There are 1 answers

0
TechnoConserve On

At the time of writing there is an open Zappa issue about this.

symroe came up with this solution which seems to work nicely:

class Runner:
  def __getattr__(self, attr):
    from django.core.management import call_command
    return lambda: call_command(attr)

import sys
sys.modules[__name__] = Runner()

This allows you to specify any Django management command in your zappa_settings.json file without further code modifications. That bit looks like this where zappa_schedule.py is the name of the file containing the above code and publish_scheduled_pages() is a registered management command:

"events": [{
  "function": "zappa_schedule.publish_scheduled_pages",
  "expression": "rate(1 hour)"
}],