Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
feat: pass farm options
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Feb 3, 2019
1 parent 57ed1e5 commit ed08ed1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
23 changes: 21 additions & 2 deletions bin/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ if (!filename) cli.showHelp()
const { file: fileOpts } = argv
const { flags: farmOpts } = cli

const { delayBetweenWorkers } = farmOpts
const {
maxRetries,
autoStart,
maxCallTime,
maxConcurrentCalls,
delayBetweenWorkers,
maxConcurrentWorkers,
maxConcurrentCallsPerWorker
} = farmOpts
const numWorkers = getNumWorkers(farmOpts)
const workersRange = [...Array(numWorkers).keys()]
const spawnWorkers = workersRange.map(spawnWorker)
Expand All @@ -37,7 +45,18 @@ function spawnWorker (id) {

function worker (cb) {
debug('creating %o', parsedArgs)
farm(parsedArgs, process.exit)
farm(
{
...parsedArgs,
maxConcurrentWorkers,
maxConcurrentCallsPerWorker,
maxRetries,
autoStart,
maxCallTime,
maxConcurrentCalls
},
process.exit
)
return setTimeout(cb, delayBetweenWorkers)
}

Expand Down
6 changes: 4 additions & 2 deletions examples/basic/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Basic
# Shared memory

An example for illustrating that the memory can be shared between workers in the same core.

```bash
$ DEBUG=farm-cli farm -w 2 -n 5 -d 0 examples/basic --your-file-flags foo=bar
$ DEBUG=farm-cli farm -n 1 -w 5 -d 0 examples/basic --your-file-flags foo=bar
```
13 changes: 10 additions & 3 deletions examples/basic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@

const shared = []

module.exports = function (opts) {
const { worker } = opts
module.exports = function (opts, cb) {
const {
isMaster,
worker,
maxConcurrentCallsPerWorker,
maxConcurrentWorkers
} = opts
const total = maxConcurrentCallsPerWorker * maxConcurrentWorkers
console.log('-----------------------------')
console.log(`Hello I'm worker #${worker}`)
console.log(`Hello I'm worker #${worker} ${isMaster ? '(master)' : ''}`)
const printSharedWorkers = shared.join(' ') || 'none'
console.log(`The shared variable was visited by ${printSharedWorkers}`)
shared.push(`#${worker}`)
console.log('-----------------------------\n')
if (total === worker + 1) return cb()
}

0 comments on commit ed08ed1

Please sign in to comment.