Optimize log file writes using stream pooling (#2835)

* Initial plan

* Implement LogStreamManager for optimized log writing

Co-authored-by: whyour <22700758+whyour@users.noreply.github.com>

* Fix error handler in LogStreamManager to avoid race conditions

Co-authored-by: whyour <22700758+whyour@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: whyour <22700758+whyour@users.noreply.github.com>
This commit is contained in:
Copilot
2025-11-16 21:11:10 +08:00
committed by GitHub
parent fbeb4f4a6c
commit 08ef509e27
4 changed files with 132 additions and 12 deletions
+6 -3
View File
@@ -24,6 +24,7 @@ import pickBy from 'lodash/pickBy';
import omit from 'lodash/omit';
import { writeFileWithLock } from '../shared/utils';
import { ScheduleType } from '../interface/schedule';
import { logStreamManager } from '../shared/logStreamManager';
@Service()
export default class CronService {
@@ -516,7 +517,7 @@ export default class CronService {
{ where: { id } },
);
cp.stdout.on('data', async (data) => {
await fs.appendFile(absolutePath, data.toString());
await logStreamManager.write(absolutePath, data.toString());
});
cp.stderr.on('data', async (data) => {
this.logger.info(
@@ -524,7 +525,7 @@ export default class CronService {
command,
data.toString(),
);
await fs.appendFile(absolutePath, data.toString());
await logStreamManager.write(absolutePath, data.toString());
});
cp.on('error', async (err) => {
this.logger.error(
@@ -532,7 +533,7 @@ export default class CronService {
command,
err,
);
await fs.appendFile(absolutePath, JSON.stringify(err));
await logStreamManager.write(absolutePath, JSON.stringify(err));
});
cp.on('exit', async (code) => {
@@ -541,6 +542,8 @@ export default class CronService {
JSON.stringify(params),
code,
);
// Close the stream after task completion
await logStreamManager.closeStream(absolutePath);
await CrontabModel.update(
{ status: CrontabStatus.idle, pid: undefined },
{ where: { id } },