Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bot: add readiness handler #1154

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -200,6 +200,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");

@@ -218,6 +219,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() {
@@ -298,6 +304,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";
}
}