dashcam 1.0.1-beta.9 → 1.1.0-beta.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.
@@ -1,177 +0,0 @@
1
- #!/usr/bin/env node
2
- /**
3
- * Background recording process for dashcam CLI
4
- * This script runs detached from the parent process to handle long-running recordings
5
- */
6
-
7
- import { startRecording, stopRecording } from '../lib/recorder.js';
8
- import { upload } from '../lib/uploader.js';
9
- import { logger, setVerbose } from '../lib/logger.js';
10
- import fs from 'fs';
11
- import path from 'path';
12
- import os from 'os';
13
-
14
- // Get process directory for status files
15
- const PROCESS_DIR = path.join(os.homedir(), '.dashcam-cli');
16
- const STATUS_FILE = path.join(PROCESS_DIR, 'status.json');
17
- const RESULT_FILE = path.join(PROCESS_DIR, 'upload-result.json');
18
-
19
- // Parse options from command line argument
20
- const optionsJson = process.argv[2];
21
- if (!optionsJson) {
22
- console.error('No options provided to background process');
23
- process.exit(1);
24
- }
25
-
26
- const options = JSON.parse(optionsJson);
27
-
28
- // Enable verbose logging in background
29
- setVerbose(true);
30
-
31
- logger.info('Background recording process started', {
32
- pid: process.pid,
33
- options
34
- });
35
-
36
- // Write status file
37
- function writeStatus(status) {
38
- try {
39
- fs.writeFileSync(STATUS_FILE, JSON.stringify({
40
- ...status,
41
- timestamp: Date.now(),
42
- pid: process.pid
43
- }, null, 2));
44
- } catch (error) {
45
- logger.error('Failed to write status file', { error });
46
- }
47
- }
48
-
49
- // Write upload result file
50
- function writeUploadResult(result) {
51
- try {
52
- logger.info('Writing upload result to file', { path: RESULT_FILE, shareLink: result.shareLink });
53
- fs.writeFileSync(RESULT_FILE, JSON.stringify({
54
- ...result,
55
- timestamp: Date.now()
56
- }, null, 2));
57
- logger.info('Successfully wrote upload result to file');
58
- } catch (error) {
59
- logger.error('Failed to write upload result file', { error });
60
- }
61
- }
62
-
63
- // Main recording function
64
- async function runBackgroundRecording() {
65
- let recordingResult = null;
66
- let isShuttingDown = false;
67
-
68
- try {
69
- // Start the recording
70
- const recordingOptions = {
71
- fps: parseInt(options.fps) || 10,
72
- includeAudio: options.audio || false,
73
- customOutputPath: options.output || null
74
- };
75
-
76
- logger.info('Starting recording with options', { recordingOptions });
77
-
78
- recordingResult = await startRecording(recordingOptions);
79
-
80
- // Write status to track the recording
81
- writeStatus({
82
- isRecording: true,
83
- startTime: recordingResult.startTime,
84
- options,
85
- pid: process.pid,
86
- outputPath: recordingResult.outputPath
87
- });
88
-
89
- logger.info('Recording started successfully', {
90
- outputPath: recordingResult.outputPath,
91
- startTime: recordingResult.startTime
92
- });
93
-
94
- // Set up signal handlers for graceful shutdown
95
- const handleShutdown = async (signal) => {
96
- if (isShuttingDown) {
97
- logger.info('Shutdown already in progress...');
98
- return;
99
- }
100
- isShuttingDown = true;
101
-
102
- logger.info(`Received ${signal}, stopping background recording...`);
103
-
104
- try {
105
- // Stop the recording
106
- const stopResult = await stopRecording();
107
-
108
- if (stopResult) {
109
- logger.info('Recording stopped successfully', {
110
- outputPath: stopResult.outputPath,
111
- duration: stopResult.duration
112
- });
113
-
114
- // Upload the recording
115
- logger.info('Starting upload...');
116
- const uploadResult = await upload(stopResult.outputPath, {
117
- title: options.title || 'Dashcam Recording',
118
- description: options.description || 'Recorded with Dashcam CLI',
119
- project: options.project || options.k,
120
- duration: stopResult.duration,
121
- clientStartDate: stopResult.clientStartDate,
122
- apps: stopResult.apps,
123
- logs: stopResult.logs,
124
- gifPath: stopResult.gifPath,
125
- snapshotPath: stopResult.snapshotPath
126
- });
127
-
128
- logger.info('Upload complete', { shareLink: uploadResult.shareLink });
129
-
130
- // Write upload result for stop command to read
131
- writeUploadResult({
132
- shareLink: uploadResult.shareLink,
133
- replayId: uploadResult.replay?.id
134
- });
135
- }
136
-
137
- // Update status to indicate recording stopped
138
- writeStatus({
139
- isRecording: false,
140
- completedTime: Date.now(),
141
- pid: process.pid
142
- });
143
-
144
- logger.info('Background process exiting successfully');
145
- process.exit(0);
146
- } catch (error) {
147
- logger.error('Error during shutdown:', error);
148
- process.exit(1);
149
- }
150
- };
151
-
152
- process.on('SIGINT', () => handleShutdown('SIGINT'));
153
- process.on('SIGTERM', () => handleShutdown('SIGTERM'));
154
-
155
- // Keep the process alive
156
- logger.info('Background recording is now running. Waiting for stop signal...');
157
- await new Promise(() => {}); // Wait indefinitely for signals
158
-
159
- } catch (error) {
160
- logger.error('Background recording failed:', error);
161
-
162
- // Update status to indicate failure
163
- writeStatus({
164
- isRecording: false,
165
- error: error.message,
166
- pid: process.pid
167
- });
168
-
169
- process.exit(1);
170
- }
171
- }
172
-
173
- // Run the background recording
174
- runBackgroundRecording().catch(error => {
175
- logger.error('Fatal error in background process:', error);
176
- process.exit(1);
177
- });
@@ -1,317 +0,0 @@
1
- import { spawn } from 'child_process';
2
- import fs from 'fs';
3
- import path from 'path';
4
- import os from 'os';
5
- import { fileURLToPath } from 'url';
6
- import { logger } from './logger.js';
7
-
8
- const __filename = fileURLToPath(import.meta.url);
9
- const __dirname = path.dirname(__filename);
10
-
11
- // Use a fixed directory in the user's home directory for cross-process communication
12
- const PROCESS_DIR = path.join(os.homedir(), '.dashcam-cli');
13
- const PID_FILE = path.join(PROCESS_DIR, 'recording.pid');
14
- const STATUS_FILE = path.join(PROCESS_DIR, 'status.json');
15
- const RESULT_FILE = path.join(PROCESS_DIR, 'upload-result.json');
16
-
17
- // Ensure process directory exists
18
- if (!fs.existsSync(PROCESS_DIR)) {
19
- fs.mkdirSync(PROCESS_DIR, { recursive: true });
20
- }
21
-
22
- class ProcessManager {
23
- constructor() {
24
- this.isBackgroundMode = false;
25
- this.isStopping = false;
26
- }
27
-
28
- setBackgroundMode(enabled = true) {
29
- this.isBackgroundMode = enabled;
30
- }
31
-
32
- writeStatus(status) {
33
- try {
34
- fs.writeFileSync(STATUS_FILE, JSON.stringify({
35
- ...status,
36
- timestamp: Date.now(),
37
- pid: process.pid
38
- }, null, 2));
39
- } catch (error) {
40
- logger.error('Failed to write status file', { error });
41
- }
42
- }
43
-
44
- readStatus() {
45
- try {
46
- if (!fs.existsSync(STATUS_FILE)) return null;
47
- const data = fs.readFileSync(STATUS_FILE, 'utf8');
48
- return JSON.parse(data);
49
- } catch (error) {
50
- logger.error('Failed to read status file', { error });
51
- return null;
52
- }
53
- }
54
-
55
- writeUploadResult(result) {
56
- try {
57
- logger.info('Writing upload result to file', { path: RESULT_FILE, shareLink: result.shareLink });
58
- fs.writeFileSync(RESULT_FILE, JSON.stringify({
59
- ...result,
60
- timestamp: Date.now()
61
- }, null, 2));
62
- logger.info('Successfully wrote upload result to file');
63
- // Verify it was written
64
- if (fs.existsSync(RESULT_FILE)) {
65
- logger.info('Verified upload result file exists');
66
- } else {
67
- logger.error('Upload result file does not exist after write!');
68
- }
69
- } catch (error) {
70
- logger.error('Failed to write upload result file', { error });
71
- }
72
- }
73
-
74
- readUploadResult() {
75
- try {
76
- if (!fs.existsSync(RESULT_FILE)) return null;
77
- const data = fs.readFileSync(RESULT_FILE, 'utf8');
78
- return JSON.parse(data);
79
- } catch (error) {
80
- logger.error('Failed to read upload result file', { error });
81
- return null;
82
- }
83
- }
84
-
85
- writePid(pid = process.pid) {
86
- try {
87
- fs.writeFileSync(PID_FILE, pid.toString());
88
- } catch (error) {
89
- logger.error('Failed to write PID file', { error });
90
- }
91
- }
92
-
93
- readPid() {
94
- try {
95
- if (!fs.existsSync(PID_FILE)) return null;
96
- const pid = parseInt(fs.readFileSync(PID_FILE, 'utf8').trim());
97
- return isNaN(pid) ? null : pid;
98
- } catch (error) {
99
- return null;
100
- }
101
- }
102
-
103
- isProcessRunning(pid) {
104
- if (!pid) return false;
105
- try {
106
- process.kill(pid, 0); // Signal 0 just checks if process exists
107
- return true;
108
- } catch (error) {
109
- return false;
110
- }
111
- }
112
-
113
- isRecordingActive() {
114
- const pid = this.readPid();
115
- const status = this.readStatus();
116
-
117
- if (!pid || !this.isProcessRunning(pid)) {
118
- // Clean up but preserve upload result in case the background process just finished uploading
119
- this.cleanup({ preserveResult: true });
120
- return false;
121
- }
122
-
123
- return status && status.isRecording;
124
- }
125
-
126
- getActiveStatus() {
127
- if (!this.isRecordingActive()) return null;
128
- return this.readStatus();
129
- }
130
-
131
- cleanup(options = {}) {
132
- const { preserveResult = false } = options;
133
- try {
134
- if (fs.existsSync(PID_FILE)) fs.unlinkSync(PID_FILE);
135
- if (fs.existsSync(STATUS_FILE)) fs.unlinkSync(STATUS_FILE);
136
- if (!preserveResult && fs.existsSync(RESULT_FILE)) fs.unlinkSync(RESULT_FILE);
137
- } catch (error) {
138
- logger.error('Failed to cleanup process files', { error });
139
- }
140
- }
141
-
142
- async stopActiveRecording() {
143
- if (this.isStopping) {
144
- logger.info('Stop already in progress, ignoring additional stop request');
145
- return false;
146
- }
147
-
148
- this.isStopping = true;
149
-
150
- try {
151
- const pid = this.readPid();
152
- const status = this.readStatus();
153
-
154
- if (!pid || !this.isProcessRunning(pid)) {
155
- logger.warn('No active recording process found');
156
- return false;
157
- }
158
-
159
- // Recording is active, send SIGINT to trigger graceful shutdown
160
- logger.info('Stopping active recording process', { pid });
161
- process.kill(pid, 'SIGINT');
162
-
163
- // Wait for the process to actually finish
164
- const maxWaitTime = 30000; // 30 seconds max
165
- const startWait = Date.now();
166
-
167
- while (this.isProcessRunning(pid) && (Date.now() - startWait) < maxWaitTime) {
168
- await new Promise(resolve => setTimeout(resolve, 500));
169
- }
170
-
171
- if (this.isProcessRunning(pid)) {
172
- logger.warn('Process did not stop within timeout, forcing termination');
173
- process.kill(pid, 'SIGKILL');
174
- await new Promise(resolve => setTimeout(resolve, 1000));
175
- }
176
-
177
- // Call the actual recorder's stopRecording function to get complete results
178
- if (status) {
179
- try {
180
- const { stopRecording } = await import('./recorder.js');
181
- const result = await stopRecording();
182
-
183
- logger.info('Recording stopped successfully via recorder', {
184
- outputPath: result.outputPath,
185
- duration: result.duration,
186
- hasLogs: result.logs?.length > 0,
187
- hasApps: result.apps?.length > 0
188
- });
189
-
190
- // Cleanup process files but preserve upload result for stop command
191
- this.cleanup({ preserveResult: true });
192
-
193
- return result;
194
- } catch (recorderError) {
195
- logger.warn('Failed to stop via recorder, falling back to basic result', { error: recorderError.message });
196
-
197
- // Fallback to basic result if recorder fails
198
- const basePath = status.outputPath.substring(0, status.outputPath.lastIndexOf('.'));
199
- const result = {
200
- outputPath: status.outputPath,
201
- gifPath: `${basePath}.gif`,
202
- snapshotPath: `${basePath}.png`,
203
- duration: Date.now() - status.startTime,
204
- clientStartDate: status.startTime,
205
- apps: [],
206
- logs: []
207
- };
208
-
209
- this.cleanup({ preserveResult: true });
210
- return result;
211
- }
212
- } else {
213
- throw new Error('No status information available for active recording');
214
- }
215
- } catch (error) {
216
- logger.error('Failed to stop recording', { error });
217
- throw error;
218
- } finally {
219
- this.isStopping = false;
220
- }
221
- }
222
-
223
- async startRecording(options) {
224
- // Check if recording is already active
225
- if (this.isRecordingActive()) {
226
- throw new Error('Recording already in progress');
227
- }
228
-
229
- // Always run in background mode by spawning a detached process
230
- logger.info('Starting recording in background mode');
231
-
232
- // Get the path to the CLI binary
233
- const binPath = path.join(__dirname, '..', 'bin', 'dashcam-background.js');
234
-
235
- logger.debug('Background process path', { binPath, exists: fs.existsSync(binPath) });
236
-
237
- // Create log files for background process
238
- const logDir = PROCESS_DIR;
239
- const stdoutLog = path.join(logDir, 'background-stdout.log');
240
- const stderrLog = path.join(logDir, 'background-stderr.log');
241
-
242
- const stdoutFd = fs.openSync(stdoutLog, 'a');
243
- const stderrFd = fs.openSync(stderrLog, 'a');
244
-
245
- // Spawn a detached process that will handle the recording
246
- const backgroundProcess = spawn(process.execPath, [
247
- binPath,
248
- JSON.stringify(options)
249
- ], {
250
- detached: true,
251
- stdio: ['ignore', stdoutFd, stderrFd], // Log stdout and stderr
252
- env: {
253
- ...process.env,
254
- DASHCAM_BACKGROUND: 'true'
255
- }
256
- });
257
-
258
- // Close the file descriptors in the parent process
259
- fs.closeSync(stdoutFd);
260
- fs.closeSync(stderrFd);
261
-
262
- // Get the background process PID before unreffing
263
- const backgroundPid = backgroundProcess.pid;
264
-
265
- // Allow the parent process to exit independently
266
- backgroundProcess.unref();
267
-
268
- // Wait a moment for the background process to initialize
269
- await new Promise(resolve => setTimeout(resolve, 2000));
270
-
271
- // Read the status file to get recording details
272
- const status = this.readStatus();
273
-
274
- if (!status || !status.isRecording) {
275
- throw new Error('Background process failed to start recording');
276
- }
277
-
278
- // Write PID file so other commands can find the background process
279
- this.writePid(status.pid);
280
-
281
- logger.info('Background recording process started', {
282
- pid: status.pid,
283
- outputPath: status.outputPath
284
- });
285
-
286
- return {
287
- pid: status.pid,
288
- outputPath: status.outputPath,
289
- startTime: status.startTime
290
- };
291
- }
292
-
293
- async gracefulExit() {
294
- logger.info('Graceful exit requested');
295
-
296
- // If we're currently recording, stop it properly
297
- if (this.isRecordingActive()) {
298
- try {
299
- logger.info('Stopping active recording before exit');
300
- await this.stopActiveRecording();
301
- logger.info('Recording stopped successfully during graceful exit');
302
- } catch (error) {
303
- logger.error('Failed to stop recording during graceful exit', { error });
304
- this.cleanup(); // Fallback cleanup
305
- }
306
- } else {
307
- // Just cleanup if no recording is active
308
- this.cleanup();
309
- }
310
-
311
- process.exit(0);
312
- }
313
- }
314
-
315
- const processManager = new ProcessManager();
316
-
317
- export { processManager };