Move state enums to the model code

This commit is contained in:
Girish Ramakrishnan
2019-08-30 13:12:49 -07:00
parent b4cbf63519
commit dd0fb8292c
15 changed files with 118 additions and 156 deletions

View File

@@ -53,6 +53,29 @@ exports = module.exports = {
ETASK_STOPPED: 'task_stopped', // user stopped a task
ETASK_CRASHED: 'task_crashed', // apptask crashed
// installation codes (keep in sync in UI)
ISTATE_PENDING_INSTALL: 'pending_install', // installs and fresh reinstalls
ISTATE_PENDING_CLONE: 'pending_clone', // clone
ISTATE_PENDING_CONFIGURE: 'pending_configure', // config (location, port) changes and on infra update
ISTATE_PENDING_UNINSTALL: 'pending_uninstall', // uninstallation
ISTATE_PENDING_RESTORE: 'pending_restore', // restore to previous backup or on upgrade
ISTATE_PENDING_UPDATE: 'pending_update', // update from installed state preserving data
ISTATE_PENDING_BACKUP: 'pending_backup', // backup the app. this is state because it blocks other operations
ISTATE_ERROR: 'error', // error executing last pending_* command
ISTATE_INSTALLED: 'installed', // app is installed
// run states
RSTATE_RUNNING: 'running',
RSTATE_PENDING_START: 'pending_start',
RSTATE_PENDING_STOP: 'pending_stop',
RSTATE_STOPPED: 'stopped', // app stopped by us
// health states (keep in sync in UI)
HEALTH_HEALTHY: 'healthy',
HEALTH_UNHEALTHY: 'unhealthy',
HEALTH_ERROR: 'error',
HEALTH_DEAD: 'dead',
// exported for testing
_validatePortBindings: validatePortBindings,
_validateAccessRestriction: validateAccessRestriction,
@@ -604,8 +627,8 @@ function scheduleTask(appId, args, values, callback) {
if (error && (error.crashed || error.stopped)) {
debug(`Apptask crashed/stopped: ${error.message}`);
const code = error.crashed ? exports.ETASK_CRASHED : exports.ETASK_STOPPED;
appdb.update(appId, { installationState: appdb.ISTATE_ERROR, error: { code: code, message: error.message }, taskId: null }, NOOP_CALLBACK);
} else if (values.installationState !== appdb.ISTATE_PENDING_UNINSTALL) {
appdb.update(appId, { installationState: exports.ISTATE_ERROR, error: { code: code, message: error.message }, taskId: null }, NOOP_CALLBACK);
} else if (values.installationState !== exports.ISTATE_PENDING_UNINSTALL) {
appdb.update(appId, { taskId: null }, NOOP_CALLBACK);
}
});
@@ -744,7 +767,7 @@ function install(data, user, auditSource, callback) {
const restoreConfig = backupId ? { backupId: backupId, backupFormat: backupFormat } : null;
scheduleTask(appId, { restoreConfig }, { installationState: appdb.ISTATE_PENDING_INSTALL }, function (error, result) {
scheduleTask(appId, { restoreConfig }, { installationState: exports.ISTATE_PENDING_INSTALL }, function (error, result) {
if (error) return callback(error);
eventlog.add(eventlog.ACTION_APP_INSTALL, auditSource, { appId: appId, app: result });
@@ -768,7 +791,7 @@ function configure(appId, data, user, auditSource, callback) {
if (error) return callback(error);
if (app.taskId) return callback(new AppsError(AppsError.BAD_STATE, `Not allowed in this app state : ${app.installationState} / ${app.runState}`));
let domain, location, portBindings, values = { installationState: appdb.ISTATE_PENDING_CONFIGURE };
let domain, location, portBindings, values = { installationState: exports.ISTATE_PENDING_CONFIGURE };
if ('location' in data && 'domain' in data) {
location = values.location = data.location.toLowerCase();
@@ -963,7 +986,7 @@ function update(appId, data, auditSource, callback) {
updateConfig.memoryLimit = updateConfig.manifest.memoryLimit;
}
scheduleTask(appId, { updateConfig: updateConfig }, { installationState: appdb.ISTATE_PENDING_UPDATE }, function (error, result) {
scheduleTask(appId, { updateConfig: updateConfig }, { installationState: exports.ISTATE_PENDING_UPDATE }, function (error, result) {
if (error) return callback(error);
eventlog.add(eventlog.ACTION_APP_UPDATE, auditSource, { appId: appId, toManifest: manifest, fromManifest: app.manifest, force: data.force, app: app });
@@ -1058,7 +1081,7 @@ function restore(appId, data, auditSource, callback) {
if (error) return callback(error);
let values = {
installationState: appdb.ISTATE_PENDING_RESTORE,
installationState: exports.ISTATE_PENDING_RESTORE,
manifest: backupInfo.manifest
};
@@ -1153,7 +1176,7 @@ function clone(appId, data, user, auditSource, callback) {
var newAppId = uuid.v4();
var data = {
installationState: appdb.ISTATE_PENDING_CLONE,
installationState: exports.ISTATE_PENDING_CLONE,
memoryLimit: app.memoryLimit,
accessRestriction: app.accessRestriction,
sso: !!app.sso,
@@ -1172,7 +1195,7 @@ function clone(appId, data, user, auditSource, callback) {
const restoreConfig = { backupId: backupId, backupFormat: backupInfo.format };
scheduleTask(newAppId, { restoreConfig }, { installationState: appdb.ISTATE_PENDING_CLONE }, function (error, result) {
scheduleTask(newAppId, { restoreConfig }, { installationState: exports.ISTATE_PENDING_CLONE }, function (error, result) {
if (error) return callback(error);
eventlog.add(eventlog.ACTION_APP_CLONE, auditSource, { appId: newAppId, oldAppId: appId, backupId: backupId, oldApp: app, newApp: result });
@@ -1204,7 +1227,7 @@ function uninstall(appId, auditSource, callback) {
if (error && error.reason === AppstoreError.EXTERNAL_ERROR) return callback(new AppsError(AppsError.EXTERNAL_ERROR, error.message));
if (error) return callback(new AppsError(AppsError.INTERNAL_ERROR, error));
scheduleTask(appId, { /* args */ }, { installationState: appdb.ISTATE_PENDING_UNINSTALL }, function (error, result) {
scheduleTask(appId, { /* args */ }, { installationState: exports.ISTATE_PENDING_UNINSTALL }, function (error, result) {
if (error) return callback(error);
eventlog.add(eventlog.ACTION_APP_UNINSTALL, auditSource, { appId: appId, app: result });
@@ -1225,7 +1248,7 @@ function start(appId, callback) {
if (error) return callback(error);
if (app.taskId) return callback(new AppsError(AppsError.BAD_STATE, `Not allowed in this app state : ${app.installationState} / ${app.runState}`));
scheduleTask(appId, { /* args */ }, { runState: appdb.RSTATE_PENDING_START }, callback);
scheduleTask(appId, { /* args */ }, { runState: exports.RSTATE_PENDING_START }, callback);
});
}
@@ -1239,7 +1262,7 @@ function stop(appId, callback) {
if (error) return callback(error);
if (app.taskId) return callback(new AppsError(AppsError.BAD_STATE, `Not allowed in this app state : ${app.installationState} / ${app.runState}`));
scheduleTask(appId, { /* args */ }, { runState: appdb.RSTATE_PENDING_STOP }, callback);
scheduleTask(appId, { /* args */ }, { runState: exports.RSTATE_PENDING_STOP }, callback);
});
}
@@ -1272,7 +1295,7 @@ function exec(appId, options, callback) {
get(appId, function (error, app) {
if (error) return callback(error);
if (app.installationState !== appdb.ISTATE_INSTALLED || app.runState !== appdb.RSTATE_RUNNING) {
if (app.installationState !== exports.ISTATE_INSTALLED || app.runState !== exports.RSTATE_RUNNING) {
return callback(new AppsError(AppsError.BAD_STATE, 'App not installed or running'));
}
@@ -1383,7 +1406,7 @@ function backup(appId, callback) {
if (error) return callback(error);
if (app.taskId) return callback(new AppsError(AppsError.BAD_STATE, `Not allowed in this app state : ${app.installationState} / ${app.runState}`));
scheduleTask(appId, { /* args */ }, { installationState: appdb.ISTATE_PENDING_BACKUP }, (error, result) => {
scheduleTask(appId, { /* args */ }, { installationState: exports.ISTATE_PENDING_BACKUP }, (error, result) => {
if (error) return callback(error);
callback(null, { taskId: result.taskId });
@@ -1425,7 +1448,7 @@ function restoreInstalledApps(callback) {
appdb.update(app.id, { taskId: null }, function (error) { // clear any stale taskId
if (error) debug(`Error marking ${app.fqdn} for restore: ${JSON.stringify(error)}`);
scheduleTask(app.id, { restoreConfig }, { installationState: appdb.ISTATE_PENDING_RESTORE }, () => iteratorDone()); // always succeed
scheduleTask(app.id, { restoreConfig }, { installationState: exports.ISTATE_PENDING_RESTORE }, () => iteratorDone()); // always succeed
});
});
}, callback);
@@ -1444,7 +1467,7 @@ function configureInstalledApps(callback) {
appdb.update(app.id, { taskId: null }, function (error) { // clear any stale taskId
if (error) debug(`Error marking ${app.fqdn} for reconfigure: ${JSON.stringify(error)}`);
scheduleTask(app.id, { oldConfig: getAppConfig(app) }, { installationState: appdb.ISTATE_PENDING_CONFIGURE }, () => iteratorDone()); // always succeed
scheduleTask(app.id, { oldConfig: getAppConfig(app) }, { installationState: exports.ISTATE_PENDING_CONFIGURE }, () => iteratorDone()); // always succeed
});
}, callback);
});