GAE Task Queue - Is there a way to delay a task fr

2019-03-26 06:05发布

  • 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?)

5条回答
成全新的幸福
2楼-- · 2019-03-26 06:29

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

查看更多
神经病院院长
3楼-- · 2019-03-26 06:42

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

查看更多
放荡不羁爱自由
4楼-- · 2019-03-26 06:44

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.
查看更多
Anthone
5楼-- · 2019-03-26 06:48

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]
);
查看更多
啃猪蹄的小仙女
6楼-- · 2019-03-26 06:50

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

    }
    }   
查看更多
登录 后发表回答