mirror of
https://github.com/LightZirconite/Microsoft-Rewards-Bot.git
synced 2026-01-11 09:46:16 +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
|
// Only cluster when there's more than 1 cluster demanded
|
||||||
if (this.config.clusters > 1) {
|
if (this.config.clusters > 1) {
|
||||||
if (cluster.isPrimary) {
|
if (cluster.isPrimary) {
|
||||||
this.runMaster()
|
await this.runMaster()
|
||||||
} else if (cluster.worker) {
|
} else if (cluster.worker) {
|
||||||
await this.runWorker()
|
await this.runWorker()
|
||||||
} else {
|
} else {
|
||||||
@@ -279,15 +279,17 @@ export class MicrosoftRewardsBot {
|
|||||||
return this.accountSummaries
|
return this.accountSummaries
|
||||||
}
|
}
|
||||||
|
|
||||||
private runMaster() {
|
private runMaster(): Promise<void> {
|
||||||
|
return new Promise((resolve) => {
|
||||||
log('main', 'MAIN-PRIMARY', 'Primary process started')
|
log('main', 'MAIN-PRIMARY', 'Primary process started')
|
||||||
|
|
||||||
const totalAccounts = this.accounts.length
|
const totalAccounts = this.accounts.length
|
||||||
|
|
||||||
// Validate accounts exist
|
// Validate accounts exist
|
||||||
if (totalAccounts === 0) {
|
if (totalAccounts === 0) {
|
||||||
log('main', 'MAIN-PRIMARY', 'No accounts found to process. Exiting.', 'warn')
|
log('main', 'MAIN-PRIMARY', 'No accounts found to process. Nothing to do.', 'warn')
|
||||||
process.exit(0)
|
resolve()
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// If user over-specified clusters (e.g. 10 clusters but only 2 accounts), don't spawn useless idle workers.
|
// 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
|
// Store worker-to-chunk mapping for crash recovery
|
||||||
const workerChunkMap = new Map<number, Account[]>()
|
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++) {
|
for (let i = 0; i < workerCount; i++) {
|
||||||
const worker = cluster.fork()
|
const worker = cluster.fork()
|
||||||
const chunk = accountChunks[i] || []
|
const chunk = accountChunks[i] || []
|
||||||
@@ -363,18 +378,10 @@ export class MicrosoftRewardsBot {
|
|||||||
|
|
||||||
// Check if all workers have exited
|
// Check if all workers have exited
|
||||||
if (this.activeWorkers === 0) {
|
if (this.activeWorkers === 0) {
|
||||||
// All workers done -> send conclusion and exit (update check moved to startup)
|
void finishRun()
|
||||||
(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)
|
|
||||||
})()
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
private async runWorker() {
|
private async runWorker() {
|
||||||
|
|||||||
Reference in New Issue
Block a user