@@ -16,6 +16,7 @@ const {
1616 logErrors,
1717 setConnectionAborted
1818} = require ( './middleware.js' )
19+ const { JobQueue } = require ( './JobQueue' )
1920const router = require ( './router.js' )
2021const { formatDate, log, compileTypeScript, compileTypeScriptProject } = require ( './utilities.js' )
2122const { shouldUseWebpack } = require ( './utils.js' )
@@ -36,6 +37,18 @@ const state = {
3637 assembles : { }
3738}
3839
40+ const jobQueue = new JobQueue ( )
41+
42+ function hasActiveJobs ( ) {
43+ const activeTypescriptJobs = Object . keys ( state . typescriptJobs ) . length > 0
44+ const activeAssemblyJobs = Object . keys ( state . assembles ) . length > 0
45+ const activeQueues =
46+ jobQueue . getJobs ( 'download' ) . length > 0 ||
47+ jobQueue . getJobs ( 'compile' ) . length > 0
48+
49+ return activeTypescriptJobs || activeAssemblyJobs || activeQueues
50+ }
51+
3952/**
4053 * Adds the compilation of the branch to the job registry
4154 * Returns the promise
@@ -176,18 +189,24 @@ APP.listen(PORT)
176189
177190// Clean up the tmp folder every now and then
178191setInterval ( async ( ) => {
179- // Clean only after a certain amount of branches and when there are no jobs running
180- if ( await shouldClean ( ) ) {
181- log ( 0 , 'Cleaning up...' )
182- await cleanUp ( ) . catch ( error => {
183- console . log ( 'Cleanup failed' , error )
184- } )
192+ if ( hasActiveJobs ( ) ) {
193+ log ( 1 , 'Skipping cleanup while jobs are active' )
194+ return
185195 }
196+
197+ if ( ! ( await shouldClean ( ) ) ) {
198+ return
199+ }
200+
201+ log ( 0 , 'Cleaning up...' )
202+ await cleanUp ( ) . catch ( error => {
203+ log ( 2 , `Cleanup failed: ${ error . message } ` )
204+ } )
186205} , config . cleanInterval || 2 * 60 * 1000 )
187206
188207// Do a cleanup when restarting the server
189- cleanUp ( ) . catch ( ( ) => {
190- log ( 0 , ' Cleanup failed. Likely nothing to cleanup' )
208+ cleanUp ( ) . catch ( error => {
209+ log ( 2 , ` Cleanup failed on startup: ${ error . message } ` )
191210} )
192211
193212module . exports = {
0 commit comments