Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:APIJSON APIJSON Demo AutoTestNodeProcessSupervisor

From Leeroopedia
Knowledge Sources
Domains Process_Management, Reliability, DevOps
Last Updated 2026-10-14 00:00 GMT

Overview

Concrete tool for managing the lifecycle of the Node.js APIAuto test runner process, providing automatic startup, health monitoring, crash recovery, and graceful shutdown provided by the APIJSON Demo server.

Description

The AutoTestNodeProcessSupervisor class is a Spring @Service that manages the Node.js process running the APIAuto test UI. It:

  • Starts on application ready: Listens for ApplicationReadyEvent and starts the Node.js process after Spring Boot is fully initialized.
  • Health checking: Uses @Scheduled(fixedDelay) to poll the Node.js HTTP endpoint every 30 seconds. If unreachable, it restarts the process.
  • Process management: Uses ProcessBuilder to launch the Node.js process with configurable command, script, and working directory. Output is redirected to a log file.
  • Startup timeout: Waits up to startupTimeoutMillis (default 60s) for the process to become reachable. If the process exits immediately or the timeout expires, an error is thrown.
  • Graceful shutdown: @PreDestroy hook terminates the process on JVM shutdown.
  • Concurrency safety: Uses synchronized(lock) with double-checked isNodeServiceReachable() to prevent duplicate processes.

Usage

  • Automatic lifecycle: Starts and monitors automatically — no manual invocation needed.
  • Configuration: All parameters via @Value injection from application.yml.
  • Disabling: Set apijson.auto-test.node.enabled=false.

Code Reference

Source Location

  • Repository: APIJSON-Demo
  • File: APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/boot/AutoTestNodeProcessSupervisor.java

Signature

@Service
public class AutoTestNodeProcessSupervisor {
    @Value("${apijson.auto-test.node.command:node}") private String nodeCommand;
    @Value("${apijson.auto-test.node.script:js/server.js}") private String nodeScript;
    @Value("${apijson.auto-test.node.working-directory:src/main/resources/static/api}") private String nodeWorkingDirectory;
    @Value("${apijson.auto-test.node.startup-timeout-millis:60000}") private long startupTimeoutMillis;

    @EventListener(ApplicationReadyEvent.class)
    public void startWhenApplicationReady();

    @Scheduled(fixedDelayString = "${apijson.auto-test.node.health-check-interval-millis:30000}",
               initialDelayString = "${apijson.auto-test.node.health-check-interval-millis:30000}")
    public void keepAlive();

    public void ensureRunning();
    public boolean isNodeServiceReachable();

    @PreDestroy
    public void stopManagedProcess();

    private void startProcess();
    private void waitUntilReachable();
    private Path resolveWorkingDirectory();
    private Path prepareLogFile(Path workingDirectory);
}

Import

import apijson.boot.AutoTestNodeProcessSupervisor;

I/O Contract

Inputs

Name Type Required Description
nodeCommand String No Node.js executable (default: "node")
nodeScript String No Script path relative to working directory (default: "js/server.js")
nodeWorkingDirectory String No Working directory (default: "src/main/resources/static/api")
startupTimeoutMillis long No Max startup wait (default: 60000ms)

Outputs

Name Type Description
process Process Managed Node.js process with PID
logFilePath Path Log file at {workingDir}/logs/api-auto-node.log
isNodeServiceReachable boolean True if HTTP GET returns 2xx

Usage Examples

application.yml Configuration

apijson:
  auto-test:
    node:
      enabled: true
      command: node
      script: js/server.js
      working-directory: src/main/resources/static/api
      startup-timeout-millis: 60000
      health-check-interval-millis: 30000

Manual Health Check

@Autowired
private AutoTestNodeProcessSupervisor supervisor;

// Check if Node.js is running
if (supervisor.isNodeServiceReachable()) {
    System.out.println("APIAuto Node is healthy");
} else {
    // Ensure it starts
    supervisor.ensureRunning();
}

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment