Skip to content
Permalink
Browse files
bot: add readiness handler
Reviewed-by: erikj
  • Loading branch information
edvbld committed May 12, 2021
1 parent 2565015 commit 8cbcc1abbba2849381a2790289ac1583268587b4
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
@@ -211,6 +211,7 @@ private void drain(Duration timeout) throws TimeoutException {
private final List<Bot> bots;
private final ScheduledThreadPoolExecutor executor;
private final BotWatchdog botWatchdog;
private volatile boolean isReady;

private static final Logger log = Logger.getLogger("org.openjdk.skara.bot");

@@ -229,6 +230,11 @@ public BotRunner(BotRunnerConfiguration config, List<Bot> bots) {

executor = new ScheduledThreadPoolExecutor(config.concurrency());
botWatchdog = new BotWatchdog(Duration.ofMinutes(10));
isReady = false;
}

boolean isReady() {
return isReady;
}

private void checkPeriodicItems() {
@@ -309,6 +315,7 @@ public void run(Duration timeout) {
}
}

isReady = true;
executor.scheduleAtFixedRate(this::itemWatchdog, 0,
config.scheduledExecutionPeriod().toMillis(), TimeUnit.MILLISECONDS);
executor.scheduleAtFixedRate(this::checkPeriodicItems, 0,
@@ -391,7 +391,8 @@ Optional<HttpServerConfiguration> httpServer(BotRunner runner) {

Map<String, BiFunction<BotRunner, JSONObject, HttpHandler>> factories = Map.of(
WebhookHandler.name(), WebhookHandler::create,
MetricsHandler.name(), MetricsHandler::create
MetricsHandler.name(), MetricsHandler::create,
ReadinessHandler.name(), ReadinessHandler::create
);
var contexts = new ArrayList<HttpContextConfiguration>();
var port = config.get("http-server").get("port").asInt();
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.skara.bot;

import org.openjdk.skara.json.JSONObject;

import com.sun.net.httpserver.*;

import java.io.IOException;
import java.util.logging.Logger;

class ReadinessHandler implements HttpHandler {
private static final Logger log = Logger.getLogger("org.openjdk.skara.bot");
private final BotRunner runner;

ReadinessHandler(BotRunner runner) {
this.runner = runner;
}

@Override
public void handle(HttpExchange exchange) throws IOException {
if (runner.isReady()) {
exchange.sendResponseHeaders(200, 0);
exchange.getResponseBody().close();
} else {
exchange.sendResponseHeaders(404, 0);
exchange.getResponseBody().close();
}
}

static ReadinessHandler create(BotRunner runner, JSONObject configuration) {
return new ReadinessHandler(runner);
}

static String name() {
return "readiness";
}
}

1 comment on commit 8cbcc1a

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on 8cbcc1a May 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.