SlideShare une entreprise Scribd logo
1  sur  18
Télécharger pour lire hors ligne
MinionPool

A worker pool for NodeJS
MinionPool - Overview
●
●
●
●
●
●

●
●

Easily create worker pools in seconds
Based on callbacks (hooks) into the lifecycle of the pool
Let's your do any number and type of tasks in parallel
ONLY if those tasks do not rely in the order of processing
Best results when doing idempotent operations
Actually a family of pools:
○ MinionPool
○ ArrayMinionPool
○ MysqlMinionPool
○ RabbitMQMinionPool
Can poll for tasks or accept injected tasks
Open Source
MinionPool - Proven in the field
●
●
●
●
●

Log files processing
Large mysql tables processing
ElasticSearch reindexing
S3 uploaders
Web crawlers
MinionPool - Installing
●
●
●

npm install minionpool
npm install mysql_minionpool
npm install rabbitmq_minionpool
MinionPool - As dependency
{
"dependencies": {
"minionpool": "*",
"rabbitmq_minionpool": "*",
"mysql_minionpool": "*"
}
}
MinionPool - Concepts
●
●

TaskSource: Used to produce and introduce tasks into the pool. A task
source will be initialized, used, and then shutdown
Minion: Each pool consists of N minions. Tasks will be assigned to the first
available minion. Each minion will be initialized, used, and then shutdown
when the TaskSource has reported that no more tasks are available.

MySQL

RabbitMQ

W1

Poll

Inject

Task
Source

Pool
W2
MinionPool - How to use it
var minionpoolMod = require('minionpool');
var minionPool = new minionpoolMod.MinionPool(options);
minionPool.start();
MinionPool - Options
var options = {
name: 'test',
debug: true,
concurrency: 5,
logger: console.log,
continueOnError: true,
taskSourceStart: function(callback) { … callback(err, state); }
taskSourceNext: function(state, callback) { callback(err, task); return state; },
taskSourceEnd: function(state, callback) { callback(); },
minionTaskHandler: function(task, state, callback) { callback(err, state); },
minionStart: function(callback) { callback(err, state); },
minionEnd: function(state, callback) { callback(); },
poolEnd: function() { process.exit(0); }
};
MinionPool - ArrayMinionPool
var options = {
name: 'test',
concurrency: 5,
minionTaskHandler: function(task, state, callback) {
setTimeout(function() { callback(undefined, state); }, Math.floor(Math.random() * 500));
},
poolEnd: function() {
process.exit(0);
}
};
var data = [.., .., .., .., ...];
var minionPool = new minionsMod.ArrayMinionPool(options, data);
minionPool.start();
MinionPool - MysqlMinionPool
var pool = new mysqlMinionPoolMod.MysqlMinionPool({
mysqlConfig: {
host: '127.0.0.1',
user: 'root',
password: 'pass',
database: 'db',
port: 3306
},
concurrency: 5,

// How many pages to get concurrently...

rowConcurrency: 1, // ... and how many concurrent rows processed PER query
taskSourceStart: function(callback) { callback(undefined, {page: 0, pageSize: 10}); },
minionTaskHandler: function(task, state, callback) { callback(undefined, state); }
});
pool.start();
MinionPool - MysqlMinionPool
taskSourceNext: function(state, callback) {
var query = "SELECT * FROM `db`.`table` LIMIT ?,?";
state.mysqlPool.getConnection(function(err, mysqlConnection) {
if(err) {
callback(err, undefined);
} else {
mysqlConnection.query(
query, [state.page * state.pageSize, state.pageSize], function(err, rows) {
mysqlConnection.release();
if(err) {
callback(err, undefined);
} else if(rows.length === 0) {
callback(undefined, undefined);
} else {
callback(undefined, rows);
}
}
);
}
});
state.page++;
return state;
},
MinionPool - RabbitMQMinionPool
var options = {
name: 'test',
concurrency: 5,
mqOptions: {
exchangeName: 'workers', // Will also create workers.retry
queueName: 'myWorkers',

// Will also create myWorkers.retry

routingKey: 'myWorkers', // Optional. Equals to queueName if missing
}
};
var pool = new minionsMod.RabbitMqMinionPool(options);
process.on('SIGINT', function() {
pool.end();
});
pool.start();
MinionPool - RabbitMQMinionPool
minionTaskHandler: function(msg, state, callback) {
var payload = msg.payload;
var headers = msg.headers;
var deliveryInfo = msg.deliveryInfo;
var message = msg.message;
var queue = msg.queue;
console.log('got task: %s', util.inspect(payload));
// See the node-amqp doc for more info.
message.reject(); // or message.acknowledge();
callback(undefined, state);
},
MinionPool - RabbitMQMinionPool
●
●
●
●

Dead Letter eXchanges to support retrying failed (rejected) operations
Channels in confirmation mode, so failed publishes will be notified
Messages published as persistent
Queues and exchanges marked as durable, autodelete = false
MinionPool - MultiCore
taskset: "used to set or retrieve the CPU affinity of a running process given its
PID or to launch a new COMMAND with a given CPU affinity"
●
●

http://linux.die.net/man/1/taskset
https://github.com/karelzak/util-linux

$ taskset -c N /usr/node/bin/node bin/myworker # 0 =< N < number of cores
MinionPool - MultiCore
For mysql minion pools: you can divide the total number of rows per the
number of cores available, and launch one pool per core that will process then
given range of id's.
For rabbitmq minion pools: Just spawn as many pools per core needed, they
will compete to consume tasks, rabbitmq will handle the complex part :)
MinionPool - Links

●
●
●

https://github.com/marcelog/minionpool
https://github.com/marcelog/mysql_minionpool
https://github.com/marcelog/rabbitmq_minionpool
MinionPool

Thank you, Inakos!

Contenu connexe

Tendances

Openstack taskflow 簡介
Openstack taskflow 簡介Openstack taskflow 簡介
Openstack taskflow 簡介kao kuo-tung
 
PyCon KR 2019 sprint - RustPython by example
PyCon KR 2019 sprint  - RustPython by examplePyCon KR 2019 sprint  - RustPython by example
PyCon KR 2019 sprint - RustPython by exampleYunWon Jeong
 
OTcl and C++ linkages in NS2
OTcl and C++ linkages in NS2OTcl and C++ linkages in NS2
OTcl and C++ linkages in NS2Pradeep Kumar TS
 
#Gophercon Talk by Smita Vijayakumar - Go's Context Library
#Gophercon Talk by Smita Vijayakumar - Go's Context Library#Gophercon Talk by Smita Vijayakumar - Go's Context Library
#Gophercon Talk by Smita Vijayakumar - Go's Context LibraryExotel
 
NS2: Binding C++ and OTcl variables
NS2: Binding C++ and OTcl variablesNS2: Binding C++ and OTcl variables
NS2: Binding C++ and OTcl variablesTeerawat Issariyakul
 
Go Concurrency Basics
Go Concurrency Basics Go Concurrency Basics
Go Concurrency Basics ElifTech
 
Gogo shell
Gogo shellGogo shell
Gogo shelljwausle
 
Perl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one linersPerl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one linersKirk Kimmel
 
Paris Kafka Meetup - How to develop with Kafka
Paris Kafka Meetup - How to develop with KafkaParis Kafka Meetup - How to develop with Kafka
Paris Kafka Meetup - How to develop with KafkaFlorian Hussonnois
 
Zabbix LLD from a C Module by Jan-Piet Mens
Zabbix LLD from a C Module by Jan-Piet MensZabbix LLD from a C Module by Jan-Piet Mens
Zabbix LLD from a C Module by Jan-Piet MensNETWAYS
 
Php 5.6 From the Inside Out
Php 5.6 From the Inside OutPhp 5.6 From the Inside Out
Php 5.6 From the Inside OutFerenc Kovács
 
Call stack, event loop and async programming
Call stack, event loop and async programmingCall stack, event loop and async programming
Call stack, event loop and async programmingMasters Academy
 
What is row level isolation on cassandra
What is row level isolation on cassandraWhat is row level isolation on cassandra
What is row level isolation on cassandraKazutaka Tomita
 
Deep dive into PostgreSQL statistics.
Deep dive into PostgreSQL statistics.Deep dive into PostgreSQL statistics.
Deep dive into PostgreSQL statistics.Alexey Lesovsky
 

Tendances (20)

Openstack taskflow 簡介
Openstack taskflow 簡介Openstack taskflow 簡介
Openstack taskflow 簡介
 
Node day 2014
Node day 2014Node day 2014
Node day 2014
 
PyCon KR 2019 sprint - RustPython by example
PyCon KR 2019 sprint  - RustPython by examplePyCon KR 2019 sprint  - RustPython by example
PyCon KR 2019 sprint - RustPython by example
 
Rcpp11
Rcpp11Rcpp11
Rcpp11
 
Using zone.js
Using zone.jsUsing zone.js
Using zone.js
 
OTcl and C++ linkages in NS2
OTcl and C++ linkages in NS2OTcl and C++ linkages in NS2
OTcl and C++ linkages in NS2
 
Node.js Stream API
Node.js Stream APINode.js Stream API
Node.js Stream API
 
#Gophercon Talk by Smita Vijayakumar - Go's Context Library
#Gophercon Talk by Smita Vijayakumar - Go's Context Library#Gophercon Talk by Smita Vijayakumar - Go's Context Library
#Gophercon Talk by Smita Vijayakumar - Go's Context Library
 
NS2: Binding C++ and OTcl variables
NS2: Binding C++ and OTcl variablesNS2: Binding C++ and OTcl variables
NS2: Binding C++ and OTcl variables
 
Ns tutorial
Ns tutorialNs tutorial
Ns tutorial
 
Go Concurrency Basics
Go Concurrency Basics Go Concurrency Basics
Go Concurrency Basics
 
Angular2 rxjs
Angular2 rxjsAngular2 rxjs
Angular2 rxjs
 
Gogo shell
Gogo shellGogo shell
Gogo shell
 
Perl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one linersPerl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one liners
 
Paris Kafka Meetup - How to develop with Kafka
Paris Kafka Meetup - How to develop with KafkaParis Kafka Meetup - How to develop with Kafka
Paris Kafka Meetup - How to develop with Kafka
 
Zabbix LLD from a C Module by Jan-Piet Mens
Zabbix LLD from a C Module by Jan-Piet MensZabbix LLD from a C Module by Jan-Piet Mens
Zabbix LLD from a C Module by Jan-Piet Mens
 
Php 5.6 From the Inside Out
Php 5.6 From the Inside OutPhp 5.6 From the Inside Out
Php 5.6 From the Inside Out
 
Call stack, event loop and async programming
Call stack, event loop and async programmingCall stack, event loop and async programming
Call stack, event loop and async programming
 
What is row level isolation on cassandra
What is row level isolation on cassandraWhat is row level isolation on cassandra
What is row level isolation on cassandra
 
Deep dive into PostgreSQL statistics.
Deep dive into PostgreSQL statistics.Deep dive into PostgreSQL statistics.
Deep dive into PostgreSQL statistics.
 

Similaire à Minion pool - a worker pool for nodejs

Asynchronous programming done right - Node.js
Asynchronous programming done right - Node.jsAsynchronous programming done right - Node.js
Asynchronous programming done right - Node.jsPiotr Pelczar
 
Adding 1.21 Gigawatts to Applications with RabbitMQ (PHPNW Dec 2014 Meetup)
Adding 1.21 Gigawatts to Applications with RabbitMQ (PHPNW Dec 2014 Meetup)Adding 1.21 Gigawatts to Applications with RabbitMQ (PHPNW Dec 2014 Meetup)
Adding 1.21 Gigawatts to Applications with RabbitMQ (PHPNW Dec 2014 Meetup)James Titcumb
 
The Ring programming language version 1.5.1 book - Part 12 of 180
The Ring programming language version 1.5.1 book - Part 12 of 180The Ring programming language version 1.5.1 book - Part 12 of 180
The Ring programming language version 1.5.1 book - Part 12 of 180Mahmoud Samir Fayed
 
Automation with Ansible and Containers
Automation with Ansible and ContainersAutomation with Ansible and Containers
Automation with Ansible and ContainersRodolfo Carvalho
 
Finagle and Java Service Framework at Pinterest
Finagle and Java Service Framework at PinterestFinagle and Java Service Framework at Pinterest
Finagle and Java Service Framework at PinterestPavan Chitumalla
 
Node.js System: The Landing
Node.js System: The LandingNode.js System: The Landing
Node.js System: The LandingHaci Murat Yaman
 
Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)
Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)
Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)James Titcumb
 
Think Async: Asynchronous Patterns in NodeJS
Think Async: Asynchronous Patterns in NodeJSThink Async: Asynchronous Patterns in NodeJS
Think Async: Asynchronous Patterns in NodeJSAdam L Barrett
 
Mysql handle socket
Mysql handle socketMysql handle socket
Mysql handle socketPhilip Zhong
 
Job Queue in Golang
Job Queue in GolangJob Queue in Golang
Job Queue in GolangBo-Yi Wu
 
Aplicações assíncronas no Android com
Coroutines & Jetpack
Aplicações assíncronas no Android com
Coroutines & JetpackAplicações assíncronas no Android com
Coroutines & Jetpack
Aplicações assíncronas no Android com
Coroutines & JetpackNelson Glauber Leal
 
ReactJS Component Lifecycle hooks with examples
ReactJS Component Lifecycle hooks with examples ReactJS Component Lifecycle hooks with examples
ReactJS Component Lifecycle hooks with examples Ravi Mone
 
Streaming replication in practice
Streaming replication in practiceStreaming replication in practice
Streaming replication in practiceAlexey Lesovsky
 
Nine Circles of Inferno or Explaining the PostgreSQL Vacuum
Nine Circles of Inferno or Explaining the PostgreSQL VacuumNine Circles of Inferno or Explaining the PostgreSQL Vacuum
Nine Circles of Inferno or Explaining the PostgreSQL VacuumAlexey Lesovsky
 
생산적인 개발을 위한 지속적인 테스트
생산적인 개발을 위한 지속적인 테스트생산적인 개발을 위한 지속적인 테스트
생산적인 개발을 위한 지속적인 테스트기룡 남
 
Bonnes pratiques de développement avec Node js
Bonnes pratiques de développement avec Node jsBonnes pratiques de développement avec Node js
Bonnes pratiques de développement avec Node jsFrancois Zaninotto
 
The Ring programming language version 1.6 book - Part 15 of 189
The Ring programming language version 1.6 book - Part 15 of 189The Ring programming language version 1.6 book - Part 15 of 189
The Ring programming language version 1.6 book - Part 15 of 189Mahmoud Samir Fayed
 
AngularJS: what is underneath the hood
AngularJS: what is underneath the hood AngularJS: what is underneath the hood
AngularJS: what is underneath the hood DA-14
 

Similaire à Minion pool - a worker pool for nodejs (20)

Asynchronous programming done right - Node.js
Asynchronous programming done right - Node.jsAsynchronous programming done right - Node.js
Asynchronous programming done right - Node.js
 
Adding 1.21 Gigawatts to Applications with RabbitMQ (PHPNW Dec 2014 Meetup)
Adding 1.21 Gigawatts to Applications with RabbitMQ (PHPNW Dec 2014 Meetup)Adding 1.21 Gigawatts to Applications with RabbitMQ (PHPNW Dec 2014 Meetup)
Adding 1.21 Gigawatts to Applications with RabbitMQ (PHPNW Dec 2014 Meetup)
 
Node js
Node jsNode js
Node js
 
The Ring programming language version 1.5.1 book - Part 12 of 180
The Ring programming language version 1.5.1 book - Part 12 of 180The Ring programming language version 1.5.1 book - Part 12 of 180
The Ring programming language version 1.5.1 book - Part 12 of 180
 
Automation with Ansible and Containers
Automation with Ansible and ContainersAutomation with Ansible and Containers
Automation with Ansible and Containers
 
Finagle and Java Service Framework at Pinterest
Finagle and Java Service Framework at PinterestFinagle and Java Service Framework at Pinterest
Finagle and Java Service Framework at Pinterest
 
Node.js System: The Landing
Node.js System: The LandingNode.js System: The Landing
Node.js System: The Landing
 
Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)
Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)
Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)
 
Think Async: Asynchronous Patterns in NodeJS
Think Async: Asynchronous Patterns in NodeJSThink Async: Asynchronous Patterns in NodeJS
Think Async: Asynchronous Patterns in NodeJS
 
Mysql handle socket
Mysql handle socketMysql handle socket
Mysql handle socket
 
Node js lecture
Node js lectureNode js lecture
Node js lecture
 
Job Queue in Golang
Job Queue in GolangJob Queue in Golang
Job Queue in Golang
 
Aplicações assíncronas no Android com
Coroutines & Jetpack
Aplicações assíncronas no Android com
Coroutines & JetpackAplicações assíncronas no Android com
Coroutines & Jetpack
Aplicações assíncronas no Android com
Coroutines & Jetpack
 
ReactJS Component Lifecycle hooks with examples
ReactJS Component Lifecycle hooks with examples ReactJS Component Lifecycle hooks with examples
ReactJS Component Lifecycle hooks with examples
 
Streaming replication in practice
Streaming replication in practiceStreaming replication in practice
Streaming replication in practice
 
Nine Circles of Inferno or Explaining the PostgreSQL Vacuum
Nine Circles of Inferno or Explaining the PostgreSQL VacuumNine Circles of Inferno or Explaining the PostgreSQL Vacuum
Nine Circles of Inferno or Explaining the PostgreSQL Vacuum
 
생산적인 개발을 위한 지속적인 테스트
생산적인 개발을 위한 지속적인 테스트생산적인 개발을 위한 지속적인 테스트
생산적인 개발을 위한 지속적인 테스트
 
Bonnes pratiques de développement avec Node js
Bonnes pratiques de développement avec Node jsBonnes pratiques de développement avec Node js
Bonnes pratiques de développement avec Node js
 
The Ring programming language version 1.6 book - Part 15 of 189
The Ring programming language version 1.6 book - Part 15 of 189The Ring programming language version 1.6 book - Part 15 of 189
The Ring programming language version 1.6 book - Part 15 of 189
 
AngularJS: what is underneath the hood
AngularJS: what is underneath the hood AngularJS: what is underneath the hood
AngularJS: what is underneath the hood
 

Dernier

Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 

Dernier (20)

Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 

Minion pool - a worker pool for nodejs

  • 2. MinionPool - Overview ● ● ● ● ● ● ● ● Easily create worker pools in seconds Based on callbacks (hooks) into the lifecycle of the pool Let's your do any number and type of tasks in parallel ONLY if those tasks do not rely in the order of processing Best results when doing idempotent operations Actually a family of pools: ○ MinionPool ○ ArrayMinionPool ○ MysqlMinionPool ○ RabbitMQMinionPool Can poll for tasks or accept injected tasks Open Source
  • 3. MinionPool - Proven in the field ● ● ● ● ● Log files processing Large mysql tables processing ElasticSearch reindexing S3 uploaders Web crawlers
  • 4. MinionPool - Installing ● ● ● npm install minionpool npm install mysql_minionpool npm install rabbitmq_minionpool
  • 5. MinionPool - As dependency { "dependencies": { "minionpool": "*", "rabbitmq_minionpool": "*", "mysql_minionpool": "*" } }
  • 6. MinionPool - Concepts ● ● TaskSource: Used to produce and introduce tasks into the pool. A task source will be initialized, used, and then shutdown Minion: Each pool consists of N minions. Tasks will be assigned to the first available minion. Each minion will be initialized, used, and then shutdown when the TaskSource has reported that no more tasks are available. MySQL RabbitMQ W1 Poll Inject Task Source Pool W2
  • 7. MinionPool - How to use it var minionpoolMod = require('minionpool'); var minionPool = new minionpoolMod.MinionPool(options); minionPool.start();
  • 8. MinionPool - Options var options = { name: 'test', debug: true, concurrency: 5, logger: console.log, continueOnError: true, taskSourceStart: function(callback) { … callback(err, state); } taskSourceNext: function(state, callback) { callback(err, task); return state; }, taskSourceEnd: function(state, callback) { callback(); }, minionTaskHandler: function(task, state, callback) { callback(err, state); }, minionStart: function(callback) { callback(err, state); }, minionEnd: function(state, callback) { callback(); }, poolEnd: function() { process.exit(0); } };
  • 9. MinionPool - ArrayMinionPool var options = { name: 'test', concurrency: 5, minionTaskHandler: function(task, state, callback) { setTimeout(function() { callback(undefined, state); }, Math.floor(Math.random() * 500)); }, poolEnd: function() { process.exit(0); } }; var data = [.., .., .., .., ...]; var minionPool = new minionsMod.ArrayMinionPool(options, data); minionPool.start();
  • 10. MinionPool - MysqlMinionPool var pool = new mysqlMinionPoolMod.MysqlMinionPool({ mysqlConfig: { host: '127.0.0.1', user: 'root', password: 'pass', database: 'db', port: 3306 }, concurrency: 5, // How many pages to get concurrently... rowConcurrency: 1, // ... and how many concurrent rows processed PER query taskSourceStart: function(callback) { callback(undefined, {page: 0, pageSize: 10}); }, minionTaskHandler: function(task, state, callback) { callback(undefined, state); } }); pool.start();
  • 11. MinionPool - MysqlMinionPool taskSourceNext: function(state, callback) { var query = "SELECT * FROM `db`.`table` LIMIT ?,?"; state.mysqlPool.getConnection(function(err, mysqlConnection) { if(err) { callback(err, undefined); } else { mysqlConnection.query( query, [state.page * state.pageSize, state.pageSize], function(err, rows) { mysqlConnection.release(); if(err) { callback(err, undefined); } else if(rows.length === 0) { callback(undefined, undefined); } else { callback(undefined, rows); } } ); } }); state.page++; return state; },
  • 12. MinionPool - RabbitMQMinionPool var options = { name: 'test', concurrency: 5, mqOptions: { exchangeName: 'workers', // Will also create workers.retry queueName: 'myWorkers', // Will also create myWorkers.retry routingKey: 'myWorkers', // Optional. Equals to queueName if missing } }; var pool = new minionsMod.RabbitMqMinionPool(options); process.on('SIGINT', function() { pool.end(); }); pool.start();
  • 13. MinionPool - RabbitMQMinionPool minionTaskHandler: function(msg, state, callback) { var payload = msg.payload; var headers = msg.headers; var deliveryInfo = msg.deliveryInfo; var message = msg.message; var queue = msg.queue; console.log('got task: %s', util.inspect(payload)); // See the node-amqp doc for more info. message.reject(); // or message.acknowledge(); callback(undefined, state); },
  • 14. MinionPool - RabbitMQMinionPool ● ● ● ● Dead Letter eXchanges to support retrying failed (rejected) operations Channels in confirmation mode, so failed publishes will be notified Messages published as persistent Queues and exchanges marked as durable, autodelete = false
  • 15. MinionPool - MultiCore taskset: "used to set or retrieve the CPU affinity of a running process given its PID or to launch a new COMMAND with a given CPU affinity" ● ● http://linux.die.net/man/1/taskset https://github.com/karelzak/util-linux $ taskset -c N /usr/node/bin/node bin/myworker # 0 =< N < number of cores
  • 16. MinionPool - MultiCore For mysql minion pools: you can divide the total number of rows per the number of cores available, and launch one pool per core that will process then given range of id's. For rabbitmq minion pools: Just spawn as many pools per core needed, they will compete to consume tasks, rabbitmq will handle the complex part :)