@@ -0,0 +1,2233 @@
+/* Copyright 2020 Google Inc.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A%0Apackage com.google.sps.servlets;%0A%0Aimport com.google.appengine.api.taskqueue.Queue;%0Aimport com.google.appengine.api.taskqueue.QueueFactory;%0Aimport com.google.appengine.api.taskqueue.TaskOptions;%0Aimport com.google.sps.Recommend;%0Aimport java.io.IOException;%0Aimport java.util.logging.Logger;%0Aimport javax.servlet.ServletException;%0Aimport javax.servlet.annotation.WebServlet;%0Aimport javax.servlet.http.HttpServlet;%0Aimport javax.servlet.http.HttpServletRequest;%0Aimport javax.servlet.http.HttpServletResponse;%0A%0A@WebServlet(%22/worker%22)%0Apublic class TaskHandler extends HttpServlet %7B%0A%0A  private static final Logger log = Logger.getLogger(TaskHandler.class.getName());%0A%0A  private static final int TWELVE_HOURS = 43_200_000;%0A  private Queue queue;%0A%0A  @Override%0A  public void init() %7B%0A    queue = QueueFactory.getQueue(%22recommend-queue%22);%0A  %7D%0A%0A  /** Makes sure there is a recommend job queued. */%0A  protected void doGet(HttpServletRequest request, HttpServletResponse response)%0A      throws ServletException, IOException %7B%0A    if (queue == null) %7B%0A      init();%0A    %7D%0A    if (queue.fetchStatistics().getNumTasks() == 0) %7B%0A      queue.add(TaskOptions.Builder.withUrl(%22/worker%22));%0A    %7D%0A  %7D%0A%0A  /** Calculates recommendations and re-schedules the next calculation. */%0A  protected void doPost(HttpServletRequest request, HttpServletResponse response)%0A      throws ServletException, IOException %7B%0A    if (queue == null) %7B%0A      init();%0A    %7D%0A%0A    // recalculates the recommendation model%0A    Recommend.calculateRecommend();%0A%0A    // queues new task for next recalculation%0A    queue.add(TaskOptions.Builder.withUrl(%22/worker%22).countdownMillis(TWELVE_HOURS));%0A  %7D%0A%7D%0A

Add handler servlet for task scheduling