Skip to content

Commit b90f8b2

Browse files
author
joseconstela
committed
Fix #2
1 parent 3f89a46 commit b90f8b2

File tree

7 files changed

+77
-22
lines changed

7 files changed

+77
-22
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ $ npm i -g @tideflowio/tideflow-agent
1616
Usage: index [options]
1717

1818
Options:
19-
--noupdate Opt-out of update version check
20-
-v, --version output the version number
21-
-t, --token [token] Authentication token
22-
-h, --help output usage information
19+
-v, --version output the version number
20+
-c, --concurrency [concurrency] Max number of jobs the agent should process concurrently
21+
-t, --token [token] Authentication token
22+
--noupdate Opt-out of update version check
23+
-h, --help output usage information
2324

2425
Examples:
2526
$ tideflow-agent --help

agent.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,42 @@ const io = require('socket.io-client')
33
const colors = require('colors')
44
const pretty = require('./helpers/pretty')
55
const runner = require('./runner')
6+
const os = require('os')
67

78
/**
89
* @param {Object} cli's parameters parsed via commander's npm package
910
*/
1011
module.exports.exec = (program) => {
1112

13+
const cpusCounts = os.cpus().length
14+
const programConcurrency = program.concurrency || cpusCounts
15+
let concurrency = programConcurrency >= cpusCounts ? cpusCounts : programConcurrency
16+
17+
const q = require('queue')({
18+
concurrency: concurrency
19+
})
20+
1221
let agent = {}
1322

1423
// Shows welcome messages
1524
console.log(pretty.logo())
1625
console.log(` || Tideflow.io - agent ${pjson.version}`.blue)
26+
console.log(` || Using ${concurrency} as concurrency`.yellow)
1727

1828
const URL = process.env.TF_AGENT_URL || 'http://localhost:1337'
1929

2030
const socket = io(`${URL}?token=${program.token}`)
2131

22-
// socket.on('connect', function () {})
23-
24-
// socket.on('reconnect', function () {})
25-
2632
// Execute command
2733
socket.on('tf.command', function (req) {
2834
if (!agent.authenticated) return
29-
runner.cmd(socket, 'tf.command', req)
35+
q.push(runner.cmd(socket, 'tf.command', req))
3036
})
3137

3238
// Execute code
3339
socket.on('tf.code', function () {
34-
if (!agent.token) return
40+
if (!agent.authenticated) return
41+
q.push(runner.code(socket, 'tf.code', req))
3542
})
3643

3744
// Authorize agent

index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
#!/usr/bin/env node
22
'use strict'
33
const program = require('commander')
4+
const os = require('os')
45
const pkg = require('./package.json')
56

67
program
78
.version(pkg.version, '-v, --version')
9+
10+
.option('-c, --concurrency [concurrency]', 'Max number of jobs the agent should process concurrently', (v) => {
11+
const concurrency = parseInt(v)
12+
if (!concurrency) { throw new Error('Concurrency parameter must be a valid integer') }
13+
return parseInt(concurrency)
14+
})
815
.option('-t, --token [token]', 'Authentication token', (v) => {
916
return v || process.env.TIDEFLOWIO_AGENT_TOKEN
1017
})
@@ -16,6 +23,8 @@ program
1623
program.on('--help', function(){
1724
console.log('')
1825
console.log('Examples:')
26+
console.log(' $ tideflow-agent -t agent-auth-token')
27+
console.log(' $ tideflow-agent -c 16 -t agent-auth-token')
1928
console.log(' $ tideflow-agent --help')
2029
console.log(' $ tideflow-agent -h')
2130
})

package-lock.json

Lines changed: 30 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
"dependencies": {
3535
"colors": "^1.3.3",
3636
"commander": "^2.19.0",
37+
"cross-spawn": "^6.0.5",
38+
"queue": "^6.0.0",
3739
"socket.io-client": "^2.2.0",
3840
"update-notifier": "^2.5.0"
3941
},

runner.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { spawn } = require('child_process')
1+
const spawn = require('cross-spawn')
22
const report = require('./helpers/report')
33

44
/**
@@ -49,4 +49,19 @@ const cmd = (socket, topic, req) => {
4949
})
5050
}
5151

52-
module.exports.cmd = cmd
52+
module.exports.cmd = cmd
53+
54+
/**
55+
* Handles the execution of nodejs code sent from the platform
56+
*
57+
* @param {Object} socket Socket's io socket that requested the execution
58+
* @param {String} topic Original message's topic
59+
* @param {Object} req Original request that came fromthe platform
60+
*/
61+
const code = (socket, topic, req) => {
62+
return new Promise((resolve, reject) => {
63+
return reject('Not supported yet')
64+
})
65+
}
66+
67+
module.exports.code = code

0 commit comments

Comments
 (0)