File size: 2,258 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { execSync } from 'node:child_process'
import { promisify } from 'util'

/**
 * Returns array of PIDs that were started by the given parent PID.
 */
export const getChildProcesses = (parentPid: number): number[] => {
  try {
    // Get all running processes
    const allProcesses = execSync('ps -e -o pid= -o ppid=')
      .toString()
      .trim()
      .split('\n')

    // Filter out processes whose PPID matches the given PID
    const childPids = allProcesses
      .map((line) => line.trim().split(/\s+/))
      .filter(([, ppid]) => Number(ppid) === parentPid)
      .map(([pid]) => Number(pid))

    return childPids
  } catch (e) {
    console.error('Failed to get child processes', e)
    return []
  }
}

/**
 * Returns the command line binary and args.
 */
export const getProcessBinary = (pid: number): string => {
  try {
    const binary = execSync(`ps -p ${pid} -o command=`).toString().trim()
    return binary
  } catch (e) {
    console.error('Failed to get process binary', e)
    return 'unknown'
  }
}

const sleep = promisify(setTimeout)

/**
 * Tries to kill the given PID with the given signal.
 * Returns true if the signal was sent successfully.
 */
const tryKill = (pid: number, signal: NodeJS.Signals): boolean => {
  try {
    process.kill(pid, signal)
    return true
  } catch {
    return false
  }
}

export default async function globalTeardown(): Promise<void> {
  const shellPIDs = getChildProcesses(process.pid).filter((pid) =>
    getProcessBinary(pid).includes('./bin/goalert.cover'),
  )

  const goalertPIDs = shellPIDs.flatMap((pid) => getChildProcesses(pid))
  console.log('Found GoAlert PIDs:', goalertPIDs.map(getProcessBinary))

  // for loop over goalertPIDs, and send SIGTERM to each process.

  // eslint-disable-next-line no-labels
  nextPID: for (const pid of goalertPIDs) {
    console.log(`Killing process ${pid}...`)
    let limit = 5
    while (tryKill(pid, 'SIGINT')) {
      if (limit-- <= 0) {
        console.log(`Process ${pid} did not exit after 5 seconds, skipping...`)

        // eslint-disable-next-line no-labels
        continue nextPID
      }
      console.log(`Waiting for process ${pid} to exit...`)
      await sleep(100)
    }

    console.log(`Process ${pid} exited.`)
  }
}