Skip to content

Instantly share code, notes, and snippets.

@Gaubee
Last active January 11, 2018 00:50
Show Gist options
  • Save Gaubee/a31c4a31b8e40b6c236a38144f7a2352 to your computer and use it in GitHub Desktop.
Save Gaubee/a31c4a31b8e40b6c236a38144f7a2352 to your computer and use it in GitHub Desktop.
separate read and write 读写分离
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const cluster = require("cluster"); // const cluster = require("cluster")
const os = require("os"); // const os = require("os")
/* Share code */
const prepare_job_promise = new Promise((resolve, reject) => {
// bootstrap modules
setTimeout(resolve, 1000);
});
// or write like this
class PromiseOut extends Promise {
constructor() {
super((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
});
}
}
const prepare_job_promise2 = new PromiseOut();
setTimeout(prepare_job_promise2.resolve, 1000);
/* Consts */
const READY_SIGN = "ready";
const RUN_SIGN = "run";
const taskMap = new Map();
taskMap.set("read mongodb", async function runner() { });
taskMap.set("job2", async function runner() { });
// Foker cluster
if (cluster.isMaster) {
/*Master Job*/
// do write into mongodb.
console.log(`Master ${process.pid} is running`);
/*Simple Fork*/
function simpleFork() {
const numCPUs = os.cpus().length;
/* save CPU,master*1,Mongodb*1,redis*1 , or more*/
const saveCPUs = 3;
const fork_num = numCPUs - saveCPUs;
for (let i = 0; i < fork_num; i += 1) {
const worker = cluster.fork();
worker.on("message", msg => {
if (msg === READY_SIGN) {
/*object will be stringify, and send, and parse to object in child*/
worker.send({
commond: "run",
task_name: "job2",
});
}
});
}
}
/*Custom Fork*/
function customFork() {
for (let task_name of taskMap.keys()) {
const worker = cluster.fork();
worker.on("message", msg => {
if (msg === READY_SIGN) {
worker.send({
commond: "run",
task_name,
});
}
});
}
}
}
else {
// 子进程 child process
// talk to master, child is ready.
process.on("message", msg => {
if (msg) {
const { commond, task_name } = msg;
if (commond === RUN_SIGN) {
const runner = taskMap.get(task_name);
if (runner) {
runner().catch(err => console.error(process.pid, err));
}
}
}
});
process.send(READY_SIGN);
}
//# sourceMappingURL=worker.test.js.map
import * as cluster from "cluster"; // const cluster = require("cluster")
import * as os from "os"; // const os = require("os")
/* Share code */
const prepare_job_promise = new Promise((resolve, reject) => {
// bootstrap modules
setTimeout(resolve, 1000);
});
// or write like this
class PromiseOut<T> extends Promise<T> {
constructor() {
super((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
});
}
resolve: (value?: T | PromiseLike<T>) => void;
reject: (reason?) => void;
}
const prepare_job_promise2 = new PromiseOut();
setTimeout(prepare_job_promise2.resolve, 1000);
/* Consts */
const READY_SIGN = "ready";
const RUN_SIGN = "run";
const taskMap = new Map();
taskMap.set("read mongodb", async function runner() {});
taskMap.set("job2", async function runner() {});
// Foker cluster
if (cluster.isMaster) {
/*Master Job*/
// do write into mongodb.
console.log(`Master ${process.pid} is running`);
/*Simple Fork*/
function simpleFork() {
const numCPUs = os.cpus().length;
/* save CPU,master*1,Mongodb*1,redis*1 , or more*/
const saveCPUs = 3;
const fork_num = numCPUs - saveCPUs;
for (let i = 0; i < fork_num; i += 1) {
const worker = cluster.fork();
worker.on("message", msg => {
if (msg === READY_SIGN) {
/*object will be stringify, and send, and parse to object in child*/
worker.send({
commond: "run",
task_name: "job2",
});
}
});
}
}
/*Custom Fork*/
function customFork() {
for (let task_name of taskMap.keys()) {
const worker = cluster.fork();
worker.on("message", msg => {
if (msg === READY_SIGN) {
worker.send({
commond: "run",
task_name,
});
}
});
}
}
} else {
// 子进程 child process
// talk to master, child is ready.
process.on("message", msg => {
if (msg) {
const { commond, task_name } = msg;
if (commond === RUN_SIGN) {
const runner = taskMap.get(task_name);
if (runner) {
runner().catch(err => console.error(process.pid, err));
}
}
}
});
process.send(READY_SIGN);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment