summaryrefslogtreecommitdiff
path: root/src/jobctl.js
blob: 05e91c81788502c2774273692552058767a56235 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#!/usr/bin/env node
const minimist = require('minimist')
const loggerModule = require('./lib/logger')
const config = require('./lib/config')
const package_json = require('../package.json')
const os = require('os')
const path = require('path')
const fs = require('fs/promises')
const {Connection, RequestMessage} = require('./lib/server')
const {isNumeric} = require('./lib/util')
const columnify = require('columnify')
const readline = require('readline')

const DEFAULT_CONFIG_PATH = path.join(os.homedir(), '.jobctl.conf')

const WORKER_COMMANDS = {
    'list-targets': workerListTargets,
    'memory-usage': workerMemoryUsage,
    'poll': workerPoll,
    'set-target-concurrency': workerSetTargetConcurrency,
    'pause': workerPause,
    'continue': workerContinue
}

const MASTER_COMMANDS = {
    'list-workers': masterListWorkers,
    // 'list-workers-memory-usage': masterListWorkersMemoryUsage,
    'memory-usage': masterMemoryUsage,
    'poke': masterPoke,

    // we can just reuse worker functions here, as they do the same
    'pause': workerPause,
    'continue': workerContinue,
}

/**
 * @type {Logger}
 */
let logger

/**
 * @type {Connection}
 */
let connection


main().catch(e => {
    console.error(e)
    process.exit(1)
})


async function main() {
    const argv = await initApp('jobctl')
    if (!argv.length)
        usage()

    const isMaster = config.get('master')

    logger.info('Working mode: ' + (isMaster ? 'master' : 'worker'))
    logger.trace('Command arguments: ', argv)

    let availableCommands = isMaster ? MASTER_COMMANDS : WORKER_COMMANDS
    let command = argv.shift()
    if (!(command in availableCommands)) {
        logger.error(`Unsupported command: '${command}'`)
        process.exit(1)
    }

    let host = config.get('host')
    let port = config.get('port')

    // connect to instance
    try {
        connection = new Connection()
        await connection.connect(host, port)

        logger.info('Successfully connected.')
    } catch (error) {
        logger.error('Connection failure:', error)
        process.exit(1)
    }

    try {
        await availableCommands[command](argv)
    } catch (error) {
        logger.error(error.message)
        logger.trace(error)
    }

    connection.close()
}

async function initApp(appName) {
    if (process.argv.length < 3)
        usage()

    process.on('SIGINT', term)
    process.on('SIGTERM', term)

    const argv = minimist(process.argv.slice(2), {
        boolean: ['master', 'version', 'help', 'password'],
        string: ['host', 'port', 'config', 'log-level'],
        stopEarly: true,
        default: {
            config: DEFAULT_CONFIG_PATH
        }
    })

    if (argv.help)
        usage()

    if (argv.version) {
        console.log(package_json.version)
        process.exit(0)
    }

    // read config
    if (await exists(argv.config)) {
        try {
            config.parseJobctlConfig(argv.config)
        } catch (e) {
            console.error(`config parsing error: ${e.message}`)
            process.exit(1)
        }
    }

    if (argv.master || config.get('master') === null)
        config.set('master', argv.master)

    for (let key of ['log-level', 'host', 'port']) {
        if (key in argv)
            config.set(key.replace('-', '_'), argv[key])
    }

    if (config.get('port') === null)
        config.set('port', config.get('master') ? 7081 : 7080)

    if (config.get('log_level') === null)
        config.set('log_level', 'warn')

    if (argv.password) {
        let password = await question('Enter password: ')
        config.set('password', password)
    }

    // init logger
    await loggerModule.init({
        levelConsole: config.get('log_level'),
        disableTimestamps: true
    })
    logger = loggerModule.getLogger(appName)

    process.title = appName

    ///  ///  ///
    ///  \\\  \\\
    ///  ///  ///
    ///  \\\  \\\
    ///  ///  ///
    /* * * * * */
    /*         */
    /*   ^_^   */
    /*         */
    /*   '_'   */
    /*         */
    /*   <_<   */
    /*         */
    /*   >_>   */
    /*         */
    /* * * * * */
    ///  ///  ///
    ///  \\\  \\\
    ///  ///  ///
    ///  \\\  \\\
    ///  ///  ///

    return argv['_'] || []
}

/**
 * @param {string} name
 * @param {null|object} data
 * @return {Promise<object>}
 */
async function request(name, data = null) {
    let req = new RequestMessage(name, data)
    let response = await connection.sendRequest(req)
    if (response.error)
        throw new Error(`Worker error: ${response.error}`)
    return response.data
}

async function workerListTargets() {
    let response = await request('status')
    const rows = []
    const columns = [
        'target',
        'concurrency',
        'length',
        'paused'
    ]
    for (const target in response.targets) {
        const row = [
            target,
            response.targets[target].concurrency,
            response.targets[target].length,
            response.targets[target].paused ? 'yes' : 'no'
        ]
        rows.push(row)
    }

    table(columns, rows)
}

async function workerMemoryUsage() {
    let response = await request('status')
    const columns = ['what', 'value']
    const rows = []
    for (const what in response.memoryUsage)
        rows.push([what, response.memoryUsage[what]])
    rows.push(['pendingJobPromises', response.jobPromisesCount])
    table(columns, rows)
}

async function workerPoll(argv) {
    return await sendCommandForTargets(argv, 'poll')
}

async function workerPause(argv) {
    return await sendCommandForTargets(argv, 'pause')
}

async function workerContinue(argv) {
    return await sendCommandForTargets(argv, 'continue')
}

async function workerSetTargetConcurrency(argv) {
    if (argv.length !== 2)
        throw new Error('Invalid number of arguments.')

    let [target, concurrency] = argv
    if (!isNumeric(concurrency))
        throw new Error(`'concurrency' must be a number.`)

    concurrency = parseInt(concurrency, 10)

    let response = await request('set-target-concurrency', {target, concurrency})
    console.log(response)
}

async function masterPoke(argv) {
    return await sendCommandForTargets(argv, 'poke')
}

async function masterMemoryUsage() {
    let response = await request('status')
    const columns = ['what', 'value']
    const rows = []
    for (const what in response.memoryUsage)
        rows.push([what, response.memoryUsage[what]])
    table(columns, rows)
}

async function masterListWorkers() {
    let response = await request('status', {poll_workers: true})
    const columns = ['worker', 'targets', 'concurrency', 'length', 'paused']
    const rows = []
    for (const worker of response.workers) {
        let info = `${worker.name}\n(${worker.remoteAddr})`
        let targets = Object.keys(worker.workerStatus.targets)
        let concurrencies = targets.map(t => worker.workerStatus.targets[t].concurrency)
        let lengths = targets.map(t => worker.workerStatus.targets[t].length)
        let pauses = targets.map(t => worker.workerStatus.targets[t].paused ? 'yes' : 'no')
        rows.push([
            info,
            targets.join("\n"),
            concurrencies.join("\n"),
            lengths.join("\n"),
            pauses.join("\n")
        ])
    }
    table(columns, rows)
}

async function sendCommandForTargets(targets, command) {
    if (!targets.length)
        throw new Error('No targets specified.')

    let response = await request(command, {targets})
    console.log(response)
}


function usage(exitCode = 0) {
    let s = `${process.argv[1]} [OPTIONS] COMMAND 

Worker commands:
    list-targets          Print list of targets, their length and inner state.
    memory-usage          Print info about memory usage of the worker.
    poll <...TARGETS>     Ask worker to get tasks for specified targets.
    
                          Example:
                          $ jobctl poke t1 t2 t3
                          
    set-target-concurrency <target> <concurrency>
                          Set concurrency of the target.
                          
    pause <...TARGETS>    Pause specified or all targets.
    continue <...TARGETS> Pause specified or all targets.
    
Master commands:
    list-workers           Print list of connected workers and their state.
    memory-usage           Print info about memory usage.
    poke <...TARGETS>      Poke specified targets.
    pause <...TARGETS>     Send pause() to all workers serving specified targets.
                           If no targets specified, just sends pause() to all
                           connected workers.
    continue <...TARGETS>  Send continue() to all workers serving specified
                           targets. If no targets specified, just sends pause()
                           to all connected workers.
                          
Options:
    --master              Connect to jobd-master instance.
    --host                Address of jobd or jobd-master instance.
    --port                Port. Default: 7080 when --master is not used,
                          7081 otherwise.
    --config <path>       Path to config. Default: ~/.jobctl.conf
    --password            Ask for a password before launching a command.
    --log-level <level>   'error', 'warn', 'info', 'debug' or 'trace'.
                          Default: warn
    --help:               Show this help.
    --version:            Print version. 
    
Configuration file    
    Example of possible ~/.jobctl.conf file:
    
    ;password = 
    hostname = 1.2.3.4
    port = 7080
    log_level = warn
    master = true
`

    console.log(s)
    process.exit(exitCode)
}

async function term() {
    if (logger)
        logger.info('shutdown')

    await loggerModule.shutdown()
    process.exit()
}

async function exists(file) {
    let exists
    try {
        await fs.stat(file)
        exists = true
    } catch (error) {
        exists = false
    }
    return exists
}

function table(columns, rows) {
    const maxColumnSize = {}
    for (const c of columns)
        maxColumnSize[c] = c.length

    rows = rows.map(values => {
        if (!Array.isArray(values))
            throw new Error('row must be array, got', values)

        let row = {}
        for (let i = 0; i < columns.length; i++) {
            let value = String(values[i])
            row[columns[i]] = value

            let width
            if (value.indexOf('\n') !== -1) {
                width = Math.max(...value.split('\n').map(s => s.length))
            } else {
                width = value.length
            }

            if (width > maxColumnSize[columns[i]])
                maxColumnSize[columns[i]] = width
        }

        return row
    })

    console.log(columnify(rows, {
        columns,
        preserveNewLines: true,
        columnSplitter: ' | ',
        headingTransform: (text) => {
            const repeat = () => '-'.repeat(maxColumnSize[text])
            return `${text.toUpperCase()}\n${repeat()}`
        }
    }))
}

/**
 * @param prompt
 * @return {Promise<string>}
 */
function question(prompt) {
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    })
    return new Promise((resolve, reject) => {
        rl.question(prompt, (answer) => {
            rl.close()
            resolve(answer)
        })
    })
}