|
| 1 | +var _ = require('lodash'), |
| 2 | + { prepareVaultVariableScope, prepareVariablesScope, |
| 3 | + getIterationData, processExecutionResult |
| 4 | + } = require('../util'); |
| 5 | + |
| 6 | +/** |
| 7 | + * Adds options |
| 8 | + * disableSNR:Boolean |
| 9 | + * |
| 10 | + * @type {Object} |
| 11 | + */ |
| 12 | +module.exports = { |
| 13 | + init: function (done) { |
| 14 | + // bail out if iterations are not parallelized |
| 15 | + if (!this.areIterationsParallelized) { |
| 16 | + return done(); |
| 17 | + } |
| 18 | + |
| 19 | + var state = this.state; |
| 20 | + |
| 21 | + // ensure that the environment, globals and collectionVariables are in VariableScope instance format |
| 22 | + prepareVariablesScope(state); |
| 23 | + // prepare the vault variable scope |
| 24 | + prepareVaultVariableScope(state.vaultSecrets); |
| 25 | + |
| 26 | + // create the partition manager and partition the iterations |
| 27 | + this.partitionManager.createPartitions(); |
| 28 | + const { partitions } = this.partitionManager; |
| 29 | + |
| 30 | + // queue a parallel command for each of our partitions |
| 31 | + partitions.forEach((partition) => { |
| 32 | + this.queue('parallel', { |
| 33 | + coords: partition.cursor.current(), |
| 34 | + static: true, |
| 35 | + start: true |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + done(); |
| 40 | + }, |
| 41 | + |
| 42 | + triggers: ['beforeIteration', 'iteration'], |
| 43 | + |
| 44 | + prototype: { |
| 45 | + /** |
| 46 | + * Starts a parallel iteration |
| 47 | + * |
| 48 | + * @param {Number} index - The index of the partition to run |
| 49 | + * @param {Object} localVariables - Local variables for the iteration |
| 50 | + * @param {Function} callback - The callback to call when the iteration is complete |
| 51 | + */ |
| 52 | + startParallelIteration (index, localVariables, callback) { |
| 53 | + this.partitionManager.runSinglePartition(index, localVariables, callback); |
| 54 | + }, |
| 55 | + |
| 56 | + /** |
| 57 | + * Stops a parallel iteration |
| 58 | + * |
| 59 | + * @param {Number} index - The index of the partition to stop |
| 60 | + * @param {Function} callback - The callback to call when the iteration is complete |
| 61 | + */ |
| 62 | + stopParallelIteration (index, callback) { |
| 63 | + this.partitionManager.stopSinglePartition(index, callback); |
| 64 | + } |
| 65 | + }, |
| 66 | + |
| 67 | + process: { |
| 68 | + /** |
| 69 | + * This processor queues partitions in parallel. |
| 70 | + * |
| 71 | + * @param {Object} payload - |
| 72 | + * @param {Object} payload.coords - |
| 73 | + * @param {Boolean} [payload.static=false] - |
| 74 | + * @param {Function} next - |
| 75 | + */ |
| 76 | + parallel (payload, next) { |
| 77 | + var partitionIndex = payload.coords.partitionIndex, |
| 78 | + partition = this.partitionManager.partitions[partitionIndex], |
| 79 | + coords = payload.static ? payload.coords : partition.cursor.whatnext(payload.coords), |
| 80 | + item = this.state.items[coords.position], |
| 81 | + delay; |
| 82 | + |
| 83 | + |
| 84 | + if (coords.empty) { |
| 85 | + return next(); |
| 86 | + } |
| 87 | + |
| 88 | + if (payload.stopRunNow) { |
| 89 | + this.triggers.iteration(null, coords); |
| 90 | + |
| 91 | + return next(); |
| 92 | + } |
| 93 | + |
| 94 | + // if it is a beginning of a run, we need to raise events for iteration start |
| 95 | + if (payload.start) { |
| 96 | + this.triggers.beforeIteration(null, coords); |
| 97 | + } |
| 98 | + |
| 99 | + // since we will never reach coords.eof for some partitions because each cursor |
| 100 | + // contains cycles for the entire run, we are breaking off early here. |
| 101 | + // this has been done to keep the contract of a cursor intact. |
| 102 | + // cycles is defined as "number of iterations in the run" |
| 103 | + if (coords.iteration === partition.startIndex + coords.partitionCycles) { |
| 104 | + this.triggers.iteration(null, payload.coords); |
| 105 | + |
| 106 | + return next(); |
| 107 | + } |
| 108 | + |
| 109 | + if (coords.cr) { |
| 110 | + delay = _.get(this.options, 'delay.iteration', 0); |
| 111 | + |
| 112 | + this.triggers.iteration(null, payload.coords); |
| 113 | + this.triggers.beforeIteration(null, coords); |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | + if (coords.eof) { |
| 118 | + this.triggers.iteration(null, coords); |
| 119 | + |
| 120 | + return next(); |
| 121 | + } |
| 122 | + |
| 123 | + this.queueDelay(function () { |
| 124 | + this.queue('item', { |
| 125 | + item: item, |
| 126 | + coords: coords, |
| 127 | + data: getIterationData(this.state.data, coords.iteration + partition.startIndex), |
| 128 | + environment: partition.variables.environment, |
| 129 | + globals: partition.variables.globals, |
| 130 | + vaultSecrets: this.state.vaultSecrets, |
| 131 | + collectionVariables: partition.variables.collectionVariables, |
| 132 | + _variables: partition.variables._variables |
| 133 | + }, function (executionError, executions) { |
| 134 | + // Use shared utility function to process execution results and handle SNR logic |
| 135 | + var result = processExecutionResult({ |
| 136 | + coords: coords, |
| 137 | + executions: executions, |
| 138 | + executionError: executionError, |
| 139 | + runnerOptions: this.options, |
| 140 | + snrHash: this.snrHash, |
| 141 | + items: this.state.items |
| 142 | + }), |
| 143 | + nextCoords, |
| 144 | + seekingToStart, |
| 145 | + stopRunNow; |
| 146 | + |
| 147 | + // Update the snrHash if it was created/updated by the utility function |
| 148 | + this.snrHash = result.snrHash; |
| 149 | + |
| 150 | + nextCoords = result.nextCoords; |
| 151 | + seekingToStart = result.seekingToStart; |
| 152 | + stopRunNow = result.stopRunNow; |
| 153 | + |
| 154 | + |
| 155 | + partition.cursor.seek(nextCoords.position, nextCoords.iteration, function (err, chngd, coords) { |
| 156 | + // this condition should never arise, so better throw error when this happens |
| 157 | + if (err) { |
| 158 | + throw err; |
| 159 | + } |
| 160 | + |
| 161 | + this.queue('parallel', { |
| 162 | + coords: { |
| 163 | + ...coords, |
| 164 | + partitionIndex |
| 165 | + }, |
| 166 | + static: seekingToStart, |
| 167 | + stopRunNow: stopRunNow |
| 168 | + }); |
| 169 | + }, this); |
| 170 | + }); |
| 171 | + }.bind(this), { |
| 172 | + time: delay, |
| 173 | + source: 'iteration', |
| 174 | + cursor: coords |
| 175 | + }, next); |
| 176 | + } |
| 177 | + } |
| 178 | +}; |
0 commit comments