@andersbakken/fisk 5.0.2 → 5.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3433,6 +3433,28 @@ var output = {
3433
3433
 
3434
3434
  var fs = libExports;
3435
3435
 
3436
+ // systemd socket activation: the first passed fd is at SD_LISTEN_FDS_START (3).
3437
+ // See sd_listen_fds(3). When LISTEN_PID matches our pid and LISTEN_FDS >= 1,
3438
+ // listen on the inherited fd instead of creating a new socket. This lets
3439
+ // systemd own the socket file, so its inode survives daemon restarts and any
3440
+ // bind-mount of the socket (e.g. docker -v /tmp/fisk.socket:/tmp/fisk.socket)
3441
+ // keeps working across upgrades.
3442
+ const SD_LISTEN_FDS_START = 3;
3443
+ function systemdListenFd() {
3444
+ const pid = process.env.LISTEN_PID;
3445
+ const fds = process.env.LISTEN_FDS;
3446
+ if (!pid || !fds) {
3447
+ return undefined;
3448
+ }
3449
+ if (parseInt(pid, 10) !== process.pid) {
3450
+ return undefined;
3451
+ }
3452
+ const count = parseInt(fds, 10);
3453
+ if (!count || count < 1) {
3454
+ return undefined;
3455
+ }
3456
+ return SD_LISTEN_FDS_START;
3457
+ }
3436
3458
  class Server extends EventEmitter__default["default"] {
3437
3459
  constructor(option, common) {
3438
3460
  super();
@@ -3443,6 +3465,7 @@ class Server extends EventEmitter__default["default"] {
3443
3465
  this.option = option;
3444
3466
  this._connections = {};
3445
3467
  this._connectionId = 0;
3468
+ this._activated = false;
3446
3469
  }
3447
3470
  close() {
3448
3471
  if (this.debug) {
@@ -3451,14 +3474,44 @@ class Server extends EventEmitter__default["default"] {
3451
3474
  if (this.server) {
3452
3475
  this.server.close();
3453
3476
  }
3454
- try {
3455
- fs.unlinkSync(this.file);
3456
- }
3457
- catch (err) {
3458
- /* */
3477
+ // When socket-activated, systemd owns the socket file. Do not unlink
3478
+ // it -- that would break the bind-mount contract for existing clients.
3479
+ if (!this._activated) {
3480
+ try {
3481
+ fs.unlinkSync(this.file);
3482
+ }
3483
+ catch (err) {
3484
+ /* */
3485
+ }
3459
3486
  }
3460
3487
  }
3461
3488
  listen() {
3489
+ const inheritedFd = systemdListenFd();
3490
+ if (inheritedFd !== undefined) {
3491
+ this._activated = true;
3492
+ if (this.debug) {
3493
+ console.log("Server::listen using systemd-activated fd", inheritedFd);
3494
+ }
3495
+ return new Promise((resolve) => {
3496
+ let connected = false;
3497
+ this.server = net__default["default"].createServer(this._onConnection.bind(this));
3498
+ this.server.listen({ fd: inheritedFd }, () => {
3499
+ connected = true;
3500
+ resolve();
3501
+ });
3502
+ this.server.on("error", (err) => {
3503
+ if (!connected) {
3504
+ console.error("Got server error", err);
3505
+ setTimeout(this.listen.bind(this), 1000);
3506
+ }
3507
+ });
3508
+ this.server.on("close", () => {
3509
+ if (!connected) {
3510
+ setTimeout(this.listen.bind(this), 1000);
3511
+ }
3512
+ });
3513
+ });
3514
+ }
3462
3515
  try {
3463
3516
  fs.unlinkSync(this.file); // this should be more
3464
3517
  // complicated with attempts to
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@andersbakken/fisk",
3
- "version": "5.0.2",
3
+ "version": "5.0.3",
4
4
  "description": "Fisk, a distributed compile system",
5
5
  "scripts": {
6
6
  "lint": "eslint . --ext .ts",