diff --git a/src/docker.js b/src/docker.js index 4b778a61c..a78ff4f48 100644 --- a/src/docker.js +++ b/src/docker.js @@ -334,6 +334,7 @@ function createSubcontainer(app, name, cmd, options, callback) { containerOptions = _.extend(containerOptions, options); gConnection.createContainer(containerOptions, function (error, container) { + if (error && error.statusCode === 409) return callback(new BoxError(BoxError.ALREADY_EXISTS, error)); if (error) return callback(new BoxError(BoxError.DOCKER_ERROR, error)); callback(null, container); diff --git a/src/scheduler.js b/src/scheduler.js index 41eeca1e7..5a8b30ea2 100644 --- a/src/scheduler.js +++ b/src/scheduler.js @@ -7,6 +7,7 @@ exports = module.exports = { let apps = require('./apps.js'), assert = require('assert'), async = require('async'), + BoxError = require('./boxerror.js'), constants = require('./constants.js'), CronJob = require('cron').CronJob, debug = require('debug')('box:scheduler'), @@ -58,6 +59,7 @@ function createCronJobs(app, schedulerConfig, callback) { const cmd = schedulerConfig[taskName].command; docker.createSubcontainer(app, containerName, [ '/bin/sh', '-c', cmd ], { } /* options */, function (error) { + if (error && error.reason === BoxError.ALREADY_EXISTS) return iteratorDone(); if (error) return iteratorDone(error); var cronJob = new CronJob({ @@ -90,7 +92,6 @@ function stopJobs(appId, appState, callback) { } const containerName = `${appId}-${taskName}`; - docker.stopContainerByName(containerName, function (error) { if (error) debug(`stopJobs: failed to stop task container with name ${containerName} : ${error.message}`); @@ -120,13 +121,14 @@ function sync() { gState = _.omit(gState, removedAppIds); async.eachSeries(allApps, function (app, iteratorDone) { - debug(`sync: adding jobs of ${app.id}`); var appState = gState[app.id] || null; var schedulerConfig = app.manifest.addons ? app.manifest.addons.scheduler : null; if (!appState && !schedulerConfig) return iteratorDone(); // nothing to do if (appState && _.isEqual(appState.schedulerConfig, schedulerConfig) && appState.cronJobs) return iteratorDone(); // nothing changed + debug(`sync: adding jobs of ${app.id}`); + stopJobs(app.id, appState, function (error) { if (error) debug(`sync: error stopping jobs of ${app.id} : ${error.message}`); @@ -143,6 +145,8 @@ function sync() { iteratorDone(); }); }); + }, function (error) { + if (error) return debug('sync: error creating jobs', error); }); }); });