diff --git a/docs/openaf-flags.md b/docs/openaf-flags.md index ff15172c6..43313a03b 100644 --- a/docs/openaf-flags.md +++ b/docs/openaf-flags.md @@ -38,6 +38,12 @@ Central list of noteworthy runtime flags and environment variables. | OAF_SIGNATURE_KEY | (unset) | Public key for signature validation | | OAF_VALIDATION_STRICT | false | Require integrity + signature both | +## Server Management + +| Env | Default | Purpose | +|-----|---------|---------| +| OAF_PIDFILE | (unset) | Override PID file path in ow.server.checkIn | + ## Misc Performance / Behavior | Flag | Purpose | diff --git a/docs/openaf.md b/docs/openaf.md index 2e807a21c..110d732d3 100644 --- a/docs/openaf.md +++ b/docs/openaf.md @@ -483,6 +483,7 @@ ow.sec.closeMainSBuckets(); ow.loadServer(); // Process management +// Note: The PID file path can be overridden by setting the OAF_PIDFILE environment variable var isRunning = ow.server.checkIn("server.pid", function(existingPid) { log("Server already running with PID: " + existingPid); diff --git a/js/owrap.server.js b/js/owrap.server.js index 65fce9ddc..13bd91dc5 100644 --- a/js/owrap.server.js +++ b/js/owrap.server.js @@ -14,7 +14,8 @@ OpenWrap.server = function() { * Optionally you can provide an onShutdown function to execute any code needed upon controlled shutdown * of the server and provide an onAlreadyRunning function (that will received the corresponding aPidFile). * If the onAlreadyRunning function returns false the process will exit with -1 (or the anExitCode provided), - * if true will continue processing. + * if true will continue processing. The aPidFile parameter can be overridden by setting the OAF_PIDFILE + * environment variable. * Example:\ * \ * var params = processExpr();\ @@ -41,6 +42,11 @@ OpenWrap.server.prototype.checkIn = function(aPidFile, onAlreadyRunning, onShutd var ret = false; if (isUndefined(anExitCode)) anExitCode = -1; + // Override aPidFile with OAF_PIDFILE environment variable if set + var envPidFile = getEnv("OAF_PIDFILE") + if (isDef(envPidFile) && String(envPidFile).trim() != "") { + aPidFile = String(envPidFile).trim() + } if (isUndefined(aPidFile)) aPidFile = "server.pid"; ret = pidCheckIn(aPidFile); if (ret && isDefined(onShutdown)) addOnOpenAFShutdown(onShutdown); diff --git a/tests/autoTestAll.Server.js b/tests/autoTestAll.Server.js index 9aeffac0b..7fa82122a 100644 --- a/tests/autoTestAll.Server.js +++ b/tests/autoTestAll.Server.js @@ -395,4 +395,63 @@ $ch("queue::test").destroy(); }; + + exports.testCheckIn = function() { + ow.loadServer(); + + // Test 1: Default behavior without parameter - should use default "server.pid" + io.rm("server.pid"); + + var result1 = ow.server.checkIn(void 0, function() { return false; }); + ow.test.assert(result1, true, "Problem with checkIn creating default PID file"); + ow.test.assert(io.fileExists("server.pid"), true, "Problem with checkIn - default PID file should exist"); + + pidCheckOut("server.pid"); + io.rm("server.pid"); + + // Test 2: Specified PID file parameter + var testPid = "test_checkin.pid"; + io.rm(testPid); + + var result2 = ow.server.checkIn(testPid, function() { return false; }); + ow.test.assert(result2, true, "Problem with checkIn creating specified PID file"); + ow.test.assert(io.fileExists(testPid), true, "Problem with checkIn - specified PID file should exist"); + ow.test.assert(io.fileExists("server.pid"), false, "Problem with checkIn - default PID file should not exist when custom specified"); + + pidCheckOut(testPid); + io.rm(testPid); + + // Test 3: Verify OAF_PIDFILE environment variable behavior + // Note: Environment variables cannot be set at runtime in Java (they're inherited from parent process) + // This test checks if OAF_PIDFILE is set in the environment and validates override behavior. + // If OAF_PIDFILE is not set, these tests will be skipped (which is expected behavior). + // To test this functionality in CI, set OAF_PIDFILE before running the test suite. + // Example CI configuration: + // env: + // OAF_PIDFILE: test_env.pid + // run: cd tests && ../ojob autoTestAll.yaml + var envPidFile = getEnv("OAF_PIDFILE"); + if (isDefined(envPidFile) && envPidFile != "") { + // Test 3a: Environment variable overrides explicit parameter + io.rm(envPidFile); + var result3 = ow.server.checkIn("test_param.pid", function() { return false; }); + ow.test.assert(result3, true, "Problem with checkIn when OAF_PIDFILE is set"); + ow.test.assert(io.fileExists(envPidFile), true, "Problem with checkIn - OAF_PIDFILE file should exist"); + ow.test.assert(io.fileExists("test_param.pid"), false, "Problem with checkIn - parameter file should not exist when OAF_PIDFILE is set"); + + pidCheckOut(envPidFile); + io.rm(envPidFile); + + // Test 3b: Environment variable overrides default (undefined parameter) + io.rm(envPidFile); + io.rm("server.pid"); + var result4 = ow.server.checkIn(void 0, function() { return false; }); + ow.test.assert(result4, true, "Problem with checkIn when OAF_PIDFILE is set with undefined parameter"); + ow.test.assert(io.fileExists(envPidFile), true, "Problem with checkIn - OAF_PIDFILE file should exist when parameter is undefined"); + ow.test.assert(io.fileExists("server.pid"), false, "Problem with checkIn - default file should not exist when OAF_PIDFILE is set"); + + pidCheckOut(envPidFile); + io.rm(envPidFile); + } + }; })(); \ No newline at end of file diff --git a/tests/autoTestAll.Server.yaml b/tests/autoTestAll.Server.yaml index 5d35c766f..4b2b19ba3 100644 --- a/tests/autoTestAll.Server.yaml +++ b/tests/autoTestAll.Server.yaml @@ -80,6 +80,11 @@ jobs: to : oJob Test exec: args.func = args.tests.testQueue; + - name: Server::CheckIn + from: Server::Init + to : oJob Test + exec: args.func = args.tests.testCheckIn; + todo: # Server tests # ------------ @@ -97,4 +102,5 @@ todo: - Server::Locks - Server::Auth - Server::AuthApp - - Server::Queue \ No newline at end of file + - Server::Queue + - Server::CheckIn \ No newline at end of file