pipeline.js

RetryOnError

function
RetryOnError()

Option name Type Description
Object onfi

configuration object

Retries to run, if error occures specified number of times

config as Object:

  • stage
      evaluating stage
    
  • retry
      number that limits number of retries
    
  • retry
      Function that decide either to run or to stop trying
    
function RetryOnError(config) {

	var self = this;

	if (!(self instanceof RetryOnError)) {
		throw new Error('constructor is not a function');
	}

	if (config && config.run instanceof Function) {
		config.stage = new Stage(config.run);
		delete config.run;
	}

	Stage.apply(self, arguments);

	if (config.stage instanceof Stage) {
		self.stage = config.stage;
	} else if (config.stage instanceof Function) {
		self.stage = new Stage(config.stage);
	} else {
		self.stage = new Empty();
	}

	if (config) {
		if (config.retry) {
			// function, count
			if (typeof config.retry !== 'function') {
				config.retry *= 1; // To get NaN is wrong type
			}
			if (config.retry)
				self.retry = config.retry;
		}
	} else {
		self.retry = 1;
	}
}

retry

method
RetryOnError.prototype.retry()

Option name Type Description
err Error,Object,any

error that is examined

ctx Context

main context

iter Number

current iteration: 0 is the run, but 1... retry couner

internal declaration fo combine

RetryOnError.prototype.retry = function(err, ctx, iter) {
	// 0 means that run once 1 and more than one;
	return iter <= 1;
};

reportName

method
RetryOnError.prototype.reportName()

override of reportName

RetryOnError.prototype.reportName = function() {
	return "RetryOnError:" + this.name;
};

backupContext

method
RetryOnError.prototype.backupContext()

Option name Type Description
ctx Context

main context

override of reportName

RetryOnError.prototype.backupContext = function(ctx) {
	return ctx.toObject();
};

restoreContext

method
RetryOnError.prototype.restoreContext()

Option name Type Description
ctx Context

main context

backup Context

main context

override of reportName

RetryOnError.prototype.restoreContext = function(ctx, backup) {
	ctx.overwrite(backup);
	// ctx.__errors.length = 0;
};

compile

method
RetryOnError.prototype.compile()

override of compile
provide a way to compose retry run.

RetryOnError.prototype.compile = function() {

	var self = this;

	if (!self.name) {
		self.name = "stage: " + self.stage.reportName() + " with retry " + self.retry + " times";
	}

	var run = function(ctx, done) {
		// backup context object to overwrite if needed
		var backup = self.backupContext(ctx);

		reachEnd = function(err, iter) {
			if (err) {
				if (self.retry instanceof Function) {
					return !self.retry(err, ctx, iter);
				} else { // number
					return iter > self.retry;
				}
			} else {
				return true;
			}
		};
		var iter = -1;
		var next = function(err, ctx) {
			iter++;
			if (reachEnd(err, iter)) {
				done(err);
			} else {
				// clean changes of existing before values.
				// may be will need to clear at all and rewrite ? i don't know yet.
				self.restoreContext(ctx, backup);
				self.stage.execute(ctx, next);
			}
		};
		self.stage.execute(ctx, next);
	};

	self.run = run;
};

execute

method
RetryOnError.prototype.execute()

Option name Type Description
context Context

evaluating context

[callback] Context

returning callback

override of execute

RetryOnError.prototype.execute = function(context, callback) {
	var self = this;
	if (!self.run) {
		self.compile();
	}
	RetryOnError.super_.prototype.execute.apply(self, arguments);
};