0.0.102 • Published 5 years ago

window-worker v0.0.102

Weekly downloads
4
License
BSD-3-Clause
Repository
github
Last release
5 years ago

tiny-worker

Tiny WebWorker for Server

require() is available for flexible inline Worker scripts. Optional parameters args Array & options Object; see child_process.fork() documentation.

build status

Example

Creating a Worker from a file

The worker script:

onmessage = function (ev) {
	postMessage(ev.data);
};

The core script:

var Worker = require("tiny-worker");
var worker = new Worker("repeat.js");

worker.onmessage = function (ev) {
	console.log(ev.data);
	worker.terminate();
};

worker.postMessage("Hello World!");

Creating a Worker from a Function

var Worker = require("tiny-worker");
var worker = new Worker(function () {
	self.onmessage = function (ev) {
		postMessage(ev.data);
	};
});

worker.onmessage = function (ev) {
	console.log(ev.data);
	worker.terminate();
};

worker.postMessage("Hello World!");

Debugging

To be able to debug a child process, it must have a differnt debug port than the parent. Tiny worker does this by adding a random port within a range to the parents debug port. The default Range is [1, 300], it can be changed with the setRange(min, max) method. To disable any automatic port redirection set options.noDebugRedirection = true.

automatic redirection

//parent is started with '--debug=1234'
var Worker = require("tiny-worker");
Worker.setRange(2, 20);

var worker = new Worker(function () {
	postMessage(process.debugPort); 
});

worker.onmessage = function (ev) {
	console.log(ev.data); //prints any number between 1236 and 1254
	worker.terminate();
}

manual redirection

//parent is started with '--debug=1234'
var Worker = require("tiny-worker");

var worker = new Worker(function () {
	postMessage(process.debugPort); 
}, [], {noDebugRedirection: true, execArgv: ["--debug=1235"]});

worker.onmessage = function (ev) {
	console.log(ev.data); //prints 1235
	worker.terminate();
}

Properties

onmessage

Message handler, accepts an Event

onerror

Error handler, accepts an Event

API

addEventListener(event, fn)

Adds an event listener

postMessage()

Broadcasts a message to the Worker

terminate()

Terminates the Worker

static setRange(min, max)

Sets range for debug ports, only affects current process. Returns true if successful.

FAQ

  1. I have an orphaned child process that lives on past the parent process' lifespan
  • Most likely a SIGTERM or SIGINT is not reaching the child process
  1. How do I insure all process are terminated?
  • In your core script register a listener for SIGTERM or SIGINT via process.on() which terminates (all) worker process(es) and then gracefully shutdowns via process.exit(0);
  1. Why SIGTERM or SIGINT?
  • Unix/BSD will work with SIGTERM, but if you also need to support Windows use SIGINT

License

Copyright (c) 2017 Jason Mulligan Licensed under the BSD-3 license

0.0.102

5 years ago

0.0.101

5 years ago

0.0.100

5 years ago

0.0.99

5 years ago

0.0.98

6 years ago

0.0.97

6 years ago

0.0.96

6 years ago

0.0.95

6 years ago

0.0.94

6 years ago

0.0.93

6 years ago

0.0.92

6 years ago

0.0.91

6 years ago

0.0.90

6 years ago

0.0.89

6 years ago

0.0.88

6 years ago

0.0.87

6 years ago

0.0.86

6 years ago

0.0.85

6 years ago

0.0.84

6 years ago

0.0.83

6 years ago

0.0.82

6 years ago

0.0.81

6 years ago

0.0.80

6 years ago

0.0.79

6 years ago

0.0.78

6 years ago

0.0.77

6 years ago

0.0.76

6 years ago

0.0.75

6 years ago

0.0.74

6 years ago

0.0.73

6 years ago

0.0.72

6 years ago

0.0.71

6 years ago

0.0.70

6 years ago

0.0.69

6 years ago

0.0.68

6 years ago

0.0.67

6 years ago

0.0.66

6 years ago

0.0.65

6 years ago

0.0.64

6 years ago

0.0.63

6 years ago

0.0.62

6 years ago

0.0.61

6 years ago

0.0.60

6 years ago

0.0.59

6 years ago

0.0.58

6 years ago

0.0.57

6 years ago

0.0.56

6 years ago

0.0.55

6 years ago

0.0.54

6 years ago

0.0.53

6 years ago

0.0.52

6 years ago

0.0.51

6 years ago

0.0.50

6 years ago

0.0.49

6 years ago

0.0.48

6 years ago

0.0.47

6 years ago

0.0.46

6 years ago

0.0.45

6 years ago

0.0.44

6 years ago

0.0.43

6 years ago

0.0.42

6 years ago

0.0.41

6 years ago

0.0.40

6 years ago

0.0.39

6 years ago

0.0.38

6 years ago

0.0.37

6 years ago

0.0.36

6 years ago

0.0.35

6 years ago

0.0.34

6 years ago

0.0.33

6 years ago

0.0.32

6 years ago

0.0.31

6 years ago

0.0.30

6 years ago

0.0.29

6 years ago

0.0.28

6 years ago

0.0.27

6 years ago

0.0.26

6 years ago

0.0.25

6 years ago

0.0.24

6 years ago

0.0.23

6 years ago

0.0.22

6 years ago

0.0.21

6 years ago

0.0.20

6 years ago

0.0.19

6 years ago

0.0.18

6 years ago

0.0.17

6 years ago

0.0.16

6 years ago

0.0.15

6 years ago

0.0.14

6 years ago

0.0.13

6 years ago

0.0.12

6 years ago

0.0.11

6 years ago

0.0.10

6 years ago

0.0.9

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago