开发者

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

  • Is there a way to guarantee a task to be performed in X minutes (or after X min) 开发者_高级运维? (rate would mean the intervals between tasks but what about the first task, would the first task starts after the 'rate' time?)


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


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]
);


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


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.


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*/

    }
    }   
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜