all files / src/ StateTransitionBehavior.js

90.35% Statements 103/114
81.82% Branches 81/99
91.67% Functions 11/12
96.15% Lines 100/104
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196                                                      14×   13× 13× 13× 13× 13× 13× 13×   13× 39×         13×   27×   13×     13×   34×       11×               13× 13× 13× 13×   13×         13×               13×           13× 13× 13×     13×   13×                             13× 10×               10×              
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.handleTaskflowChange = handleTaskflowChange;
 
var _equals = require('mout/src/array/equals');
 
var _equals2 = _interopRequireDefault(_equals);
 
var _workflows = require('workflows');
 
var _workflows2 = _interopRequireDefault(_workflows);
 
var _projects = require('./redux/actions/projects');
 
var ProjectActions = _interopRequireWildcard(_projects);
 
var _taskflows = require('./redux/actions/taskflows');
 
var TaskflowActions = _interopRequireWildcard(_taskflows);
 
var _clusters = require('./redux/actions/clusters');
 
var ClusterActions = _interopRequireWildcard(_clusters);
 
var _fs = require('./redux/actions/fs');
 
var FSActions = _interopRequireWildcard(_fs);
 
var _AccessHelper = require('./utils/AccessHelper');
 
var _get = require('./utils/get');
 
var _get2 = _interopRequireDefault(_get);
 
var _redux = require('./redux');
 
function _interopRequireWildcard(obj) { Eif (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
var simulationsStatus = {};
 
function folderItemSize(state, folderId) {
  var folder = state.fs.folderMapById[folderId];
  if (folder) {
    var itemChildrenLength = folder.itemChildren ? folder.itemChildren.length : 0;
    var folderChildrenLength = folder.folderChildren ? folder.folderChildren.length : 0;
    return itemChildrenLength + folderChildrenLength;
  }
  return 0;
}
 
function handleTaskflowChange(state, taskflow) {
  if (!taskflow) {
    return;
  }
  var primaryJob = taskflow.primaryJob;
  var jobs = [];
  var tasks = [];
  var outputDirectory = [];
  var actions = [];
  try {
    jobs = Object.keys(taskflow.jobMapById).map(function (id) {
      return taskflow.jobMapById[id];
    });
    tasks = Object.keys(taskflow.taskMapById).map(function (id) {
      return taskflow.taskMapById[id];
    });
  } catch (e) {
    return;
  }
  var allComplete = jobs.every(function (job) {
    return job.status === 'complete';
  }) && tasks.every(function (task) {
    return task.status === 'complete';
  });
  var simulationStatus = [];
 
  // Figure out possible actions and simulation state
  if (jobs.length && jobs.every(function (job) {
    return job.status === 'terminated';
  }) || tasks.some(function (task) {
    return task.status === 'error';
  }) && (jobs.length === 0 || !jobs.some(function (job) {
    return job.status === 'running';
  }))) {
    simulationStatus.push('terminated');
    actions.push('rerun');
  } else if (!allComplete && jobs.length + tasks.length > 0 && !jobs.some(function (job) {
    return job.status === 'terminating';
  })) {
    simulationStatus.push('running');
 
    // Only allow termination if the cluster is not launching/provisioning ( we can't currently terminate a cluster in launching or provisioning )
    Eif (taskflow.flow.meta && taskflow.flow.meta.cluster) {
      var tfClusterId = taskflow.flow.meta.cluster._id;
      var tfCluster = state.preferences.clusters.mapById[tfClusterId];
      if (tfCluster && ['launching', 'provisioning'].indexOf(tfCluster.status) === -1) {
        actions.push('terminate');
      }
    }
  } else Eif (allComplete) {
    simulationStatus.push('complete');
  }
 
  Eif (taskflow.simulation && state.simulations.mapById[taskflow.simulation]) {
    var simulation = state.simulations.mapById[taskflow.simulation];
    var project = state.projects.mapById[simulation.projectId];
    simulationStatus.push(simulation.metadata.status);
    // Update local store to figure out primaryJob of taskflow if not yet available
    Iif (!primaryJob && taskflow.stepName && project) {
      primaryJob = _workflows2.default[project.type].primaryJobs[taskflow.stepName];
    }
 
    // Need to update simulation status
    if (simulationStatus.length === 2 && simulationStatus[0] !== simulationStatus[1] || simulationsStatus[simulation._id] !== simulationStatus[1]) {
      var metadata = Object.assign({}, simulation.metadata, {
        status: simulationStatus[0]
      });
      var sim = state.simulations.mapById[taskflow.simulation];
      simulationsStatus[simulation._id] = simulationStatus[0];
      Eif ((0, _AccessHelper.userHasAccess)(state.auth.user, simulation.access, 1)) {
        (0, _redux.dispatch)(ProjectActions.saveSimulation(Object.assign({}, simulation, { metadata: metadata })));
      }
      (0, _redux.dispatch)(FSActions.fetchFolder(sim.steps[sim.active].folderId));
    }
  }
 
  // Extract output directory if any
  for (var i = 0; i < jobs.length; i++) {
    if (jobs[i].name === primaryJob) {
      outputDirectory.push(jobs[i].dir);
      break;
    }
  }
 
  // for taskflows on ec2 the meta object is not as readily available
  // this is due to fewer jobs coming through SSE which triggers a fetch for trad clusters.
  Eif (taskflow.flow.meta) {
    var _tfClusterId = taskflow.flow.meta.cluster._id;
    var _tfCluster = state.preferences.clusters.mapById[_tfClusterId];
 
    // if we have no cluster in preferences, but we have an ID fetch it.
    Iif (!_tfCluster && _tfClusterId.length && state.auth.user._id === (0, _get2.default)(_tfCluster, 'userId')) {
      (0, _redux.dispatch)(ClusterActions.fetchCluster(_tfClusterId, taskflow.flow._id));
    } else if (_tfCluster) {
      // add simulation info to cluster config.
      Eif (taskflow.flow.meta.cluster && taskflow.simulation) {
        var tfSimulation = state.simulations.mapById[taskflow.simulation];
        if (tfSimulation && _tfCluster.config && !_tfCluster.config.simulation && state.auth.user._id === _tfCluster.userId) {
          var _simulation = {
            _id: tfSimulation._id,
            name: tfSimulation.name,
            step: taskflow.stepName
          };
          _tfCluster.config.simulation = _simulation;
          (0, _redux.dispatch)(ClusterActions.updateCluster(_tfCluster));
        }
      }
 
      // add the terminate instance button if running or error
      if (_tfCluster.type === 'ec2' && taskflow.flow.status !== 'running' && ['running', 'error'].indexOf(_tfCluster.status) !== -1) {
        actions.push('terminateInstance');
      }
    }
  }
 
  // Update taslkfow meta
  if (allComplete !== taskflow.allComplete || outputDirectory[0] !== taskflow.outputDirectory || !(0, _equals2.default)(actions, taskflow.actions) || primaryJob !== taskflow.primaryJob) {
    (0, _redux.dispatch)(TaskflowActions.updateTaskflowMetadata(taskflow.flow._id, {
      actions: actions,
      allComplete: allComplete,
      outputDirectory: outputDirectory[0],
      primaryJob: primaryJob
    }));
 
    // Update simulation folders when all tasks/jobs are done
    if (allComplete && taskflow.simulation && state.simulations.mapById[taskflow.simulation]) {
      var _sim = state.simulations.mapById[taskflow.simulation];
      var inputFolder = _sim.metadata.inputFolder._id;
      var outputFolder = _sim.metadata.outputFolder._id;
      var activeFolder = _sim.steps[_sim.active].folderId;
      // inputFolder is already a little populated on allComplete
      (0, _redux.dispatch)(FSActions.fetchFolder(inputFolder));
      (0, _redux.dispatch)(FSActions.fetchFolder(activeFolder));
      // outputFolder is not, only get it once.
      if (folderItemSize(state, outputFolder) === 0) {
        (0, _redux.dispatch)(FSActions.fetchFolder(outputFolder));
      }
    }
  }
}