-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
executable file
·69 lines (60 loc) · 1.92 KB
/
app.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
#!/usr/bin/env node
const scheduler = 'https://scheduler.distributed.computer';
async function main() {
const compute = require('dcp/compute');
let startTime;
const job = compute.for(
['red', 'green', 'yellow', 'blue', 'brown', 'orange', 'pink'],
(colour) => {
console.log(colour);
progress();
return colour;
},
);
job.on('accepted', (event) => {
console.log(' - Job accepted by scheduler, waiting for results');
console.log(` - Job has id ${job.id}`);
startTime = Date.now();
});
job.on('complete', (event) => {
console.log(
`Job Finished, total runtime = ${
Math.round((Date.now() - startTime) / 100) / 10
}s`,
);
});
job.on('readystatechange', (event) => {
console.log(`New ready state: ${event}`);
});
job.on('status', (event) => {
console.log('Received status update:', event);
});
job.on('console', ({ message }) => {
console.log('Received console message:', message);
});
job.on('result', ({ result, sliceNumber }) => {
console.log(
` - Received result for slice ${sliceNumber} at ${
Math.round((Date.now() - startTime) / 100) / 10
}s`,
);
console.log(` * Wow! ${result} is such a pretty colour!`);
});
job.on('error', (event) => {
console.error('Received Error:', event);
});
job.public.name = 'Events Example - Nodejs';
job.public.description = 'DCP-Client Example - examples/node/events.js';
// This is the default behaviour - change if you have multiple bank accounts
// const wallet = require('dcp/wallet');
// const ks = await wallet.get(); /* usually loads ~/.dcp/default.keystore */
// job.setPaymentAccountKeystore(ks);
const results = await job.exec(compute.marketValue);
// OR
// const results = await job.localExec();
console.log('Results are: ', results.values());
}
require('dcp-client')
.init(scheduler)
.then(main)
.finally(() => setImmediate(process.exit));