-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chore: accelerate prod build with worker-farm (#162)
- Loading branch information
Showing
4 changed files
with
52 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* eslint-disable */ | ||
const execSync = require('child_process').execSync; | ||
|
||
/** | ||
* Build a single locale | ||
* | ||
* @param {string} locale - locale to build | ||
* @param {*} callback - callback from worker-farm master process | ||
*/ | ||
module.exports = (locale, callback) => { | ||
try { | ||
console.log(`Building ${locale}...`); | ||
// build assets for a single locale | ||
execSync(`time LANGUAGE=${locale} yarn run build-prod`); | ||
callback(); | ||
} catch (error) { | ||
console.error(`Error: Failed to build ${locale}`); | ||
callback(true); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,32 @@ | ||
const execSync = require('child_process').execSync; | ||
/* eslint-disable */ | ||
const workerFarm = require('worker-farm'); | ||
const locales = require('./locales'); | ||
const numCPUs = require('os').cpus().length; | ||
const execSync = require('child_process').execSync; | ||
const path = require('path'); | ||
|
||
const filename = path.basename(__filename); | ||
const localesCount = locales.length; | ||
let counter = 0; | ||
|
||
const workers = workerFarm( | ||
{ | ||
maxConcurrentWorkers: numCPUs - 1, | ||
maxRetries: 0 | ||
}, | ||
require.resolve('./build_locale.js') | ||
); | ||
|
||
locales.forEach(locale => { | ||
workers(locale, error => { | ||
if (++counter === localesCount || error) { | ||
// terminate after all locales have been processed | ||
workerFarm.end(workers); | ||
} | ||
|
||
locales.forEach((language) => { | ||
console.log(`Building ${language}...`); | ||
execSync(`LANGUAGE=${language} yarn run build-prod`); | ||
if (error) { | ||
// kill the node process that spawns the workers as well as all processes been spawned | ||
execSync(`ps ax | grep "${filename}" | cut -b1-06 | xargs -t kill`); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters