Browse Source

Ability to run commands from the main process

develop
meriadec 6 years ago
parent
commit
afb8000eae
No known key found for this signature in database GPG Key ID: 1D2FC2305E2CB399
  1. 59
      src/internals/index.js
  2. 13
      src/main/bridge.js
  3. 48
      src/main/commandHandler.js

59
src/internals/index.js

@ -1,12 +1,12 @@
// @flow
import '@babel/polyfill'
import commands from 'commands'
import logger from 'logger'
import uuid from 'uuid/v4'
import { setImplementation } from 'api/network'
import sentry from 'sentry/node'
import { EXPERIMENTAL_HTTP_ON_RENDERER } from 'config/constants'
import { serializeError } from 'helpers/errors'
import { executeCommand, unsubscribeCommand } from 'main/commandHandler'
require('../env')
@ -52,64 +52,11 @@ if (EXPERIMENTAL_HTTP_ON_RENDERER) {
})
}
const subscriptions = {}
process.on('message', m => {
if (m.type === 'command') {
const { data, requestId, id } = m.command
const cmd = commands.find(cmd => cmd.id === id)
if (!cmd) {
logger.warn(`command ${id} not found`)
return
}
const startTime = Date.now()
logger.onCmd('cmd.START', id, 0, data)
try {
subscriptions[requestId] = cmd.impl(data).subscribe({
next: data => {
logger.onCmd('cmd.NEXT', id, Date.now() - startTime, data)
process.send({
type: 'cmd.NEXT',
requestId,
data,
})
},
complete: () => {
delete subscriptions[requestId]
logger.onCmd('cmd.COMPLETE', id, Date.now() - startTime)
process.send({
type: 'cmd.COMPLETE',
requestId,
})
},
error: error => {
logger.warn('Command error:', error)
delete subscriptions[requestId]
logger.onCmd('cmd.ERROR', id, Date.now() - startTime, error)
process.send({
type: 'cmd.ERROR',
requestId,
data: serializeError(error),
})
},
})
} catch (error) {
logger.warn('Command error:', error)
delete subscriptions[requestId]
logger.onCmd('cmd.ERROR', id, Date.now() - startTime, error)
process.send({
type: 'cmd.ERROR',
requestId,
data: serializeError(error),
})
}
executeCommand(m.command, process.send.bind(process))
} else if (m.type === 'command-unsubscribe') {
const { requestId } = m
const sub = subscriptions[requestId]
if (sub) {
sub.unsubscribe()
delete subscriptions[requestId]
}
unsubscribeCommand(m.requestId)
} else if (m.type === 'executeHttpQueryPayload') {
const { payload } = m
const defer = defers[payload.id]

13
src/main/bridge.js

@ -10,6 +10,7 @@ import logger from 'logger'
import sentry, { captureException } from 'sentry/node'
import user from 'helpers/user'
import { resolveLogsDirectory, cleanUpBeforeClosingSync } from 'helpers/log'
import { executeCommand, unsubscribeCommand } from 'main/commandHandler'
import { deserializeError } from 'helpers/errors'
import { setInternalProcessPID } from './terminator'
@ -77,10 +78,18 @@ ipcMain.on('clean-processes', () => {
ipcMainListenReceiveCommands({
onUnsubscribe: requestId => {
if (!internalProcess) return
internalProcess.send({ type: 'command-unsubscribe', requestId })
unsubscribeCommand(requestId)
if (internalProcess) {
internalProcess.send({ type: 'command-unsubscribe', requestId })
}
},
onCommand: (command, notifyCommandEvent) => {
// ability to run command from the main process
if (command.id.startsWith('main:')) {
executeCommand(command, notifyCommandEvent)
return
}
if (!internalProcess) bootInternalProcess()
const p = internalProcess
invariant(p, 'internalProcess not started !?')

48
src/main/commandHandler.js

@ -0,0 +1,48 @@
import commands from 'commands'
import logger from 'logger'
import { serializeError } from 'helpers/errors'
const subscriptions = {}
export function executeCommand(command, send) {
const { data, requestId, id } = command
const cmd = commands.find(cmd => cmd.id === id)
if (!cmd) {
logger.warn(`command ${id} not found`)
return
}
const startTime = Date.now()
logger.onCmd('cmd.START', id, 0, data)
try {
subscriptions[requestId] = cmd.impl(data).subscribe({
next: data => {
logger.onCmd('cmd.NEXT', id, Date.now() - startTime, data)
send({ type: 'cmd.NEXT', requestId, data })
},
complete: () => {
delete subscriptions[requestId]
logger.onCmd('cmd.COMPLETE', id, Date.now() - startTime)
send({ type: 'cmd.COMPLETE', requestId })
},
error: error => {
logger.warn('Command error:', error)
delete subscriptions[requestId]
logger.onCmd('cmd.ERROR', id, Date.now() - startTime, error)
send({ type: 'cmd.ERROR', requestId, data: serializeError(error) })
},
})
} catch (error) {
logger.warn('Command error:', error)
delete subscriptions[requestId]
logger.onCmd('cmd.ERROR', id, Date.now() - startTime, error)
send({ type: 'cmd.ERROR', requestId, data: serializeError(error) })
}
}
export function unsubscribeCommand(requestId) {
const sub = subscriptions[requestId]
if (sub) {
sub.unsubscribe()
delete subscriptions[requestId]
}
}
Loading…
Cancel
Save