Skip to content

Commit

Permalink
Foolproof unit set
Browse files Browse the repository at this point in the history
  • Loading branch information
willnode committed Jun 16, 2024
1 parent b324e05 commit aa7510e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
7 changes: 3 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const startTime = Date.now();
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = dirname(fileURLToPath(import.meta.url));
Expand All @@ -8,13 +7,13 @@ import app from './src/index.js';

const port = process.env.PORT ? parseInt(process.env.PORT) : 2223;
const server = app.listen(port, function () {
console.log(`Start time takes ` + (Date.now() - startTime) / 1000 + ` s`)
console.log(`Start time takes ` + Math.round(process.uptime() * 1000) / 1000 + `s`)
console.log(`Listening on ${port} in ${process.env.NODE_ENV} mode`);
})
server.on('close', () => {
console.log(`Server closing`);
setTimeout(() => {
console.log(`Server force closing`);
process.exit(0);
console.log(`Server force closing`);
process.exit(0);
}, 2000).unref();
})
8 changes: 6 additions & 2 deletions src/controllers/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ export default function () {
var router = express.Router();
router.get('/*', async function (req, res, next) {
try {
res.json(JSON.parse((await unitExec.get(req.path)).stdout));
let result = await unitExec.get(req.path);
res.header("content-type", "application/json")
res.status(200).send(result.stdout);
} catch (error) {
next(error);
}
});
router.post('/*', async function (req, res, next) {
try {
res.json(JSON.parse((await unitExec.set(req.path, JSON.stringify(req.body))).stdout));
let result = await unitExec.set(req.path, JSON.stringify(req.body));
res.header("content-type", "application/json")
res.status(200).send(result.stdout);
} catch (error) {
next(error);
}
Expand Down
2 changes: 1 addition & 1 deletion sudoutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ switch (cli.args.shift()) {
// just in case
if (!unit.killed)
unit.kill();
}, 1000 * 60).unref();
}, 1000 * 600).unref();
break;
case 'VIRTUAL_SERVER_GET':
arg = cli.args.shift();
Expand Down

0 comments on commit aa7510e

Please sign in to comment.