-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
102 lines (69 loc) · 2.31 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
if (!process.env.NODE_ENV) process.env.NODE_ENV = 'production';
var AuthClient = require('crp-auth-client');
var TaskClient = require('crp-task-client');
var TaskProducerClient = require('crp-task-producer-client');
//Authentication using your CrowdProcess login information
AuthClient.login('email', 'password', function(err, credential) {
console.log('Logging onto server:', err ? 'FAILURE' : 'SUCCESS');
if(err) return;
//Options for creating task and dataunit stream
var options = {
bid: 1,
program: 'function Run(d){return d;}', //Most basic CrowdProcess tasks
credential: credential
};
createTask(options);
});
function createTask(options) {
var taskClient = TaskClient({
credential: options.credential
});
//Create CrowdProcess task
taskClient.tasks.create({
bid: options.bid,
program: options.program
}, afterTaskCreated);
function afterTaskCreated(err, task) {
console.log('Creating task:', err ? 'FAILURE' : 'SUCCESS');
if(err) return;
//Create dataunit stream to send dataunits directly to CrowdProcess
var stream = TaskProducerClient({
credential: options.credential,
taskId: task._id
});
//Counter for the different responses emitted by CrowdProcess
var sent = 0;
var received = 0;
var errors = 0;
var faults = 0;
console.log('Sending dataunits...');
for (var i = 0; i < 5; i++) {
stream.write(i);
};
console.log('Waiting to receive dataunits...');
var id = setTimeout(function() {
console.log("Sorry...It took too long to receive the first result!");
process.exit(1);
}, 15000);
//Catch errors emited by CrowdProcess
stream.on('error', function() {
errors++;
});
//Catch faults emited by CrowdProcess
stream.on('fault', function() {
faults++;
});
//Receive results from CrowdProcess
stream.on('result', function(data) {
if(received === 0) clearTimeout(id);
received++;
});
//Result stream from CrowdProcess ended
stream.once('end', function() {
console.log('Received all results:', sent - received ? 'FAILURE' : 'SUCCESS');
console.log('Encountered errors:', errors ? 'YES' : 'NO');
console.log('Encountered faults:', faults ? 'YES' : 'NO');
console.log('Done!');
});
}
}