fdb2 1.0.10 → 1.0.11

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.
Files changed (3) hide show
  1. package/bin/fdb2.js +99 -10
  2. package/package.json +1 -1
  3. package/server.pid +0 -1
package/bin/fdb2.js CHANGED
@@ -120,24 +120,44 @@ async function startProject() {
120
120
  // 日志文件路径
121
121
  const logFilePath = path.join(projectRoot, 'server.log');
122
122
 
123
- // 创建日志文件的写入流
124
- const out = fs.openSync(logFilePath, 'a');
125
- const err = fs.openSync(logFilePath, 'a');
123
+ // 安全地创建日志文件写入流,忽略错误
124
+ function createLogFileStream(logFilePath) {
125
+ try {
126
+ const fd = fs.openSync(logFilePath, 'a');
127
+ console.log(`Log file created: ${logFilePath}`);
128
+ return fd;
129
+ } catch (error) {
130
+ console.warn(`Failed to create log file ${logFilePath}: ${error.message}`);
131
+ console.warn('Logs will only be written to console.');
132
+ return null;
133
+ }
134
+ }
135
+
136
+ // 创建日志文件流
137
+ const logFileFd = createLogFileStream(logFilePath);
126
138
 
127
139
  // 使用 node 命令启动服务器(异步,后台运行)
128
140
  const child = spawn(cmd, args, {
129
141
  cwd: projectRoot,
130
142
  detached: true,
131
- stdio: ['ignore', out, err]
143
+ stdio: ['ignore', logFileFd || 'inherit', logFileFd || 'inherit']
132
144
  });
133
145
 
134
146
  // 解除父子进程关联,让子进程在后台独立运行
135
147
  child.unref();
136
148
 
137
149
  // 保存 PID 到文件
138
- fs.writeFileSync(pidFilePath, child.pid.toString());
150
+ try {
151
+ fs.writeFileSync(pidFilePath, child.pid.toString());
152
+ } catch (error) {
153
+ console.warn(`Failed to write PID file ${pidFilePath}: ${error.message}`);
154
+ }
139
155
 
140
- console.log('Logs are written to:', logFilePath);
156
+ if (logFileFd) {
157
+ console.log('Logs are written to:', logFilePath);
158
+ } else {
159
+ console.log('Logs are written to console only');
160
+ }
141
161
  console.log('Server started successfully with PID:', child.pid);
142
162
  console.log('Server is running in the background');
143
163
  console.log(`Server is running at http://localhost:${port}`);
@@ -160,19 +180,88 @@ function stopProject() {
160
180
  const pid = parseInt(fs.readFileSync(pidFilePath, 'utf8'));
161
181
  console.log(`Stopping server process with PID: ${pid}`);
162
182
 
163
- // 发送终止信号
164
- process.kill(pid);
183
+ // 发送终止信号 - 尝试不同的信号
184
+ try {
185
+ // 首先尝试 SIGTERM (15) - 正常终止
186
+ process.kill(pid, 'SIGTERM');
187
+ console.log('SIGTERM signal sent');
188
+
189
+ // 等待一段时间(2秒)
190
+ const startTime = Date.now();
191
+ const maxWaitTime = 2000; // 2秒
192
+ let processExists = true;
193
+
194
+ while (Date.now() - startTime < maxWaitTime) {
195
+ try {
196
+ // 检查进程是否还存在
197
+ process.kill(pid, 0);
198
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 100); // 等待100ms
199
+ } catch (err) {
200
+ if (err.code === 'ESRCH') {
201
+ processExists = false;
202
+ break;
203
+ }
204
+ }
205
+ }
206
+
207
+ // 如果进程仍然存在,尝试 SIGKILL (9) - 强制终止
208
+ if (processExists) {
209
+ console.log('Process still exists, sending SIGKILL...');
210
+ try {
211
+ process.kill(pid, 'SIGKILL');
212
+ console.log('SIGKILL signal sent');
213
+
214
+ // 再等待500ms
215
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 500);
216
+ } catch (killErr) {
217
+ // SIGKILL 失败可能表示我们没有权限
218
+ if (killErr.code === 'EPERM') {
219
+ console.error('Insufficient permissions to kill process');
220
+ console.error('Try running with sudo or manually kill the process:');
221
+ console.error(` sudo kill -9 ${pid}`);
222
+ }
223
+ throw killErr;
224
+ }
225
+ }
226
+ } catch (killError) {
227
+ // 如果 kill 操作失败,检查错误类型
228
+ if (killError.code === 'EPERM') {
229
+ // 权限不足
230
+ console.error('Insufficient permissions to kill process');
231
+ console.error('Try running with sudo or manually kill the process:');
232
+ console.error(` sudo kill -9 ${pid}`);
233
+ throw killError;
234
+ } else if (killError.code === 'ESRCH') {
235
+ // 进程不存在
236
+ console.log('Process not found');
237
+ } else {
238
+ throw killError;
239
+ }
240
+ }
165
241
 
166
242
  // 删除 PID 文件
167
- fs.unlinkSync(pidFilePath);
243
+ try {
244
+ fs.unlinkSync(pidFilePath);
245
+ console.log('PID file removed');
246
+ } catch (unlinkError) {
247
+ console.warn(`Failed to remove PID file: ${unlinkError.message}`);
248
+ }
249
+
168
250
  console.log('Server stopped successfully');
169
251
  } catch (error) {
170
252
  // 如果进程不存在(ESRCH 错误),也删除 PID 文件
171
253
  if (error.code === 'ESRCH') {
172
254
  console.log('Server process not found, cleaning up PID file');
173
255
  if (fs.existsSync(pidFilePath)) {
174
- fs.unlinkSync(pidFilePath);
256
+ try {
257
+ fs.unlinkSync(pidFilePath);
258
+ } catch (e) {
259
+ // 忽略删除失败
260
+ }
175
261
  }
262
+ } else if (error.code === 'EPERM') {
263
+ // 权限不足的错误已经在上面处理过
264
+ console.error('Cannot stop server due to insufficient permissions');
176
265
  } else {
177
266
  console.error('Failed to stop server:', error.message);
178
267
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fdb2",
3
- "version": "1.0.10",
3
+ "version": "1.0.11",
4
4
  "private": false,
5
5
  "type": "commonjs",
6
6
  "main": "view/index.html",
package/server.pid DELETED
@@ -1 +0,0 @@
1
- 43286