mirror of
https://github.com/LightZirconite/Microsoft-Rewards-Bot.git
synced 2026-01-10 01:06:17 +00:00
feat: Improve workflow management by making runMaster asynchronous and adding error handling
This commit is contained in:
35
src/index.ts
35
src/index.ts
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user