Skip to content

Commit

Permalink
Extract workerfarm into seperate package (#2162)
Browse files Browse the repository at this point in the history
  • Loading branch information
DeMoorJasper authored and devongovett committed Oct 18, 2018
1 parent 75310e1 commit b1e6d59
Show file tree
Hide file tree
Showing 29 changed files with 171 additions and 1,550 deletions.
File renamed without changes.
18 changes: 18 additions & 0 deletions packages/core/babel-register/package.json
@@ -0,0 +1,18 @@
{
"name": "@parcel/babel-register",
"version": "1.10.3",
"description": "Blazing fast, zero configuration web application bundler",
"main": "index.js",
"license": "MIT",
"private": true,
"repository": {
"type": "git",
"url": "https://github.com/parcel-bundler/parcel.git"
},
"peerDependencies": {
"@babel/core": "^7.0.0"
},
"dependencies": {
"@babel/register": "^7.0.0"
}
}
8 changes: 4 additions & 4 deletions packages/core/parcel-bundler/package.json
Expand Up @@ -55,7 +55,6 @@
"node-libs-browser": "^2.0.0",
"opn": "^5.1.0",
"ora": "^2.1.0",
"physical-cpu-count": "^2.0.0",
"postcss": "^6.0.19",
"postcss-value-parser": "^3.3.0",
"posthtml": "^0.11.2",
Expand All @@ -71,15 +70,15 @@
"toml": "^2.3.3",
"tomlify-j0.4": "^3.0.0",
"v8-compile-cache": "^2.0.0",
"ws": "^5.1.1"
"ws": "^5.1.1",
"@parcel/workers": "^1.10.3"
},
"devDependencies": {
"@babel/cli": "^7.0.0",
"@babel/plugin-syntax-export-default-from": "^7.0.0",
"@babel/plugin-syntax-export-namespace-from": "^7.0.0",
"@babel/plugin-transform-runtime": "^7.0.0",
"@babel/preset-flow": "^7.0.0",
"@babel/register": "^7.0.0",
"@vue/component-compiler-utils": "^2.0.0",
"babel-core": "^6.26.3",
"babel-preset-env": "^1.7.0",
Expand Down Expand Up @@ -115,7 +114,8 @@
"sugarss": "^2.0.0",
"typescript": "^3.0.0",
"vue": "^2.5.16",
"vue-template-compiler": "^2.5.16"
"vue-template-compiler": "^2.5.16",
"@parcel/babel-register": "^1.10.3"
},
"scripts": {
"test": "cross-env NODE_ENV=test mocha",
Expand Down
6 changes: 4 additions & 2 deletions packages/core/parcel-bundler/src/Bundler.js
@@ -1,7 +1,7 @@
const fs = require('./utils/fs');
const Resolver = require('./Resolver');
const Parser = require('./Parser');
const WorkerFarm = require('./workerfarm/WorkerFarm');
const WorkerFarm = require('@parcel/workers');
const Path = require('path');
const Bundle = require('./Bundle');
const Watcher = require('./Watcher');
Expand Down Expand Up @@ -379,7 +379,9 @@ class Bundler extends EventEmitter {
this.options.hmrPort = await this.hmr.start(this.options);
}

this.farm = WorkerFarm.getShared(this.options);
this.farm = await WorkerFarm.getShared(this.options, {
workerPath: require.resolve('./worker.js')
});
}

async stop() {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/parcel-bundler/src/Logger.js
Expand Up @@ -5,7 +5,7 @@ const emoji = require('./utils/emoji');
const {countBreaks} = require('grapheme-breaker');
const stripAnsi = require('strip-ansi');
const ora = require('ora');
const WorkerFarm = require('./workerfarm/WorkerFarm');
const WorkerFarm = require('@parcel/workers');
const path = require('path');
const fs = require('fs');

Expand Down
2 changes: 1 addition & 1 deletion packages/core/parcel-bundler/src/utils/installPackage.js
Expand Up @@ -7,7 +7,7 @@ const pipeSpawn = require('./pipeSpawn');
const PromiseQueue = require('./PromiseQueue');
const path = require('path');
const fs = require('./fs');
const WorkerFarm = require('../workerfarm/WorkerFarm');
const WorkerFarm = require('@parcel/workers');

const YARN_LOCK = 'yarn.lock';

Expand Down
2 changes: 1 addition & 1 deletion packages/core/parcel-bundler/test/mocha.opts
@@ -1,3 +1,3 @@
--timeout 50000
--require ./test/babel-register
--require @parcel/babel-register
--exit
1 change: 1 addition & 0 deletions packages/core/workers/.gitignore
@@ -0,0 +1 @@
lib
5 changes: 5 additions & 0 deletions packages/core/workers/index.js
@@ -0,0 +1,5 @@
// Node 8 supports native async functions - no need to use compiled code!
module.exports =
parseInt(process.versions.node, 10) < 8
? require('./lib/WorkerFarm')
: require('./src/WorkerFarm');
28 changes: 28 additions & 0 deletions packages/core/workers/package.json
@@ -0,0 +1,28 @@
{
"name": "@parcel/workers",
"version": "1.10.3",
"description": "Blazing fast, zero configuration web application bundler",
"main": "index.js",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/parcel-bundler/parcel.git"
},
"engines": {
"node": ">= 6.0.0"
},
"scripts": {
"test": "cross-env NODE_ENV=test mocha",
"test-ci": "yarn build && yarn test",
"format": "prettier --write \"./{src,bin,test}/**/*.{js,json,md}\"",
"lint": "eslint . && prettier \"./{src,bin,test}/**/*.{js,json,md}\" --list-different",
"build": "babel src -d lib",
"prepublish": "yarn build"
},
"devDependencies": {
"mocha": "^5.2.0"
},
"dependencies": {
"physical-cpu-count": "^2.0.0"
}
}
8 changes: 8 additions & 0 deletions packages/core/workers/src/.babelrc
@@ -0,0 +1,8 @@
{
"presets": [["@babel/preset-env", {
"targets": {
"node": "6"
}
}]],
"plugins": ["@babel/plugin-transform-runtime"]
}
3 changes: 3 additions & 0 deletions packages/core/workers/src/.eslintrc.json
@@ -0,0 +1,3 @@
{
"extends": "../../../../.eslintrc.json"
}
Expand Up @@ -2,13 +2,9 @@ const childProcess = require('child_process');
const {EventEmitter} = require('events');
const errorUtils = require('./errorUtils');

const childModule =
parseInt(process.versions.node, 10) < 8
? require.resolve('../../lib/workerfarm/child')
: require.resolve('../../src/workerfarm/child');
const childModule = require.resolve('./child');

let WORKER_ID = 0;

class Worker extends EventEmitter {
constructor(options) {
super();
Expand Down
@@ -1,28 +1,37 @@
const {EventEmitter} = require('events');
const errorUtils = require('./errorUtils');
const Worker = require('./Worker');
const cpuCount = require('../utils/cpuCount');
const cpuCount = require('./cpuCount');

let shared = null;

/**
* workerPath should always be defined inside farmOptions
*/

class WorkerFarm extends EventEmitter {
constructor(options, farmOptions = {}) {
super();
this.options = Object.assign(
{
maxConcurrentWorkers: WorkerFarm.getNumWorkers(),
maxConcurrentCallsPerWorker: WorkerFarm.getConcurrentCallsPerWorker(),
forcedKillTime: 500,
warmWorkers: true,
useLocalWorker: true,
workerPath: '../worker'
},
farmOptions
);
this.options = {
maxConcurrentWorkers: WorkerFarm.getNumWorkers(),
maxConcurrentCallsPerWorker: WorkerFarm.getConcurrentCallsPerWorker(),
forcedKillTime: 500,
warmWorkers: true,
useLocalWorker: true
};

if (farmOptions) {
this.options = Object.assign(this.options, farmOptions);
}

this.warmWorkers = 0;
this.workers = new Map();
this.callQueue = [];

if (!this.options.workerPath) {
throw new Error('Please provide a worker path!');
}

this.localWorker = require(this.options.workerPath);
this.run = this.mkhandle('run');

Expand Down Expand Up @@ -243,9 +252,16 @@ class WorkerFarm extends EventEmitter {
);
}

static getShared(options) {
static async getShared(options, farmOptions) {
// Farm options shouldn't be considered safe to overwrite
// and require an entire new instance to be created
if (shared && farmOptions) {
await shared.end();
shared = null;
}

if (!shared) {
shared = new WorkerFarm(options);
shared = new WorkerFarm(options, farmOptions);
} else if (options) {
shared.init(options);
}
Expand All @@ -263,12 +279,12 @@ class WorkerFarm extends EventEmitter {
: cpuCount();
}

static callMaster(request, awaitResponse = true) {
static async callMaster(request, awaitResponse = true) {
if (WorkerFarm.isWorker()) {
const child = require('./child');
return child.addCall(request, awaitResponse);
} else {
return WorkerFarm.getShared().processRequest(request);
return (await WorkerFarm.getShared()).processRequest(request);
}
}

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions packages/core/workers/test/.babelrc
@@ -0,0 +1,9 @@
{
"presets": [["@babel/preset-env", {
"targets": {
"node": "current"
}
}]],
"plugins": ["@babel/plugin-transform-runtime"],
"ignore": ["integration"]
}
6 changes: 6 additions & 0 deletions packages/core/workers/test/.eslintrc.json
@@ -0,0 +1,6 @@
{
"extends": "../../../../.eslintrc.json",
"env": {
"mocha": true
}
}
Expand Up @@ -7,4 +7,4 @@ function init() {
}

exports.run = run;
exports.init = init;
exports.init = init;
Expand Up @@ -9,4 +9,4 @@ function init(opt) {
}

exports.run = run;
exports.init = init;
exports.init = init;
@@ -1,15 +1,19 @@
const WorkerFarm = require(`../../../${parseInt(process.versions.node, 10) < 8 ? 'lib' : 'src'}/workerfarm/WorkerFarm`);
const WorkerFarm = require(`../../../${
parseInt(process.versions.node, 10) < 8 ? 'lib' : 'src'
}/WorkerFarm`);

function run() {
let result = [process.pid];
return new Promise((resolve, reject) => {
WorkerFarm.callMaster({
location: require.resolve('./master-process-id.js'),
args: []
}).then((pid) => {
result.push(pid)
resolve(result);
}).catch(reject);
})
.then(pid => {
result.push(pid);
resolve(result);
})
.catch(reject);
});
}

Expand All @@ -18,4 +22,4 @@ function init() {
}

exports.run = run;
exports.init = init;
exports.init = init;
@@ -1,4 +1,6 @@
const WorkerFarm = require(`../../../${parseInt(process.versions.node, 10) < 8 ? 'lib' : 'src'}/workerfarm/WorkerFarm`);
const WorkerFarm = require(`../../../${
parseInt(process.versions.node, 10) < 8 ? 'lib' : 'src'
}/WorkerFarm`);

function run(a, b) {
return WorkerFarm.callMaster({
Expand All @@ -12,4 +14,4 @@ function init() {
}

exports.run = run;
exports.init = init;
exports.init = init;
@@ -1,3 +1,3 @@
module.exports = function() {
return process.pid;
}
};
@@ -1,3 +1,3 @@
module.exports = function(a, b) {
return a + b;
}
};
Expand Up @@ -7,4 +7,4 @@ function init() {
}

exports.run = run;
exports.init = init;
exports.init = init;
2 changes: 2 additions & 0 deletions packages/core/workers/test/mocha.opts
@@ -0,0 +1,2 @@
--require @parcel/babel-register
--exit
@@ -1,5 +1,5 @@
const assert = require('assert');
const WorkerFarm = require('../src/workerfarm/WorkerFarm');
const WorkerFarm = require('../index');

describe('WorkerFarm', () => {
it('Should start up workers', async () => {
Expand Down

0 comments on commit b1e6d59

Please sign in to comment.