do not error if container already exists

This commit is contained in:
Girish Ramakrishnan
2020-08-18 21:15:54 -07:00
parent 0ae8dc1040
commit 879a6b4202
2 changed files with 7 additions and 2 deletions

View File

@@ -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);
});
});
});