0
votes

In Google App Engine crons, the highest frequency to call a handler is 1m. If we want a script to run more often, does it make sense to have two cron definitions each running at 1 min? Like below:

cron:

  • description: cron1 url: /handle_info schedule: every 1 minute

  • description: cron2 url: /handle_info schedule: every 1 minute

What are other good approaches?

3
If you're trying to run something this frequently, you might want to take a close look at what you're doing and see if you can make it event based. The Task Queue is an excellent choice for running stuff just when you need it, rather than polling. - Nick Johnson

3 Answers

4
votes

You can fire a task using the Task Queue API that schedules another task using an eta < now + 1min.

1
votes

I came up with an idea to use Cron jobs to execute my script more than once a minute.

Here's what I did: I created a cronjob to execute a file called cron.php Then inside of that file I changed the max execution time to 50 seconds And then I created a loop with a sleep command inside of it And each time the loop fires it includes the commands I need to run.

Here's the code:

<?PHP
ini_set('max_execution_time', 50);
require_once('includes/settings.php'); 

for ($i=1; $i<=5; $i++){
    include('commander.php');
    sleep(10);
}
?>
0
votes

I created a 1 minute cron then used defer in the app engine code to raise another pubsub message

import logging
import webapp2
import time
import json
import pubsub_utils
from google.appengine.ext import ndb, deferred

def defer_post(item_key=None):
    try:
        logging.info('deferred pubsub')
        pubsub_utils.publish_to_topic(item_key, str(time.time()))
    except Exception as e:
        logging.info('item_key: %s', item_key)
        logging.exception(e)

class PushToPubSub(webapp2.RequestHandler):
    def get(self, topic):
        logging.info('cron pubsub: %s', topic)
        pubsub_utils.publish_to_topic(topic, str(time.time()))
        deferred.defer(defer_post, item_key=topic, _countdown=30)
        self.response.headers['Content-Type'] = 'application/json'
        self.response.write(json.dumps({"status": "200"}))

app = webapp2.WSGIApplication([
    webapp2.Route(r'/publish/<topic>', handler=PushToPubSub)
], debug=True)