querysub 0.246.0 → 0.248.0

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "querysub",
3
- "version": "0.246.0",
3
+ "version": "0.248.0",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "note1": "note on node-forge fork, see https://github.com/digitalbazaar/forge/issues/744 for details",
@@ -97,9 +97,54 @@ export async function streamScreenOutput(config: {
97
97
  return (async () => {
98
98
  try {
99
99
  const pipeFile = `${root}${screenName}/pipe.txt`;
100
+ const tailScript = `${root}${screenName}/smart_tail.sh`;
101
+
102
+ // Create a smart tail script that handles file truncation
103
+ await fs.promises.writeFile(tailScript, `#!/bin/bash
104
+ PIPE_FILE="$1"
105
+
106
+ # Initialize position tracking
107
+ CURRENT_POS=0
108
+
109
+ # Read initial content and get file size
110
+ if [ -f "$PIPE_FILE" ]; then
111
+ CURRENT_SIZE=$(stat -c%s "$PIPE_FILE" 2>/dev/null || wc -c < "$PIPE_FILE")
112
+ # Output initial content like tail would
113
+ if [ $CURRENT_SIZE -gt 0 ]; then
114
+ cat "$PIPE_FILE"
115
+ CURRENT_POS=$CURRENT_SIZE
116
+ fi
117
+ else
118
+ CURRENT_SIZE=0
119
+ fi
120
+
121
+ # Watch for file changes
122
+ while inotifywait -q -e modify "$PIPE_FILE" 2>/dev/null; do
123
+ if [ -f "$PIPE_FILE" ]; then
124
+ NEW_SIZE=$(stat -c%s "$PIPE_FILE" 2>/dev/null || wc -c < "$PIPE_FILE")
125
+
126
+ if [ $NEW_SIZE -lt $CURRENT_SIZE ]; then
127
+ # File has shrunk (truncated), reset position to end of file
128
+ # Don't output anything, just reset our tracking
129
+ CURRENT_POS=$NEW_SIZE
130
+ CURRENT_SIZE=$NEW_SIZE
131
+ elif [ $NEW_SIZE -gt $CURRENT_SIZE ]; then
132
+ # File grew normally, read only the new part
133
+ if [ $CURRENT_POS -lt $NEW_SIZE ]; then
134
+ # Use tail with byte offset to read from current position
135
+ tail -c +$((CURRENT_POS + 1)) "$PIPE_FILE" | head -c $((NEW_SIZE - CURRENT_POS))
136
+ CURRENT_POS=$NEW_SIZE
137
+ fi
138
+ CURRENT_SIZE=$NEW_SIZE
139
+ fi
140
+ # If sizes are equal, no change to output
141
+ fi
142
+ done`);
100
143
 
101
- // Use tail -f to follow the pipe file
102
- childProcess = spawn(`tail`, ["-f", pipeFile, "--lines", "1000"], {
144
+ await runPromise(`chmod +x ${tailScript}`);
145
+
146
+ // Use our smart tail script instead of regular tail
147
+ childProcess = spawn("bash", [tailScript, pipeFile], {
103
148
  stdio: "pipe",
104
149
  });
105
150
 
@@ -267,7 +312,12 @@ const runScreenCommand = measureWrap(async function runScreenCommand(config: {
267
312
  if (pid && await isScreenRunningProcess(pid)) {
268
313
  // It doesn't want to die. Wait longer, but it it just won't die, kill the screen
269
314
  console.warn(`Screen ${screenName} is not dying, giving it another 30 seconds`);
270
- await delay(timeInSecond * 30);
315
+ for (let i = 0; i < 6; i++) {
316
+ await delay(5);
317
+ if (!await isScreenRunningProcess(pid)) {
318
+ break;
319
+ }
320
+ }
271
321
  if (pid && await isScreenRunningProcess(pid)) {
272
322
  console.warn(`Screen ${screenName} is still running, killing it forcefully`);
273
323
  await killScreen({ screenName });
@@ -297,9 +347,6 @@ ${config.command}
297
347
  let pipeScript = path.resolve(config.folder + "../pipe.sh");
298
348
  await fs.promises.writeFile(pipeScript, `#!/bin/bash
299
349
  line_count=0
300
- max_lines_limit=$((${PIPE_FILE_LINE_LIMIT} * 100))
301
- keep_lines_count=$((${PIPE_FILE_LINE_LIMIT} * 50))
302
-
303
350
  while IFS= read -r line; do
304
351
  echo "$line" >> "${pipeFile}"
305
352
  ((line_count++))
@@ -309,9 +356,11 @@ while IFS= read -r line; do
309
356
  if [ -f "${pipeFile}" ]; then
310
357
  # Count total lines in file
311
358
  total_lines=$(wc -l < "${pipeFile}")
312
- if [ "$total_lines" -gt "$max_lines_limit" ]; then
313
- # Keep only the last N lines when file gets too many lines
314
- tail -n "$keep_lines_count" "${pipeFile}" > "${pipeFile}.tmp" && mv "${pipeFile}.tmp" "${pipeFile}"
359
+ if [ "$total_lines" -gt ${PIPE_FILE_LINE_LIMIT} ]; then
360
+ # Wait 2 seconds to give the watcher time to read pending content
361
+ sleep 2
362
+ # Keep only the last ${PIPE_FILE_LINE_LIMIT} lines when file gets too many lines
363
+ tail -n ${Math.floor(PIPE_FILE_LINE_LIMIT / 2)} "${pipeFile}" > "${pipeFile}.tmp" && mv "${pipeFile}.tmp" "${pipeFile}"
315
364
  fi
316
365
  fi
317
366
  fi