Last active
March 29, 2021 11:21
-
-
Save georgewhewell/4447776 to your computer and use it in GitHub Desktop.
wrapper for appengine dev_appserver to execute cron jobs from cron.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Needs libyaml and pyyaml installed | |
# on mac: brew install libyaml && pip install PyYAML | |
import time, datetime, yaml, sys, os, subprocess, threading, requests, random, argparse, re | |
threads = [] | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-p', '--port', default='8080') | |
parser.add_argument('-a', '--address', default='localhost') | |
parser.add_argument('dir', nargs='?', default=os.getcwd()) | |
args, extra = parser.parse_known_args() | |
root_url = 'http://%s:%s' % (args.address, args.port) | |
cron_pattern = re.compile('^every ([0-9]+) ([a-z]+)$') | |
def cron_task(config): | |
# Simultaneous reqs crash appserver | |
time.sleep(random.randrange(5,10)) | |
amount, unit = re.findall(cron_pattern, config['schedule'])[0] | |
sleep_time = datetime.timedelta(**{unit: int(amount)}).seconds | |
while True: | |
try: | |
requests.get(root_url + config['url']) | |
except: | |
pass | |
time.sleep(sleep_time) | |
if os.path.isfile(os.path.join(args.dir, 'cron.yaml')): | |
cron_config = yaml.load(open(os.path.join(args.dir, 'cron.yaml'))) | |
if 'cron' not in cron_config: | |
print 'No cron entries' | |
else: | |
for entry in cron_config['cron']: | |
threads.append( | |
threading.Thread(target=cron_task, args=[entry]) | |
) | |
threads[-1].daemon=True | |
threads[-1].start() | |
print "Running dev_appserver.." | |
sys.argv[0] = 'dev_appserver.py' | |
subprocess.call(sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment