All Projects → avoidwork → Tiny Worker

avoidwork / Tiny Worker

Licence: bsd-3-clause
Tiny WebWorker for the Server

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Tiny Worker

Ngx Papaparse
Papa Parse wrapper for Angular
Stars: ✭ 83 (-57.65%)
Mutual labels:  workers
Workq
Job server in Go
Stars: ✭ 1,546 (+688.78%)
Mutual labels:  workers
Cfworker
A collection of packages optimized for Cloudflare Workers and service workers.
Stars: ✭ 152 (-22.45%)
Mutual labels:  workers
Webworkify Webpack
launch a web worker at runtime that can require() in the browser with webpack
Stars: ✭ 105 (-46.43%)
Mutual labels:  workers
Dtcqueuebundle
Symfony2/3/4/5 Queue Bundle (for background jobs) supporting Mongo (Doctrine ODM), Mysql (and any Doctrine ORM), RabbitMQ, Beanstalkd, Redis, and ... {write your own}
Stars: ✭ 115 (-41.33%)
Mutual labels:  workers
Worker Typescript Template
ʕ •́؈•̀) TypeScript template for Cloudflare Workers
Stars: ✭ 129 (-34.18%)
Mutual labels:  workers
Qutee
PHP Background Jobs (Tasks) Manager
Stars: ✭ 63 (-67.86%)
Mutual labels:  workers
Ost
Redis based queues and workers.
Stars: ✭ 163 (-16.84%)
Mutual labels:  workers
Kafka Flow
KafkaFlow is a .NET framework to consume and produce Kafka messages with multi-threading support. It's very simple to use and very extendable. You just need to install, configure, start/stop the bus with your app and create a middleware/handler to process the messages.
Stars: ✭ 118 (-39.8%)
Mutual labels:  workers
Worker Plugin
👩‍🏭 Adds native Web Worker bundling support to Webpack.
Stars: ✭ 1,840 (+838.78%)
Mutual labels:  workers
Workers
Cloudflare Workers
Stars: ✭ 111 (-43.37%)
Mutual labels:  workers
React Native Workers
Do heavy data process outside of your UI JS thread.
Stars: ✭ 114 (-41.84%)
Mutual labels:  workers
Meteor Service Worker
An universal service worker for meteor apps
Stars: ✭ 132 (-32.65%)
Mutual labels:  workers
Create Google Shared Drive
Cloudflare Redesigned Script for creating a Shared/Team Drive
Stars: ✭ 93 (-52.55%)
Mutual labels:  workers
Php Resque
An implementation of Resque in PHP.
Stars: ✭ 157 (-19.9%)
Mutual labels:  workers
Rqueue
Rqueue aka Redis Queue [Task Queue, Message Broker] for Spring framework
Stars: ✭ 76 (-61.22%)
Mutual labels:  workers
Simpleue
PHP queue worker and consumer - Ready for AWS SQS, Redis, Beanstalkd and others.
Stars: ✭ 124 (-36.73%)
Mutual labels:  workers
Task bunny
TaskBunny is a background processing application written in Elixir and uses RabbitMQ as a messaging backend
Stars: ✭ 193 (-1.53%)
Mutual labels:  workers
Tunny
A goroutine pool for Go
Stars: ✭ 2,755 (+1305.61%)
Mutual labels:  workers
Gores
👷 Redis-backed library for creating background jobs in Go. Placing jobs in multiple queues, and process them later asynchronously.
Stars: ✭ 137 (-30.1%)
Mutual labels:  workers

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!");

Enable ES6 import/export within Worker file

The worker helper script (helper.js):

export const dataFormatter = (data) => {
	return `${data} World!`;
};

The worker script (repeat.js):

import { dataFormatter } from "./helper";

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

The core script:

var Worker = require("tiny-worker");
var worker = new Worker("repeat.js", [], {esm: true});

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

worker.postMessage("Hello");

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) 2019 Jason Mulligan Licensed under the BSD-3 license

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].