@camunda8/cli 4.7.0-alpha.39 → 4.7.0-alpha.40

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.
@@ -26,7 +26,7 @@ import {
26
26
  } from 'node:fs';
27
27
  import { once } from 'node:events';
28
28
  import { chmod } from 'node:fs/promises';
29
- import { homedir, platform as osPlatform, arch as osArch } from 'node:os';
29
+ import { homedir, platform as osPlatform, arch as osArch, constants as osConstants } from 'node:os';
30
30
  import { join, dirname, basename, isAbsolute, relative, resolve } from 'node:path';
31
31
  import { finished } from 'node:stream/promises';
32
32
  import { StringDecoder } from 'node:string_decoder';
@@ -1432,7 +1432,11 @@ async function downloadC8Run(config) {
1432
1432
  return { archivePath: targetFile, etag: result.etag };
1433
1433
  }
1434
1434
 
1435
- async function extractArchive(archivePath, targetDir) {
1435
+ // How long an aborted extractor gets to exit before it is killed outright.
1436
+ const EXTRACTOR_KILL_GRACE_MS = 3000;
1437
+
1438
+ /** Aborting `abortSignal` terminates the extractor; the promise settles once it has exited. */
1439
+ async function extractArchive(archivePath, targetDir, { abortSignal } = {}) {
1436
1440
  const logger = getLogger();
1437
1441
  logger.info(`Extracting to ${targetDir}...`);
1438
1442
 
@@ -1446,6 +1450,17 @@ async function extractArchive(archivePath, targetDir) {
1446
1450
 
1447
1451
  return new Promise((resolve, reject) => {
1448
1452
  const proc = spawn(command, args, { stdio: 'inherit' });
1453
+ let killTimer;
1454
+ const terminate = () => {
1455
+ proc.kill('SIGTERM');
1456
+ killTimer = setTimeout(() => proc.kill('SIGKILL'), EXTRACTOR_KILL_GRACE_MS);
1457
+ };
1458
+ if (abortSignal?.aborted) terminate();
1459
+ else abortSignal?.addEventListener('abort', terminate, { once: true });
1460
+ proc.on('close', () => {
1461
+ clearTimeout(killTimer);
1462
+ abortSignal?.removeEventListener('abort', terminate);
1463
+ });
1449
1464
  proc.on('exit', (code, signal) => {
1450
1465
  if (code === 0) {
1451
1466
  logger.info('Extraction complete.');
@@ -1767,10 +1782,35 @@ async function installGABuild(config, archivePath, installDir) {
1767
1782
  const stagedDir = join(stagingRoot, `c8run-${config.version}`);
1768
1783
  // Dot-prefixed so neither dir is ever listed as an installed version ("c8run-*").
1769
1784
  const backupDir = join(config.cacheDir, `.replaced-c8run-${config.version}-${process.pid}`);
1785
+ // Leftovers of runs that were killed outright (SIGKILL, power loss) cannot clean up after themselves.
1786
+ sweepStaleInstallDirs(config.cacheDir);
1770
1787
  rmSync(stagingRoot, { recursive: true, force: true });
1771
1788
 
1789
+ // `finally` does not run when a signal ends the process, so clean up here.
1790
+ // The swap below is synchronous, so a signal can only land while extracting.
1791
+ const extraction = new AbortController();
1792
+ let extracting = Promise.resolve();
1793
+ let interrupted = false;
1794
+ const signalHandlers = INTERRUPT_SIGNALS.map((signal) => {
1795
+ const handler = async () => {
1796
+ interrupted = true;
1797
+ extraction.abort();
1798
+ // The extractor must be gone first, or it can recreate files after the cleanup.
1799
+ await extracting.catch(() => {});
1800
+ removeStagingRoot(stagingRoot);
1801
+ process.exit(128 + osConstants.signals[signal]);
1802
+ };
1803
+ process.on(signal, handler);
1804
+ return [signal, handler];
1805
+ });
1806
+
1772
1807
  try {
1773
- await extractArchive(archivePath, stagedDir);
1808
+ extracting = extractArchive(archivePath, stagedDir, { abortSignal: extraction.signal });
1809
+ // On an interrupt the handler owns the shutdown; do not let the abort error unwind into the caller first.
1810
+ await extracting.catch((error) => {
1811
+ if (interrupted) return new Promise(() => {});
1812
+ throw error;
1813
+ });
1774
1814
  const stagedBinary = findC8RunBinaryPath({ cacheDir: stagingRoot, version: config.version });
1775
1815
  if (!stagedBinary) {
1776
1816
  throw new Error(`The downloaded c8run ${config.version} archive does not contain a c8run launcher.`);
@@ -1809,10 +1849,53 @@ async function installGABuild(config, archivePath, installDir) {
1809
1849
  }
1810
1850
  if (replacing) rmSync(backupDir, { recursive: true, force: true });
1811
1851
  } finally {
1852
+ for (const [signal, handler] of signalHandlers) process.off(signal, handler);
1812
1853
  rmSync(stagingRoot, { recursive: true, force: true });
1813
1854
  }
1814
1855
  }
1815
1856
 
1857
+ const INTERRUPT_SIGNALS = ['SIGINT', 'SIGTERM'];
1858
+
1859
+ function removeStagingRoot(stagingRoot) {
1860
+ try {
1861
+ rmSync(stagingRoot, { recursive: true, force: true, maxRetries: 5, retryDelay: 50 });
1862
+ } catch {
1863
+ // Best effort: the next install or delete/purge sweeps what is left.
1864
+ }
1865
+ }
1866
+
1867
+ const STALE_INSTALL_DIR = /^\.(staging|replaced)-(c8run-.+)-(\d+)$/;
1868
+
1869
+ /**
1870
+ * Clean up after `.staging-c8run-*` and `.replaced-c8run-*` dirs whose owning
1871
+ * c8ctl (the trailing pid) is gone. They are dot-prefixed, so `cluster list`
1872
+ * and `cluster delete` never see them. Dirs of a live pid belong to a
1873
+ * concurrent install and are left alone.
1874
+ *
1875
+ * A staging dir is only ever a partial extraction, so it is deleted. A
1876
+ * `.replaced` dir is the previous install, set aside just before the swap; if
1877
+ * its owner died between the two renames it is the only copy left, so it is
1878
+ * moved back when the install dir is missing and deleted only when redundant.
1879
+ */
1880
+ export function sweepStaleInstallDirs(cacheDir) {
1881
+ if (!existsSync(cacheDir)) return;
1882
+ for (const entry of readdirSync(cacheDir, { withFileTypes: true })) {
1883
+ const match = entry.isDirectory() && STALE_INSTALL_DIR.exec(entry.name);
1884
+ if (!match || isPidAlive(Number(match[3]))) continue;
1885
+ const dir = join(cacheDir, entry.name);
1886
+ try {
1887
+ const installDir = join(cacheDir, match[2]);
1888
+ if (match[1] === 'replaced' && !existsSync(installDir)) {
1889
+ renameSync(dir, installDir);
1890
+ } else {
1891
+ rmSync(dir, { recursive: true, force: true });
1892
+ }
1893
+ } catch {
1894
+ // Best effort: housekeeping must not fail the command that triggered it.
1895
+ }
1896
+ }
1897
+ }
1898
+
1816
1899
  // ---------------------------------------------------------------------------
1817
1900
  // Process management
1818
1901
  // ---------------------------------------------------------------------------
@@ -4125,6 +4208,10 @@ export const commands = {
4125
4208
  process.exit(1);
4126
4209
  }
4127
4210
 
4211
+ if (parsed.subcommand === 'delete' || parsed.subcommand === 'purge') {
4212
+ sweepStaleInstallDirs(getCacheDir());
4213
+ }
4214
+
4128
4215
  if (parsed.subcommand === 'delete') {
4129
4216
  try {
4130
4217
  await deleteVersion(getCacheDir(), parsed.version);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@camunda8/cli",
3
- "version": "4.7.0-alpha.39",
3
+ "version": "4.7.0-alpha.40",
4
4
  "description": "Camunda 8 CLI - minimal-dependency CLI for Camunda 8 operations",
5
5
  "type": "module",
6
6
  "engines": {