shiveshnavin commited on
Commit
4983042
·
1 Parent(s): 1a2d279

Add proxy render

Browse files
Files changed (1) hide show
  1. proxy-renderer.js +75 -24
proxy-renderer.js CHANGED
@@ -10,7 +10,37 @@ const { GenerateScript } = await import('./src/GenerateScript.cjs');
10
 
11
  const originalManuScriptPath = path.join(process.cwd(), 'public/original_manuscript.json');
12
  let cmd = `npm run preview`;
13
- var childProcess = exec(cmd);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  export const renderProxy = async (outFile, options) => {
15
  const ScriptStr = fs.readFileSync(originalManuScriptPath);
16
  const ScriptInput = JSON.parse(ScriptStr);
@@ -74,33 +104,54 @@ export const renderProxy = async (outFile, options) => {
74
  axios.post('http://localhost:3000/api/render', renderOptions).then(resp => {
75
  console.log('Studio started render', resp.data)
76
 
77
- childProcess.stdout.on('data', (data) => {
78
- console.log(data)
79
- if (data.includes('Cleanup: Closing browser instance')) {
80
- try {
81
- resolve(outFile)
82
- }
83
- catch (e) { }
84
- }
85
- });
86
- childProcess.stderr.on('data', (data) => {
87
- console.error(data)
88
  try {
89
- if (data.includes('Failed to render')) {
90
- reject(data)
91
- }
92
  } catch (e) { }
93
-
94
- });
95
- childProcess.on('close', (code) => {
96
- console.log('Render studio exited. Restarting...');
97
- setTimeout(() => {
98
- childProcess = exec(cmd);
99
- }, 2000)
100
  try {
101
- reject(data)
102
  } catch (e) { }
103
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  }).catch(reject)
105
  })
106
  }
 
10
 
11
  const originalManuScriptPath = path.join(process.cwd(), 'public/original_manuscript.json');
12
  let cmd = `npm run preview`;
13
+ let childProcess = null;
14
+
15
+ function startChildProcess() {
16
+ if (childProcess && !childProcess.killed) {
17
+ return childProcess;
18
+ }
19
+
20
+ childProcess = exec(cmd);
21
+
22
+ childProcess.on('error', (err) => {
23
+ console.error('Preview child process error:', err);
24
+ });
25
+
26
+ childProcess.on('close', (code, signal) => {
27
+ console.log('Render studio exited with', code, signal, '. Restarting in 2s...');
28
+ // clear reference so future callers can recreate
29
+ childProcess = null;
30
+ setTimeout(() => {
31
+ try {
32
+ startChildProcess();
33
+ } catch (e) {
34
+ console.error('Failed to restart preview process', e);
35
+ }
36
+ }, 2000);
37
+ });
38
+
39
+ return childProcess;
40
+ }
41
+
42
+ // start immediately
43
+ startChildProcess();
44
  export const renderProxy = async (outFile, options) => {
45
  const ScriptStr = fs.readFileSync(originalManuScriptPath);
46
  const ScriptInput = JSON.parse(ScriptStr);
 
104
  axios.post('http://localhost:3000/api/render', renderOptions).then(resp => {
105
  console.log('Studio started render', resp.data)
106
 
107
+ const proc = startChildProcess();
108
+
109
+ let settled = false;
110
+
111
+ const cleanupHandlers = () => {
 
 
 
 
 
 
112
  try {
113
+ if (proc && proc.stdout && stdoutHandler) proc.stdout.removeListener('data', stdoutHandler);
 
 
114
  } catch (e) { }
 
 
 
 
 
 
 
115
  try {
116
+ if (proc && proc.stderr && stderrHandler) proc.stderr.removeListener('data', stderrHandler);
117
  } catch (e) { }
118
+ };
119
+
120
+ const stdoutHandler = (chunk) => {
121
+ const data = String(chunk);
122
+ console.log(data);
123
+ if (data.includes('Cleanup: Closing browser instance')) {
124
+ if (settled) return;
125
+ settled = true;
126
+ cleanupHandlers();
127
+ resolve(outFile);
128
+ }
129
+ };
130
+
131
+ const stderrHandler = (chunk) => {
132
+ const data = String(chunk);
133
+ console.error(data);
134
+ if (data.includes('Failed to render')) {
135
+ if (settled) return;
136
+ settled = true;
137
+ cleanupHandlers();
138
+ reject(new Error(data));
139
+ }
140
+ };
141
+
142
+ if (proc && proc.stdout) proc.stdout.on('data', stdoutHandler);
143
+ if (proc && proc.stderr) proc.stderr.on('data', stderrHandler);
144
+
145
+ // safety: if proc exits before we resolve, clean up and let the caller retry
146
+ const onProcClose = () => {
147
+ if (settled) return;
148
+ settled = true;
149
+ cleanupHandlers();
150
+ reject(new Error('Preview process exited before render finished'));
151
+ };
152
+
153
+ proc.once('close', onProcClose);
154
+
155
  }).catch(reject)
156
  })
157
  }