dcp-worker 3.2.30-7 → 3.2.30-9

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.
@@ -1,187 +0,0 @@
1
- /**
2
- * @file default-event.js
3
- * Default worker/sandbox events, providing default logging behaviours (event handlers)
4
- * for the dcp-worker.
5
- *
6
- * - All event handlers use the *current* global console object to emit messages;
7
- * enhanced logging subsystems should intercept this object to achieve their desired
8
- * behaviours.
9
- *
10
- * - All event handlers invoke functions which are properties of the eventHandlers return
11
- * value from the hook function. This means that alternate user interfaces can either
12
- * hook or intercept the properties of that object to modify the event handlers'
13
- * behaviour without actually removing/replacing the event handler on the instance of
14
- * Worker.
15
- *
16
- * NOTE: This is just a convenience module. There is no requirement to use this module to
17
- * hook worker events, this module mainly exists to make it easy for the
18
- * dashboard-tui to replace event handlers with better ones, but it also makes it
19
- * easier to sandbox events since we only need to register one event handler here
20
- * to handle every sandbox.
21
- *
22
- * @author Ryan Rossiter, ryan@kingsds.network
23
- * @date April 2020
24
- * @author Wes Garland, wes@distributive.network
25
- * @date June 2023
26
- */
27
- 'use strict';
28
-
29
- const sandboxEventHandlers = {};
30
- const workerEventHandlers = {};
31
-
32
- /**
33
- * Sandbox 1: Slice Started: slice 1, 0x5b5214D48F0428669c4E: Simple Job
34
- * Sandbox 1: Slice Completed: slice 1, 0x5b5214D48F0428669c4E: Simple Job: dt 114ms
35
- */
36
-
37
- /**
38
- * @param worker The instance of Worker to hook
39
- * @param options cliArgs from worker
40
- */
41
- exports.hook = function hookWorkerEvents$$hook(worker, options)
42
- {
43
- const sliceMap = {}; // jobAddress --> ( sliceNumber, t0 )
44
- const truncationLength = 22; // Extra 2 for '0x'
45
-
46
- delete exports.hook;
47
-
48
- function makeSliceId (sandbox, sliceNumber)
49
- {
50
- if (!sandbox.jobAddress)
51
- return '<no job>';
52
-
53
- const address = sandbox.jobAddress ? sandbox.jobAddress.slice(0, truncationLength) : 'null';
54
- const baseInfo = sandbox.public?.name ? `${address}: ${sandbox.public.name}` : `${address}:`;
55
-
56
- if (!sliceNumber)
57
- sliceNumber = sandbox.slice ? sandbox.slice.sliceNumber : 0;
58
- return sliceNumber ? `slice ${sliceNumber}, ${baseInfo}` : baseInfo;
59
- }
60
-
61
- sandboxEventHandlers.ready = function sandboxReadyHandler(sandbox, sandboxData, ev) {
62
- console.log(` . Sandbox ${sandboxData.shortId}: Initialized`);
63
- };
64
-
65
- sandboxEventHandlers.terminated = function sandboxTerminatedHandler(sandbox, sandboxData, ev) {
66
- const sliceInfo = sliceMap[sandbox.id];
67
- console.log(` * Sandbox ${sandboxData.shortId}: Terminated: ${makeSliceId(sandbox, sliceInfo?.slice)}`);
68
- delete sliceMap[sandbox.id];
69
- };
70
-
71
- sandboxEventHandlers.start = function sliceStartHandler(sandbox, sandboxData, ev) {
72
- const sliceNumber = sandbox.slice ? sandbox.slice.sliceNumber : 0;
73
- sliceMap[sandbox.id] = { slice: sliceNumber, t0: Date.now() };
74
- console.log(` . Sandbox ${sandboxData.shortId}: Slice Started: ${makeSliceId(sandbox)}`);
75
- };
76
-
77
- sandboxEventHandlers.sliceProgress = function sliceProgressHandler(sandbox, sandbodData, ev) {
78
- // NOP
79
- };
80
-
81
- sandboxEventHandlers.sliceFinish = function sliceFinishHandler(sandbox, sandboxData, ev) {
82
- const sliceInfo = sliceMap[sandbox.id];
83
- if (sliceInfo)
84
- console.log(` * Sandbox ${sandboxData.shortId}: Slice Completed: ${makeSliceId(sandbox, sliceInfo.slice)}: dt ${Date.now() - sliceInfo.t0}ms`);
85
- else
86
- console.log(` * Sandbox ${sandboxData.shortId}: Slice Completed: ${makeSliceId(sandbox)}`);
87
- };
88
-
89
- workerEventHandlers.payment = function paymentHandler(ev) {
90
- const payment = parseFloat(ev.payment);
91
-
92
- if (isNaN(payment))
93
- console.error(' ! Failed to parse payment:', payment);
94
- else
95
- console.log(` . Payment: ${payment.toFixed(3)} ⊇`);
96
- };
97
-
98
- workerEventHandlers.fetchStart = function fetchStartHandler() {
99
- options.verbose && console.log(' * Fetching slices...');
100
- };
101
-
102
- workerEventHandlers.fetch = function fetchHandler(ev) {
103
- var slicesFetched;
104
-
105
- if (ev instanceof Error)
106
- {
107
- console.error(' ! Failed to fetch slices:', ev);
108
- return;
109
- }
110
-
111
- if (typeof ev === 'number' || typeof ev === 'string') /* <= June 2023 Worker events: remove ~ Sep 2023 /wg */
112
- slicesFetched = ev;
113
- else
114
- {
115
- const task = ev;
116
- slicesFetched = task.slices.length;
117
- }
118
-
119
- options.verbose && console.log(' . Fetched', slicesFetched, 'slices');
120
- };
121
-
122
- workerEventHandlers.fetchError = function fetchErrorHandler(ev) {
123
- console.log(' ! Failed to fetch slices:', ev);
124
- };
125
-
126
- workerEventHandlers.submitStart = function submitStartHandler() {
127
- options.verbose >= 2 && console.log(' * Submitting results...');
128
- };
129
-
130
- workerEventHandlers.submit = function submitHandler() {
131
- options.verbose >= 2 && console.log(' . Submitted');
132
- };
133
-
134
- workerEventHandlers.submitError = function submitErrorHandler(ev) {
135
- console.log(' ! Failed to submit results:', ev);
136
- };
137
-
138
- /* Register the appropriate event handlers on the worker and on each sandbox. The handlers are
139
- * registered in such a way that mutating the exports object to supply different handlers after
140
- * registration will work.
141
- *
142
- * The handlers registered on each sandbox receive two extra arguments before the usual event
143
- * arguments; these are the sandbox handle emitted by the Worker<sandbox> event and an object
144
- * called `sandboxData` which is just arbitrary storage for the eventHandlers' use, eg for memos.
145
- */
146
- for (let eventName in workerEventHandlers)
147
- worker.addEventListener(eventName, workerEventHandlers[eventName]);
148
-
149
- worker.on('sandbox', function newSandboxHandler(sandbox) {
150
- const sandboxData = {
151
- shortId: sandbox.id.toString(10).padStart(3)
152
- };
153
- for (let eventName in sandboxEventHandlers)
154
- sandbox.addEventListener(eventName, (...args) => sandboxEventHandlers[eventName](sandbox, sandboxData, ...args));
155
- });
156
-
157
- exports.sandboxEventHandlers = sandboxEventHandlers;
158
- exports. workerEventHandlers = workerEventHandlers;
159
- };
160
-
161
- /**
162
- * Function to replace a worker event handler.
163
- *
164
- * @param {string} eventName name of the event to replace
165
- * @param {function} eventHandler new event handler
166
- */
167
- exports.replaceWorkerEvent = function hookWorkerEvents$$replace(eventName, eventHandler)
168
- {
169
- if (!workerEventHandlers.hasOwnProperty(eventName))
170
- throw new Error('unknown worker event: ' + eventName + `(${Object.keys(workerEventHandlers).join(', ')})`);
171
-
172
- workerEventHandlers[eventName] = eventHandler;
173
- }
174
-
175
- /**
176
- * Function to replace a sandbox event handler.
177
- *
178
- * @param {string} eventName name of the event to replace
179
- * @param {function} eventHandler new event handler
180
- */
181
- exports.replaceSandboxEvent = function hookSandboxEvents$$replace(eventName, eventHandler)
182
- {
183
- if (!sandboxEventHandlers.hasOwnProperty(eventName))
184
- throw new Error('unknown sandbox event: ' + eventName + `(${Object.keys(sandboxEventHandlers).join(', ')})`);
185
-
186
- sandboxEventHandlers[eventName] = eventHandler;
187
- }