feat: Improve workflow management by making runMaster asynchronous and adding error handling

This commit is contained in:
2025-11-15 12:47:09 +01:00
parent cf0611d841
commit e7f8f7110c

View File

@@ -205,7 +205,7 @@ export class MicrosoftRewardsBot {
// Only cluster when there's more than 1 cluster demanded
if (this.config.clusters > 1) {
if (cluster.isPrimary) {
this.runMaster()
await this.runMaster()
} else if (cluster.worker) {
await this.runWorker()
} else {
@@ -279,15 +279,17 @@ export class MicrosoftRewardsBot {
return this.accountSummaries
}
private runMaster() {
private runMaster(): Promise<void> {
return new Promise((resolve) => {
log('main', 'MAIN-PRIMARY', 'Primary process started')
const totalAccounts = this.accounts.length
// Validate accounts exist
if (totalAccounts === 0) {
log('main', 'MAIN-PRIMARY', 'No accounts found to process. Exiting.', 'warn')
process.exit(0)
log('main', 'MAIN-PRIMARY', 'No accounts found to process. Nothing to do.', 'warn')
resolve()
return
}
// If user over-specified clusters (e.g. 10 clusters but only 2 accounts), don't spawn useless idle workers.
@@ -299,6 +301,19 @@ export class MicrosoftRewardsBot {
// Store worker-to-chunk mapping for crash recovery
const workerChunkMap = new Map<number, Account[]>()
let resolved = false
const finishRun = async () => {
if (resolved) return
resolved = true
try {
await this.sendConclusion(this.accountSummaries)
} catch (e) {
log('main', 'CONCLUSION', `Failed to send conclusion: ${e instanceof Error ? e.message : String(e)}`, 'warn')
}
log('main', 'MAIN-WORKER', 'All workers destroyed. Run complete.', 'warn')
resolve()
}
for (let i = 0; i < workerCount; i++) {
const worker = cluster.fork()
const chunk = accountChunks[i] || []
@@ -363,18 +378,10 @@ export class MicrosoftRewardsBot {
// Check if all workers have exited
if (this.activeWorkers === 0) {
// All workers done -> send conclusion and exit (update check moved to startup)
(async () => {
try {
await this.sendConclusion(this.accountSummaries)
} catch (e) {
log('main', 'CONCLUSION', `Failed to send conclusion: ${e instanceof Error ? e.message : String(e)}`, 'warn')
}
log('main', 'MAIN-WORKER', 'All workers destroyed. Exiting main process!', 'warn')
process.exit(0)
})()
void finishRun()
}
})
})
}
private async runWorker() {