@@ -796,36 +796,46 @@ export class C2DEngineDocker extends C2DEngine {
796796 }
797797 }
798798
799- private async setNewTimer ( ) {
799+ private setNewTimer ( ) {
800+ if ( this . cronTimer ) {
801+ return
802+ }
800803 // 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 )
804+ if ( this . envs . length > 0 )
805+ this . cronTimer = setTimeout ( this . InternalLoop . bind ( this ) , this . cronTime )
803806 }
804807
805808 private async InternalLoop ( ) {
806809 // this is the internal loop of docker engine
807810 // 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 )
811+ if ( this . cronTimer ) {
812+ clearTimeout ( this . cronTimer )
813+ this . cronTimer = null
814+ }
815+ try {
816+ // get all running jobs
817+ const jobs = await this . db . getRunningJobs ( this . getC2DConfig ( ) . hash )
812818
813- if ( jobs . length === 0 ) {
814- CORE_LOGGER . info ( 'No C2D jobs found for engine ' + this . getC2DConfig ( ) . hash )
819+ if ( jobs . length === 0 ) {
820+ CORE_LOGGER . info ( 'No C2D jobs found for engine ' + this . getC2DConfig ( ) . hash )
821+ this . setNewTimer ( )
822+ return
823+ } else {
824+ CORE_LOGGER . info ( `Got ${ jobs . length } jobs for engine ${ this . getC2DConfig ( ) . hash } ` )
825+ CORE_LOGGER . debug ( JSON . stringify ( jobs ) )
826+ }
827+ const promises : any = [ ]
828+ for ( const job of jobs ) {
829+ promises . push ( this . processJob ( job ) )
830+ }
831+ // wait for all promises, there is no return
832+ await Promise . all ( promises )
833+ } catch ( e ) {
834+ CORE_LOGGER . error ( `Error in C2D InternalLoop: ${ e . message } ` )
835+ } finally {
836+ // set the cron again
815837 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 ) )
820- }
821- const promises : any = [ ]
822- for ( const job of jobs ) {
823- promises . push ( this . processJob ( job ) )
824838 }
825- // wait for all promises, there is no return
826- await Promise . all ( promises )
827- // set the cron again
828- this . setNewTimer ( )
829839 }
830840
831841 private async createDockerContainer (
0 commit comments