dcp-client 4.4.9-0 → 4.4.10-0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/dist/dcp-client-bundle.js +1 -1
- package/dist/dcp-client-bundle.js.map +1 -1
- package/docs/CODEOWNERS +30 -44
- package/examples/nodejs/remote-data/simple-job-remote-data-pattern.js +121 -0
- package/examples/nodejs/remote-data/simple-job-remote-function.js +111 -0
- package/examples/nodejs/remote-data/simple-job-remote-input.js +127 -0
- package/examples/nodejs/simple-job-es6.mjs +60 -0
- package/examples/nodejs/simple-job.js +62 -0
- package/examples/nodejs/simple-worker.js +120 -8
- package/libexec/sandbox/bootstrap.js +1 -1
- package/libexec/sandbox/bravojs-env.js +1 -4
- package/libexec/sandbox/lift-webgpu.js +8 -37
- package/package.json +1 -1
- package/examples/nodejs/deploy-job.mjs +0 -28
- package/examples/nodejs/events.js +0 -67
- package/examples/nodejs/minimal.js +0 -39
- package/examples/nodejs/remote-data/dataPattern.js +0 -92
- package/examples/nodejs/remote-data/dataSet.js +0 -98
- package/examples/nodejs/remote-data/dataURL.js +0 -99
- package/examples/nodejs/workFunctionURL.js +0 -66
package/docs/CODEOWNERS
CHANGED
|
@@ -1,18 +1,9 @@
|
|
|
1
|
-
# @file
|
|
2
|
-
#
|
|
3
|
-
#
|
|
4
|
-
# See https://docs.gitlab.com/ee/user/project/code_owners.html
|
|
5
|
-
#
|
|
6
|
-
# @author Bryan Hoang <bryan@distributive.network>
|
|
7
|
-
# @date August 2022
|
|
8
|
-
|
|
9
|
-
# Username mapping:
|
|
10
|
-
# @wesgarland -> Wes Garland <wes@distributive.network>
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
[]
|
|
1
|
+
# @file CODEOWNERS - machine-generated via dcp-docs-wes
|
|
2
|
+
# @author wes@people
|
|
14
3
|
|
|
15
4
|
[Brandon]
|
|
5
|
+
/publish-docs.sh @BChristieDistributive
|
|
6
|
+
/catalog-info.yaml @BChristieDistributive
|
|
16
7
|
|
|
17
8
|
[Eddie]
|
|
18
9
|
/package-lock.json @eroosenmaallen
|
|
@@ -21,36 +12,6 @@
|
|
|
21
12
|
/dist/dcp-client-bundle.js @eroosenmaallen @wesgarland
|
|
22
13
|
|
|
23
14
|
[Wes]
|
|
24
|
-
/ns-map.js @wesgarland
|
|
25
|
-
/lib/ @wesgarland
|
|
26
|
-
/README.md @wesgarland
|
|
27
|
-
/.npmrc @wesgarland
|
|
28
|
-
/dist/ @wesgarland
|
|
29
|
-
/favicon.ico @wesgarland
|
|
30
|
-
/etc/ @wesgarland
|
|
31
|
-
/test-pseudo-root/ @wesgarland
|
|
32
|
-
/windows-registry.js @wesgarland
|
|
33
|
-
/dcp-client.css @wesgarland
|
|
34
|
-
/assets/ @wesgarland
|
|
35
|
-
/build/ @wesgarland
|
|
36
|
-
/tests/ @wesgarland
|
|
37
|
-
/.npmignore @wesgarland
|
|
38
|
-
/bin/ @wesgarland
|
|
39
|
-
/.eslintrc.json @wesgarland
|
|
40
|
-
/index.js @wesgarland
|
|
41
|
-
/libexec/ @wesgarland
|
|
42
|
-
/.tidelift @wesgarland
|
|
43
|
-
/LICENSE.md @wesgarland
|
|
44
|
-
/docs/ @wesgarland
|
|
45
|
-
/cors-proxy.html @wesgarland
|
|
46
|
-
/.gitlab-ci.yml @wesgarland
|
|
47
|
-
/.gitignore @wesgarland
|
|
48
|
-
/init-common.js @wesgarland
|
|
49
|
-
/dcp-client.js @wesgarland
|
|
50
|
-
/cjs2-shim.js @wesgarland
|
|
51
|
-
/examples/ @wesgarland
|
|
52
|
-
/templates/ @wesgarland
|
|
53
|
-
/generated/ @wesgarland
|
|
54
15
|
/templates/ @wesgarland
|
|
55
16
|
/assets/ @wesgarland
|
|
56
17
|
/test-pseudo-root/ @wesgarland
|
|
@@ -61,8 +22,33 @@
|
|
|
61
22
|
/docs/ @wesgarland
|
|
62
23
|
/lib/ @wesgarland
|
|
63
24
|
/libexec/ @wesgarland
|
|
64
|
-
/dist/ @wesgarland
|
|
25
|
+
/dist/dcp-client-bundle.js.map @wesgarland
|
|
26
|
+
/dist/dcp-modal-style.css @wesgarland
|
|
65
27
|
/bin/ @wesgarland
|
|
28
|
+
/.eslintrc.json @wesgarland
|
|
29
|
+
/.git @wesgarland
|
|
30
|
+
/.gitignore @wesgarland
|
|
31
|
+
/.gitlab-ci.yml @wesgarland
|
|
32
|
+
/.npmignore @wesgarland
|
|
33
|
+
/.npmrc @wesgarland
|
|
34
|
+
/.tidelift @wesgarland
|
|
35
|
+
/LICENSE.md @wesgarland
|
|
36
|
+
/README.md @wesgarland
|
|
37
|
+
/cjs2-shim.js @wesgarland
|
|
38
|
+
/cors-proxy.html @wesgarland
|
|
39
|
+
/dcp-client.css @wesgarland
|
|
40
|
+
/dcp-client.js @wesgarland
|
|
41
|
+
/dcp-support.py @wesgarland
|
|
42
|
+
/favicon.ico @wesgarland
|
|
43
|
+
/fs-basic.py @wesgarland
|
|
44
|
+
/generated/ @wesgarland
|
|
45
|
+
/index.js @wesgarland
|
|
46
|
+
/index.py @wesgarland
|
|
47
|
+
/init-common.js @wesgarland
|
|
48
|
+
/licenses/ @wesgarland
|
|
49
|
+
/ns-map.js @wesgarland
|
|
50
|
+
/test-helpers/ @wesgarland
|
|
51
|
+
/windows-registry.js @wesgarland
|
|
66
52
|
|
|
67
53
|
[Wes or Eddie]
|
|
68
54
|
/package.json @wesgarland @eroosenmaallen
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
#! /usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* @file simple-job-remote-data-pattern.js
|
|
4
|
+
*
|
|
5
|
+
* Simple NodeJS application showing how to use RemoteDataPattern class that works for slices which their uri's has similar patterns
|
|
6
|
+
* For more information please refer to:
|
|
7
|
+
* https://gitlab.com/Distributed-Compute-Protocol/dcp-docs-wes/-/blob/wip/scheduler/remote-storage.md#data-movement
|
|
8
|
+
*
|
|
9
|
+
* *********************************** NOTE 1 ***********************************
|
|
10
|
+
* Your keystore should be placed in your home directory in .dcp/default.keystore.
|
|
11
|
+
* When using the dcp-client API in NodeJS, this keystore will be used for communicating over DCP.
|
|
12
|
+
*
|
|
13
|
+
* *********************************** NOTE 2 ***********************************
|
|
14
|
+
* Executing Job with DCP Worker
|
|
15
|
+
*
|
|
16
|
+
* Run the following commands in your terminal:
|
|
17
|
+
* ```
|
|
18
|
+
* npm add --global dcp-worker
|
|
19
|
+
* dcp-worker --allowedOrigins http://localhost:<port number>
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @author Nazila Akhavan <nazila@distributive.network>
|
|
23
|
+
* @author Kevin Yu <kevin@distributive.network>
|
|
24
|
+
* @date Sep. 2021, June 2024
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
'use strict';
|
|
28
|
+
|
|
29
|
+
const http = require('http');
|
|
30
|
+
const port = 1234;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Setup server to serve JSON serialized input data at api-endpoint
|
|
34
|
+
*
|
|
35
|
+
* @returns {void}
|
|
36
|
+
*/
|
|
37
|
+
function startBackendServer()
|
|
38
|
+
{
|
|
39
|
+
const server = http.createServer((req, res) => {
|
|
40
|
+
// Set appropriate headers so workers on web can fetch data
|
|
41
|
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
42
|
+
|
|
43
|
+
if (req.url === '/slice-1.json')
|
|
44
|
+
{
|
|
45
|
+
res.setHeader('Content-Type', 'application/json');
|
|
46
|
+
const data = { value: 'foo' };
|
|
47
|
+
res.end(JSON.stringify(data));
|
|
48
|
+
}
|
|
49
|
+
else if (req.url === '/slice-2.json')
|
|
50
|
+
{
|
|
51
|
+
res.setHeader('Content-Type', 'application/json');
|
|
52
|
+
const data = { value: 'bar' };
|
|
53
|
+
res.end(JSON.stringify(data));
|
|
54
|
+
}
|
|
55
|
+
else
|
|
56
|
+
{
|
|
57
|
+
res.statusCode = 404;
|
|
58
|
+
res.end('Not Found');
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
server.listen(port, () => {
|
|
63
|
+
console.log(`Remote data available at http://localhost:${port}/`);
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Setup event listeners for jobs
|
|
69
|
+
*
|
|
70
|
+
* @param {object} job - the job handle object
|
|
71
|
+
* @returns {void}
|
|
72
|
+
*/
|
|
73
|
+
function addJobEventListeners(job)
|
|
74
|
+
{
|
|
75
|
+
// Log the job's assigned id.
|
|
76
|
+
job.on('accepted', ({ id }) => console.log(`Job accepted with id ${id}`));
|
|
77
|
+
|
|
78
|
+
// Log returned slice results
|
|
79
|
+
job.on('result', (result) => console.log('Received result:', result));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Main function to deploy a job with remote work function
|
|
84
|
+
*
|
|
85
|
+
* @returns {void}
|
|
86
|
+
*/
|
|
87
|
+
async function main()
|
|
88
|
+
{
|
|
89
|
+
const compute = require('dcp/compute');
|
|
90
|
+
|
|
91
|
+
// Start up server to host slice data
|
|
92
|
+
startBackendServer();
|
|
93
|
+
|
|
94
|
+
// Prevents repeating URI’s that have the similar pattern and allow passing the pattern and the number of slices.
|
|
95
|
+
// https://docs.dcp.dev/advanced/data-uri.html
|
|
96
|
+
const { RemoteDataPattern } = require('dcp/compute');
|
|
97
|
+
|
|
98
|
+
// URL where the work function is located
|
|
99
|
+
const remoteData = new RemoteDataPattern(`http://localhost:${port}/slice-{slice}.json`, 2);
|
|
100
|
+
|
|
101
|
+
// Creates a Job for the distributed computer.
|
|
102
|
+
// https://docs.dcp.dev/specs/compute-api.html#compute-for
|
|
103
|
+
const job = compute.for(
|
|
104
|
+
remoteData,
|
|
105
|
+
(datum) => {
|
|
106
|
+
// If a progress event is not emitted within 30 seconds,
|
|
107
|
+
// the scheduler will throw an ENOPROGRESS error.
|
|
108
|
+
progress(1);
|
|
109
|
+
return datum.value.toUpperCase();
|
|
110
|
+
},
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
// Listen for job emitted events
|
|
114
|
+
addJobEventListeners(job);
|
|
115
|
+
|
|
116
|
+
// Deploy job
|
|
117
|
+
const results = await job.exec(compute.marketValue);
|
|
118
|
+
console.log('Job completed, here are the results: ', Array.from(results));
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
require('../../..').init().then(main).catch(console.error).finally(process.exit);
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
#! /usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* @file simple-job-remote-function.js
|
|
4
|
+
*
|
|
5
|
+
* Simple NodeJS application showing how to implement a simple remote work-function.
|
|
6
|
+
*
|
|
7
|
+
* *********************************** NOTE 1 ***********************************
|
|
8
|
+
* Your keystore should be placed in your home directory in .dcp/default.keystore.
|
|
9
|
+
* When using the dcp-client API in NodeJS, this keystore will be used for communicating over DCP.
|
|
10
|
+
*
|
|
11
|
+
* *********************************** NOTE 2 ***********************************
|
|
12
|
+
* Executing Job with DCP Worker
|
|
13
|
+
*
|
|
14
|
+
* Run the following commands in your terminal:
|
|
15
|
+
* ```
|
|
16
|
+
* npm add --global dcp-worker
|
|
17
|
+
* dcp-worker --allowedOrigins http://localhost:<port number>
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @author Kevin Yu <kevin@distributive.network>
|
|
21
|
+
* @date June 2024
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
'use strict';
|
|
25
|
+
|
|
26
|
+
const http = require('http');
|
|
27
|
+
const port = 1234;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Setup server to serve a work function at api-endpoint
|
|
31
|
+
*
|
|
32
|
+
* @returns {void}
|
|
33
|
+
*/
|
|
34
|
+
function startBackendServer()
|
|
35
|
+
{
|
|
36
|
+
const server = http.createServer((req, res) => {
|
|
37
|
+
// Set appropriate headers to allow cross-origin requests
|
|
38
|
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
39
|
+
|
|
40
|
+
if (req.url === '/')
|
|
41
|
+
{
|
|
42
|
+
// Define work function as string
|
|
43
|
+
const workerFunction = `
|
|
44
|
+
(datum) => {
|
|
45
|
+
// If a progress event is not emitted within 30 seconds,
|
|
46
|
+
// the scheduler will throw an ENOPROGRESS error.
|
|
47
|
+
progress(1);
|
|
48
|
+
return datum * 2;
|
|
49
|
+
}`;
|
|
50
|
+
|
|
51
|
+
res.setHeader('Content-Type', 'application/javascript');
|
|
52
|
+
res.end(workerFunction);
|
|
53
|
+
}
|
|
54
|
+
else
|
|
55
|
+
{
|
|
56
|
+
res.statusCode = 404;
|
|
57
|
+
res.end('Not Found');
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
server.listen(port, () => {
|
|
62
|
+
console.log(`Work Function available at http://localhost:${port}/`);
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Setup event listeners for jobs
|
|
68
|
+
*
|
|
69
|
+
* @param {object} job - the job handle object
|
|
70
|
+
* @returns {void}
|
|
71
|
+
*/
|
|
72
|
+
function addJobEventListeners(job)
|
|
73
|
+
{
|
|
74
|
+
// Log the job's assigned id.
|
|
75
|
+
job.on('accepted', ({ id }) => console.log(`Job accepted with id ${id}`));
|
|
76
|
+
|
|
77
|
+
// Log returned slice results
|
|
78
|
+
job.on('result', (result) => console.log('Received result:', result));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Main function to deploy a job with remote work function
|
|
83
|
+
*
|
|
84
|
+
* @returns {void}
|
|
85
|
+
*/
|
|
86
|
+
async function main()
|
|
87
|
+
{
|
|
88
|
+
const compute = require('dcp/compute');
|
|
89
|
+
|
|
90
|
+
// Start up server to host work function
|
|
91
|
+
startBackendServer();
|
|
92
|
+
|
|
93
|
+
// URL where the work function is located
|
|
94
|
+
const remoteWorkFunction = new URL(`http://localhost:${port}/`);
|
|
95
|
+
|
|
96
|
+
// Creates a Job for the distributed computer.
|
|
97
|
+
// https://docs.dcp.dev/specs/compute-api.html#compute-for
|
|
98
|
+
const job = compute.for(
|
|
99
|
+
[1, 2, 3, 4],
|
|
100
|
+
remoteWorkFunction,
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
// Listen for job emitted events
|
|
104
|
+
addJobEventListeners(job);
|
|
105
|
+
|
|
106
|
+
// Deploy job
|
|
107
|
+
const results = await job.exec(compute.marketRate);
|
|
108
|
+
console.log('Job completed, here are the results: ', Array.from(results));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
require('../../..').init().then(main);
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
#! /usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* @file simple-job-remote-input.js
|
|
4
|
+
*
|
|
5
|
+
* Sample NodeJS application showing how to deploy a simple DCP job with remote input data
|
|
6
|
+
* that is serialized with either json or kvin (https://github.com/wesgarland/kvin).
|
|
7
|
+
*
|
|
8
|
+
* *********************************** NOTE 1 ***********************************
|
|
9
|
+
* Your keystore should be placed in your home directory in .dcp/default.keystore.
|
|
10
|
+
* When using the dcp-client API in NodeJS, this keystore will be used for communicating over DCP.
|
|
11
|
+
*
|
|
12
|
+
* *********************************** NOTE 2 ***********************************
|
|
13
|
+
* Executing Job with DCP Worker
|
|
14
|
+
*
|
|
15
|
+
* Run the following commands in your terminal:
|
|
16
|
+
* ```
|
|
17
|
+
* npm add --global dcp-worker
|
|
18
|
+
* dcp-worker --allowedOrigins http://localhost:<port number>
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @author Nazila Akhavan <nazila@distributive.network>
|
|
22
|
+
* @author Kevin Yu <kevin@distributive.network>
|
|
23
|
+
* @date June 2024
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
'use strict';
|
|
27
|
+
|
|
28
|
+
// Serialization library for JavaScript types for transmission over a network
|
|
29
|
+
const kvin = require('kvin');
|
|
30
|
+
const http = require('http');
|
|
31
|
+
const portA = 1234;
|
|
32
|
+
const portB = 2345;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Setup server to serve JSON serialized input data at api-endpoint
|
|
36
|
+
*
|
|
37
|
+
* @returns {void}
|
|
38
|
+
*/
|
|
39
|
+
function startBackendServerA()
|
|
40
|
+
{
|
|
41
|
+
const serverA = http.createServer((req, res) => {
|
|
42
|
+
// Set appropriate headers so workers on web can fetch data
|
|
43
|
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
44
|
+
res.setHeader('Content-Type', 'application/json');
|
|
45
|
+
|
|
46
|
+
const data = { value: 'foo' };
|
|
47
|
+
res.end(JSON.stringify(data));
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
serverA.listen(portA, () => {
|
|
51
|
+
console.log(`Remote data available at http://localhost:${portA}/`);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Setup server to serve KVIN serialized input data at api-endpoint
|
|
57
|
+
*
|
|
58
|
+
* @returns {void}
|
|
59
|
+
*/
|
|
60
|
+
function startBackendServerB()
|
|
61
|
+
{
|
|
62
|
+
const serverB = http.createServer((req, res) => {
|
|
63
|
+
// Set appropriate headers so workers on web can fetch data
|
|
64
|
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
65
|
+
res.setHeader('Content-Type', 'application/x-kvin');
|
|
66
|
+
|
|
67
|
+
const data = { value: 'bar' };
|
|
68
|
+
res.end(kvin.serialize(data));
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
serverB.listen(portB, () => {
|
|
72
|
+
console.log(`Remote data available at http://localhost:${portB}/`);
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Setup event listeners for jobs
|
|
78
|
+
*
|
|
79
|
+
* @param {object} job - the job handle object
|
|
80
|
+
* @returns {void}
|
|
81
|
+
*/
|
|
82
|
+
function addJobEventListeners(job)
|
|
83
|
+
{
|
|
84
|
+
// Log the job's assigned id.
|
|
85
|
+
job.on('accepted', ({ id }) => console.log(`Job accepted with id ${id}`));
|
|
86
|
+
|
|
87
|
+
// Log returned slice results
|
|
88
|
+
job.on('result', (result) => console.log('Received result:', result));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Main function to deploy a job with remote work function
|
|
93
|
+
*
|
|
94
|
+
* @returns {void}
|
|
95
|
+
*/
|
|
96
|
+
async function main()
|
|
97
|
+
{
|
|
98
|
+
const compute = require('dcp/compute');
|
|
99
|
+
|
|
100
|
+
// Start up server to host slice data
|
|
101
|
+
startBackendServerA();
|
|
102
|
+
startBackendServerB();
|
|
103
|
+
|
|
104
|
+
// Fetched remote input data
|
|
105
|
+
const remoteData = [new URL(`http://localhost:${portA}/`), new URL(`http://localhost:${portB}/`)];
|
|
106
|
+
|
|
107
|
+
// Creates a Job for the distributed computer.
|
|
108
|
+
// https://docs.dcp.dev/specs/compute-api.html#compute-for
|
|
109
|
+
const job = compute.for(
|
|
110
|
+
remoteData,
|
|
111
|
+
(datum) => {
|
|
112
|
+
// If a progress event is not emitted within 30 seconds,
|
|
113
|
+
// the scheduler will throw an ENOPROGRESS error.
|
|
114
|
+
progress(1);
|
|
115
|
+
return datum.value.toUpperCase();
|
|
116
|
+
},
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
// Listen for job emitted events
|
|
120
|
+
addJobEventListeners(job);
|
|
121
|
+
|
|
122
|
+
// Deploy job
|
|
123
|
+
const results = await job.exec(compute.marketValue);
|
|
124
|
+
console.log('Job completed, here are the results: ', Array.from(results));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
require('../../..').init().then(main);
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
#! /usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* @file simple-job-es6.mjs
|
|
4
|
+
*
|
|
5
|
+
* Sample NodeJS application showing how to deploy a simple DCP job using ES6 modules.
|
|
6
|
+
*
|
|
7
|
+
* *********************************** NOTE ***********************************
|
|
8
|
+
* Your keystore should be placed in your home directory in .dcp/default.keystore.
|
|
9
|
+
* When using the dcp-client API in NodeJS, this keystore will be used for communicating over DCP.
|
|
10
|
+
*
|
|
11
|
+
* @author Kevin Yu <kevin@distributive.network>
|
|
12
|
+
* @date June 2024
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { init } from '../../index.js';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Setup event listeners for jobs
|
|
19
|
+
*
|
|
20
|
+
* @param {object} job - the job handle object
|
|
21
|
+
* @returns {void}
|
|
22
|
+
*/
|
|
23
|
+
function addJobEventListeners(job)
|
|
24
|
+
{
|
|
25
|
+
// Log the job's assigned id.
|
|
26
|
+
job.on('accepted', ({ id }) => console.log(`Job accepted with id ${id}`));
|
|
27
|
+
|
|
28
|
+
// Log returned slice results
|
|
29
|
+
job.on('result', (result) => console.log('Received result:', result));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Main function to deploy a job
|
|
34
|
+
*
|
|
35
|
+
* @returns {void}
|
|
36
|
+
*/
|
|
37
|
+
async function main()
|
|
38
|
+
{
|
|
39
|
+
const { compute } = await init();
|
|
40
|
+
|
|
41
|
+
// Creates a Job for the distributed computer.
|
|
42
|
+
// https://docs.dcp.dev/specs/compute-api.html#compute-for
|
|
43
|
+
const job = compute.for(
|
|
44
|
+
[1, 2, 3, 4],
|
|
45
|
+
(datum) => {
|
|
46
|
+
// If a progress event is not emitted within 30 seconds,
|
|
47
|
+
// the scheduler will throw an ENOPROGRESS error.
|
|
48
|
+
progress(1);
|
|
49
|
+
return datum * 2;
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// Listen for job emitted events
|
|
53
|
+
addJobEventListeners(job);
|
|
54
|
+
|
|
55
|
+
// Deploys the job
|
|
56
|
+
const results = await job.exec();
|
|
57
|
+
console.log(results);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
await main();
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
#! /usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* @file simple-job.js
|
|
4
|
+
*
|
|
5
|
+
* Sample NodeJS application showing how to deploy a simple DCP job.
|
|
6
|
+
*
|
|
7
|
+
* *********************************** NOTE ***********************************
|
|
8
|
+
* Your keystore should be placed in your home directory in .dcp/default.keystore.
|
|
9
|
+
* When using the dcp-client API in NodeJS, this keystore will be used for communicating over DCP.
|
|
10
|
+
*
|
|
11
|
+
* @author Wes Garland <wes@distributive.network>
|
|
12
|
+
* @author Kevin Yu <kevin@distributive.network>
|
|
13
|
+
* @date Aug 2019, April 2020, June 2024
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
'use strict';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Setup event listeners for jobs
|
|
20
|
+
*
|
|
21
|
+
* @param {object} job - the job handle object
|
|
22
|
+
* @returns {void}
|
|
23
|
+
*/
|
|
24
|
+
function addJobEventListeners(job)
|
|
25
|
+
{
|
|
26
|
+
// Log the job's assigned id.
|
|
27
|
+
job.on('accepted', ({ id }) => console.log(`Job accepted with id ${id}`));
|
|
28
|
+
|
|
29
|
+
// Log returned slice results
|
|
30
|
+
job.on('result', (result) => console.log('Received result:', result));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Main function to deploy a job
|
|
35
|
+
*
|
|
36
|
+
* @returns {void}
|
|
37
|
+
*/
|
|
38
|
+
async function main()
|
|
39
|
+
{
|
|
40
|
+
const compute = require('dcp/compute');
|
|
41
|
+
|
|
42
|
+
// Creates a Job for the distributed computer.
|
|
43
|
+
// https://docs.dcp.dev/specs/compute-api.html#compute-for
|
|
44
|
+
const job = compute.for(
|
|
45
|
+
[1,2,3,4],
|
|
46
|
+
(datum) => {
|
|
47
|
+
// If a progress event is not emitted within 30 seconds,
|
|
48
|
+
// the scheduler will throw an ENOPROGRESS error.
|
|
49
|
+
progress(1);
|
|
50
|
+
return datum * 2;
|
|
51
|
+
},
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
// Listen for job emitted events
|
|
55
|
+
addJobEventListeners(job);
|
|
56
|
+
|
|
57
|
+
// Deploys the job
|
|
58
|
+
const results = await job.exec(compute.marketRate);
|
|
59
|
+
console.log('Job completed, here are the results: ', Array.from(results));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
require('../../').init().then(main);
|