Skip to content

Commit a421c30

Browse files
authored
AdminServer deactivate (#241)
fixes #240
1 parent fc4c8f3 commit a421c30

4 files changed

Lines changed: 53 additions & 13 deletions

File tree

transact/src/main/java/dev/dbos/transact/admin/AdminServer.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.HashMap;
1313
import java.util.List;
1414
import java.util.Map;
15+
import java.util.concurrent.atomic.AtomicBoolean;
1516
import java.util.regex.Pattern;
1617
import java.util.stream.Collectors;
1718

@@ -30,6 +31,7 @@ public class AdminServer implements AutoCloseable {
3031
private HttpServer server;
3132
private final SystemDatabase systemDatabase;
3233
private final DBOSExecutor dbosExecutor;
34+
private final AtomicBoolean isRunning = new AtomicBoolean(true);
3335

3436
public AdminServer(int port, DBOSExecutor exec, SystemDatabase sysDb) throws IOException {
3537

@@ -39,7 +41,7 @@ public AdminServer(int port, DBOSExecutor exec, SystemDatabase sysDb) throws IOE
3941
Map<String, HttpHandler> staticRoutes = new HashMap<>();
4042
staticRoutes.put("/dbos-healthz", x -> healthCheck(x));
4143
staticRoutes.put("/dbos-workflow-recovery", x -> workflowRecovery(x)); // post
42-
staticRoutes.put("/dbos-deactivate", x -> deactivate(x));
44+
staticRoutes.put("/deactivate", x -> deactivate(x));
4345
staticRoutes.put("/dbos-workflow-queues-metadata", x -> workflowQueuesMetadata(x));
4446
staticRoutes.put("/dbos-garbage-collect", x -> garbageCollect(x)); // post
4547
staticRoutes.put("/dbos-global-timeout", x -> globalTimeout(x)); // post
@@ -125,7 +127,14 @@ private void workflowRecovery(HttpExchange exchange) throws IOException {
125127
}
126128

127129
private void deactivate(HttpExchange exchange) throws IOException {
128-
sendText(exchange, 500, "not implemented");
130+
if (isRunning.compareAndSet(true, false)) {
131+
logger.info(
132+
"Deactivating DBOS executor {} app version {}",
133+
dbosExecutor.executorId(),
134+
dbosExecutor.appVersion());
135+
dbosExecutor.deactivateLifecycleListeners();
136+
}
137+
sendText(exchange, 200, "deactivated");
129138
}
130139

131140
private void workflowQueuesMetadata(HttpExchange exchange) throws IOException {

transact/src/main/java/dev/dbos/transact/execution/DBOSExecutor.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,7 @@ public void close() {
190190
recoveryService.stop();
191191
recoveryService = null;
192192

193-
for (var listener : listeners) {
194-
try {
195-
listener.dbosShutDown();
196-
} catch (Exception e) {
197-
logger.warn("Exception from shutdown", e);
198-
}
199-
}
193+
shutdownLifecycleListeners();
200194

201195
queueService.stop();
202196
queueService = null;
@@ -211,6 +205,22 @@ public void close() {
211205
}
212206
}
213207

208+
public void deactivateLifecycleListeners() {
209+
if (isRunning.get()) {
210+
shutdownLifecycleListeners();
211+
}
212+
}
213+
214+
private void shutdownLifecycleListeners() {
215+
for (var listener : listeners) {
216+
try {
217+
listener.dbosShutDown();
218+
} catch (Exception e) {
219+
logger.warn("Exception from shutdown", e);
220+
}
221+
}
222+
}
223+
214224
// package private method for test purposes
215225
SystemDatabase getSystemDatabase() {
216226
return systemDatabase;

transact/src/test/java/dev/dbos/transact/admin/AdminServerTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,12 @@ public void deactivate() throws IOException {
146146
given()
147147
.port(port)
148148
.when()
149-
.get("/dbos-deactivate")
149+
.get("/deactivate")
150150
.then()
151-
.statusCode(500)
152-
.body(equalTo("not implemented"));
151+
.statusCode(200)
152+
.body(equalTo("deactivated"));
153+
154+
verify(mockExec).deactivateLifecycleListeners();
153155
}
154156
}
155157

transact/src/test/java/dev/dbos/transact/execution/LifecycleTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

55
import dev.dbos.transact.DBOS;
6+
import dev.dbos.transact.DBOSTestAccess;
67
import dev.dbos.transact.config.DBOSConfig;
78
import dev.dbos.transact.utils.DBUtils;
89

@@ -44,18 +45,36 @@ void beforeEachTest() throws SQLException {
4445

4546
@AfterEach
4647
void afterEachTest() throws Exception {
48+
DBOS.shutdown();
49+
}
50+
51+
@Test
52+
void checkThatItAllHappened() throws Exception {
53+
// Pretend this is an external event
54+
var total = svc.runThemAll();
55+
assertEquals(3, impl.nInstances); // One of these is internal... for better or worse
56+
assertEquals(4, impl.nWfs);
57+
assertEquals(14, svc.annotationCount);
58+
assertEquals(30, total);
59+
4760
assertEquals(0, svc.shutdownCount);
4861
DBOS.shutdown();
4962
assertEquals(1, svc.shutdownCount);
5063
}
5164

5265
@Test
53-
void checkThatItAllHappened() throws Exception {
66+
void deactivateLifecycleListeners() throws Exception {
5467
// Pretend this is an external event
5568
var total = svc.runThemAll();
5669
assertEquals(3, impl.nInstances); // One of these is internal... for better or worse
5770
assertEquals(4, impl.nWfs);
5871
assertEquals(14, svc.annotationCount);
5972
assertEquals(30, total);
73+
74+
assertEquals(0, svc.shutdownCount);
75+
DBOSTestAccess.getDbosExecutor().deactivateLifecycleListeners();
76+
assertEquals(1, svc.shutdownCount);
77+
DBOS.shutdown();
78+
assertEquals(2, svc.shutdownCount);
6079
}
6180
}

0 commit comments

Comments
 (0)