bach
bach

Reputation: 369

GAE Task Queue - Is there a way to delay a task from executing for X seconds

Upvotes: 9

Views: 4542

Answers (5)

sa_penguin
sa_penguin

Reputation: 457

Using TaskQueue API

public class Enqueue extends HttpServlet {
    private static final Logger log = Logger.getLogger(Enqueue.class.getName());
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String param1= request.getParameter("param1");
        String param2= request.getParameter("param2");
        String time = request.getParameter("time");
        int timer = Integer.parseInt(time) * 1000;//sec to millisec
        log.info("Executing in "+ timer+" seconds");
        // Add the task to the default queue.
        // Execute in 5 seconds
    Queue queue = QueueFactory.getDefaultQueue();
            queue.add(TaskOptions.Builder.withUrl("/index1").param("param1", param1)
                    .param("param2", param2)
                    .countdownMillis(time));

        response.sendRedirect("/");
    }
}

Now define the job in Index1 class

public class Index1 extends HttpServlet {
    private static final Logger log = Logger.getLogger(Index1.class.getName());

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
        String param1= req.getParameter("param1");
        String param2= req.getParameter("param2");

            log.info("Worker processing " + param1);
/*Define job to be executed*/

    }
    }   

Upvotes: 0

hamx0r
hamx0r

Reputation: 4278

Per @Peter Recore's comment, the countdown field in add() is "Time in seconds into the future that this Task should execute. Defaults to zero."

Documentation: https://cloud.google.com/appengine/docs/python/refdocs/google.appengine.api.taskqueue

Upvotes: 2

Fakeer
Fakeer

Reputation: 1034

In PHP

$task = new PushTask(
    '/some/callback', 
    ['param1' => $param1, 'param2' => $param2, 'param3' => $param3],
    ['name'=>'EmailTask', 'method'=>'POST', 'delay_seconds'=>30]
);

Or more simply (it is a POST by default)

$task = new PushTask(
    '/some/callback', 
    ['param1' => $param1, 'param2' => $param2, 'param3' => $param3],
    ['delay_seconds'=>30]
);

Upvotes: 2

Joe Bergevin
Joe Bergevin

Reputation: 3268

Google has updated this portion of their api (see here). You can now send in a 3rd parameter with PushTask containing the following options:

  1. 'method': string One of 'POST', 'GET', 'HEAD', 'PUT', 'DELETE'. Default value: 'POST'.
  2. 'name': string Name of the task. Defaults to '' meaning the service will generate a unique task name.
  3. 'delay_seconds': float The minimum time to wait before executing the task. Default: zero.
  4. 'header': string Additional headers to be sent when the task executes.

Upvotes: 1

Nick Johnson
Nick Johnson

Reputation: 101149

If you mean 'at least X minutes from now', yes - use the task queue API.

Upvotes: 4

Related Questions