Skip to content

Commit

Permalink
Fix pubber shutdown logic (faucetsdn#432)
Browse files Browse the repository at this point in the history
  • Loading branch information
grafnu authored and noursaidi committed Sep 9, 2022
1 parent 9862acb commit bda281e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 25 deletions.
3 changes: 2 additions & 1 deletion bin/reset_config
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
ROOT=$(realpath $(dirname $0)/..)

if [[ $# != 3 && $# != 4 ]]; then
echo Usage: $0 site_dir project_id device_id [config_file]
echo Usage: $0 SITE_DIR PROJECT_ID DEVICE_ID [CONFIG_FILE]
false
fi

Expand All @@ -22,6 +22,7 @@ cd ${ROOT}

dst_config=/tmp/${device_id}_config.json
src_config=${site_dir}/devices/${device_id}/out/$config_file
ls -l ${src_config}
now_date=$(python3 -c 'import datetime; print(datetime.datetime.utcnow().isoformat() + "Z")')
echo Setting config timestamp ${now_date}
jq < ${src_config} .timestamp=\"${now_date}\" |\
Expand Down
39 changes: 15 additions & 24 deletions pubber/src/main/java/daq/pubber/Pubber.java
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,8 @@ private synchronized void cancelPeriodicSend() {
private void sendMessages() {
try {
updatePoints();
sendDeviceMessage();
deferredConfigActions();
sendDeviceMessage();
flushDirtyState();
} catch (Exception e) {
error("Fatal error during execution", e);
Expand Down Expand Up @@ -531,9 +531,14 @@ private void maybeRestartSystem() {

private void restartSystem(boolean restart) {
deviceState.system.mode = restart ? SystemMode.RESTART : SystemMode.SHUTDOWN;
publishSynchronousState();
error("Stopping system with extreme prejudice, restart " + restart);
System.exit(restart ? RESTART_EXIT_CODE : SHUTDOWN_EXIT_CODE);
try {
publishSynchronousState();
} catch (Exception e) {
error("Squashing error publishing state while shutting down", e);
}
int exitCode = restart ? RESTART_EXIT_CODE : SHUTDOWN_EXIT_CODE;
error("Stopping system with extreme prejudice, restart " + restart + " with code " + exitCode);
System.exit(exitCode);
}

private void flushDirtyState() {
Expand Down Expand Up @@ -1269,7 +1274,7 @@ private void publishAsynchronousState() {
if (delay > 0) {
markStateDirty(delay);
} else {
publishStateRaw();
publishStateMessage();
}
} finally {
stateLock.release();
Expand All @@ -1282,35 +1287,21 @@ private void publishAsynchronousState() {
private void publishSynchronousState() {
try {
stateLock.acquire();
publishStateRaw();
publishStateMessage();
} catch (Exception e) {
throw new RuntimeException("While sending synchronous state", e);
} finally {
stateLock.release();
}
}

private void publishStateRaw() {
private void publishStateMessage() {
long delay = lastStateTimeMs + STATE_THROTTLE_MS - System.currentTimeMillis();
if (delay > 0) {
warn(String.format("State update delay %dms", delay));
safeSleep(delay);
}

CountDownLatch latch = new CountDownLatch(1);
publishStateMessage(latch);

try {
info("Waiting for synchronous state send...");
if (!latch.await(CONFIG_WAIT_TIME_SEC, TimeUnit.SECONDS)) {
error("Timeout waiting for synchronous state send");
}
} catch (Exception e) {
error("Exception while waiting for synchronous state send", e);
}
}

private void publishStateMessage(CountDownLatch latch) {
deviceState.timestamp = new Date();
info(String.format("update state %s last_config %s", isoConvert(deviceState.timestamp),
isoConvert(deviceState.system.last_config)));
Expand All @@ -1321,13 +1312,13 @@ private void publishStateMessage(CountDownLatch latch) {
}
stateDirty.set(false);
lastStateTimeMs = System.currentTimeMillis();
CountDownLatch useLatch = latch == null ? new CountDownLatch(1) : latch;
CountDownLatch latch = new CountDownLatch(1);
publishDeviceMessage(deviceState, () -> {
lastStateTimeMs = System.currentTimeMillis();
useLatch.countDown();
latch.countDown();
});
try {
if (!useLatch.await(CONFIG_WAIT_TIME_SEC, TimeUnit.SECONDS)) {
if (!latch.await(CONFIG_WAIT_TIME_SEC, TimeUnit.SECONDS)) {
throw new RuntimeException("Timeout waiting for state send");
}
} catch (Exception e) {
Expand Down

0 comments on commit bda281e

Please sign in to comment.