UNPKG

dcp-client

Version:

Core libraries for accessing DCP network

109 lines (93 loc) 3.65 kB
<!DOCTYPE html> <html lang="en"> <!-- -- @file simple-worker.html - Sample web page showing how to implement a trivial DCP worker. -- -- @author Wes Garland <wes@distributive.network> -- @author Bryan Hoang <bryan@distributive.network> -- @author Kevin Yu <kevin@distributive.network> -- @date Aug. 2019, Sept. 2022, July 2024 --> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <!-- Load dcp-client into `window.dcp`. --> <script src="https://scheduler.distributed.computer/dcp-client/dcp-client.js"></script> <script> 'use strict'; function addWorkerEventListeners(worker) { // The start event fires after the worker is ready to fetch tasks, but // before the first task has been fetched. worker.on('start', () => { console.log('Worker ready to fetch tasks'); }); // The fetch event is fired when the worker has fetched a task from the // task distributor. worker.on('fetch', (task) => { if (task.slices) console.log(`Worker has fetched ${Object.keys(task.slices).length} slices`); }); // The sandbox event is emitted when a new sandbox is created. The event // handler receives as its sole argument an EventEmitter which is used // to emit sandbox events worker.on('sandbox', (sandbox) => { // The progress event is fired to indicate progress throughout a job // this is triggered by the progress() call in the work function, // however, quickly-repeating calls to progress() may be composited // into a single event. sandbox.on('progress', (event) => { console.log('Sandbox progress', event); }); }); // After a result is sent to the result-submitter for consideration worker.on('payment', (event) => { console.log('Work payment', event); }); // Result event is fired immediately after the worker sends a result to // the the result data sink worker.on('result', (event) => { console.log('Worker has sent a result back to the scheduler'); }); // Worker has stopped working and no new results will be sent to the // result submitter or the result data sink and no more slices will be // returned to the scheduler worker.on('end', () => { console.log('Worker has stopped working'); }); // Log errors worker.on('error', console.error); } async function start() { const { wallet, worker: { Worker: DCPWorker }, } = window.dcp; // Bank account funds will deposit into const paymentAddress = (await wallet.get()).address; // Retrieve auth keystore object const identity = await wallet.getId(); // Instantiate DCP worker const worker = new DCPWorker( identity, { cores: { cpu: 1 }, paymentAddress, } ); // Listen for work emitted events addWorkerEventListeners(worker); // Start the worker await worker.start(); } </script> </head> <body onload="start()"> <p style="text-align: center; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 24px;"> This is a simple vanilla web DCP Worker example. <br />Look in your browser's console for output. </p> </body> </html>