Skip to content

Commit 97f1cc1

Browse files
committed
use set timeout instead of interval
1 parent 1098c9c commit 97f1cc1

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

src/components/c2d/compute_engine_docker.ts

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -796,36 +796,47 @@ export class C2DEngineDocker extends C2DEngine {
796796
}
797797
}
798798

799-
private async setNewTimer() {
799+
private setNewTimer() {
800+
// Prevent multiple timers from being created
801+
if (this.cronTimer) {
802+
return
803+
}
800804
// don't set the cron if we don't have compute environments
801-
if ((await this.getComputeEnvironments()).length > 0)
802-
this.cronTimer = setInterval(this.InternalLoop.bind(this), this.cronTime)
805+
if (this.envs.length > 0) {
806+
this.cronTimer = setTimeout(this.InternalLoop.bind(this), this.cronTime)
807+
}
803808
}
804809

805810
private async InternalLoop() {
806811
// this is the internal loop of docker engine
807812
// gets list of all running jobs and process them one by one
808-
clearInterval(this.cronTimer)
809-
this.cronTimer = null
810-
// get all running jobs
811-
const jobs = await this.db.getRunningJobs(this.getC2DConfig().hash)
812813

813-
if (jobs.length === 0) {
814-
CORE_LOGGER.info('No C2D jobs found for engine ' + this.getC2DConfig().hash)
815-
this.setNewTimer()
816-
return
817-
} else {
818-
CORE_LOGGER.info(`Got ${jobs.length} jobs for engine ${this.getC2DConfig().hash}`)
819-
CORE_LOGGER.debug(JSON.stringify(jobs))
814+
if (this.cronTimer) {
815+
clearTimeout(this.cronTimer)
816+
this.cronTimer = null
820817
}
821-
const promises: any = []
822-
for (const job of jobs) {
823-
promises.push(this.processJob(job))
818+
819+
try {
820+
// get all running jobs
821+
const jobs = await this.db.getRunningJobs(this.getC2DConfig().hash)
822+
823+
if (jobs.length === 0) {
824+
CORE_LOGGER.info('No C2D jobs found for engine ' + this.getC2DConfig().hash)
825+
} else {
826+
CORE_LOGGER.info(`Got ${jobs.length} jobs for engine ${this.getC2DConfig().hash}`)
827+
CORE_LOGGER.debug(JSON.stringify(jobs))
828+
829+
const promises: any = []
830+
for (const job of jobs) {
831+
promises.push(this.processJob(job))
832+
}
833+
await Promise.all(promises)
834+
}
835+
} catch (err) {
836+
CORE_LOGGER.error(`Error in C2D InternalLoop: ${err.message}`)
837+
} finally {
838+
this.setNewTimer()
824839
}
825-
// wait for all promises, there is no return
826-
await Promise.all(promises)
827-
// set the cron again
828-
this.setNewTimer()
829840
}
830841

831842
private async createDockerContainer(

0 commit comments

Comments
 (0)