-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
262 lines (227 loc) · 7.19 KB
/
index.js
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/**
* index.js
*
* agency is an asynchronous execution flow control utility for node.js
* based on dependencies defined by a logical expression.
*
* Copyright 2016 Fernando Pires
*
* Released under the MIT License.
*
* --------------------------------------------------------
*
* author: Fernando Pires
* version: 1.0.0
*/
/*jshint newcap: false*/
function agency() {
'use strict';
var agent = require('./lib/agent');
var journal = require('./lib/journal');
var jn = journal();
// private properties
var agents = [];
var modes = ['q', 'v', 'l']; // 'Quiet', 'Verbose', 'Log'
var defaultMode = 'v';
var args;
var mode;
var agentInterface;
var err = new Error();
var logFile;
// arguments handling
if (arguments instanceof Array) {
args = arguments;
} else {
args = Array.prototype.slice.call(arguments);
}
if (args[0] !== undefined) {
if (modes.indexOf(args[0]) !== -1) {
mode = args[0];
} else {
mode = defaultMode;
}
} else {
mode = defaultMode;
}
jn.setLogMode(mode);
// returns a boolean value for each dependency of the current agent indicating
// if is was already executed or is queued for execution (see difDepCheck flag)
function hasAgentExecuted(agentsList, dependency, currAgent) {
var ret;
for (var i = 0; i < agentsList.length; i++) {
if (agentsList[i].getId() === dependency) {
if (!currAgent.getDeffDepCheck()) {
return agentsList[i].getExecuted();
} else {
ret = agentsList[i].getExecuted() ? true : agentsList[i].getWaitingExec();
return ret;
}
}
}
err.error = 'InvalidDependency';
err.message = 'execution failed (invalid dependency agent: ' + dependency + ')';
return err;
}
// replaces the agent's dependency list ids with the correspondent boolean
// values to indicate if they were already executed
function replaceByLogicalValue(tokenArray, id, bool) {
var len = tokenArray.length;
var ret = tokenArray.slice();
for (var i = 0; i < len; i++) {
if(ret[i] === id) {
ret[i] = bool;
}
}
return ret;
}
// evaluates the agent's logical dependency list and returns a boolean indicating if the
// dependency agents have completed and therefore allow for the execution of the dependent agent
function evaluateLogicalExpression(agent) {
var len = agent.getDependencies().length;
var expression = agent.getExpression();
var dep;
var agentExecuted;
var ret = agent.getTokenizedExpression();
var logicalEval;
for (var i = 0; i < len; i++) {
dep = agent.getDependencies()[i];
agentExecuted = hasAgentExecuted(agents, dep, agent);
if (agentExecuted instanceof Error) {
return agentExecuted;
} else {
ret = replaceByLogicalValue(ret, dep , agentExecuted);
}
}
logicalEval = eval(ret.join(''));
return logicalEval;
}
// returns a boolean value indicating the end of the
// agency execution by checking if no agents await execution
function hasAgencyCompleted() {
var ret = true;
var len = agents.length;
for (var i = 0; i < len; i++) {
if (agents[i].getWaitingExec()) {
ret = false;
}
}
return ret;
}
// calls the journal's module function that prints the execution
// log on the specified file
function report() {
if (mode === 'l') {
jn.report(logFile);
}
}
// method that allows one agent to communicate with one of its dependencies,
// returns the dependency agent's function result
function getAgentValue(id) {
var len = agents.length;
for (var i = 0; i< len; i++) {
if (agents[i].getId() === id) {
return agents[i].getResult();
}
}
jn.logExecutionEvent(id, 'INFO', 'result could not be retrieved', (new Date()).toJSON());
}
// method called during agent creation to check if there is
// a possible agent id duplication
function isIdInUse(id) {
var ret = false;
var len = agents.length;
for (var i = 0; i < len; i++) {
if (agents[i].getId() === id) {
ret = true;
}
}
return ret;
}
// controls the execution of agents based on the current status and on its dependencies; reports
// when there are no more agents to run
function runAgents() {
var len = agents.length;
var checkDependency;
for (var i = 0; i < len; i++) {
if (!agents[i].getExecuted() && !agents[i].getWaitingExec() && agents[i].getCanExecute()) {
if (agents[i].getExpression() === '') {
agents[i].execute();
} else {
checkDependency = evaluateLogicalExpression(agents[i]);
if (checkDependency instanceof Error) {
jn.logExecutionEvent(agents[i].getId(), 'ERROR', checkDependency.message, (new Date()).toJSON());
} else {
if (checkDependency) {
agents[i].execute();
}
}
}
}
}
if (hasAgencyCompleted()) {
report();
}
}
// sets the name and location of the log file if that was the log method - if not
// defined log will be written in a default directory/file
function setLogFile(fileName) {
logFile = fileName;
}
// returns an object that exposes only the necessary methods needed by the agent module
// to communicate with the agency
function createAgentInterface() {
return {
getAgentValue: getAgentValue,
isIdInUse: isIdInUse,
runAgents: runAgents,
report: report
};
}
agentInterface = createAgentInterface();
// creates a new agent; receives an obligatory id and an optional dependency list;
// returns an external interface with a subset of its methods
function createAgent(id, expr) {
var ag;
var validId;
var parsedExpression;
var agentExternalInterface;
jn.logCreationEvent(id, 'INFO', 'creation initialized', (new Date()).toJSON());
ag = agent(agentInterface, jn);
validId = ag.validateId(id);
if (!(validId instanceof Error)) {
ag.setId(id);
jn.logCreationEvent(id, 'INFO', 'id validation completed', (new Date()).toJSON());
parsedExpression = ag.parseExpression(expr);
if (!(parsedExpression instanceof Error)) {
jn.logCreationEvent(id, 'INFO', 'dependency expression validated', (new Date()).toJSON());
ag.setExecuted(false);
ag.setWaitingExec(false);
ag.setDeffDepCheck(false);
ag.setHaltExecution(false);
ag.setCanExecute(true);
jn.logCreationEvent(id, 'INFO', 'creation completed', (new Date()).toJSON());
} else {
ag.setCanExecute(false);
jn.logCreationEvent(id, 'ERROR', parsedExpression.message, (new Date()).toJSON());
}
} else {
ag.setCanExecute(false);
jn.logCreationEvent(id, 'ERROR', validId.message, (new Date()).toJSON());
}
agents.push(ag);
agentExternalInterface = {
setFunction: ag.setFunction,
setCallback: ag.setCallback,
setHaltExecution: ag.setHaltExecution,
setDeffDepCheck: ag.setDeffDepCheck
};
return agentExternalInterface;
}
// public interface
return {
createAgent: createAgent,
runAgents: runAgents,
setLogFile: setLogFile
};
}
module.exports = agency;