Skip to content

Commit

Permalink
feat: add bee lifecycle management to tray
Browse files Browse the repository at this point in the history
  • Loading branch information
Cafe137 committed Mar 7, 2022
1 parent 7f0a27b commit 45a4648
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 12 deletions.
2 changes: 0 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const { runLauncher } = require('./src/launcher')
const { runElectronTray } = require('./src/electron')
const { runStaticServer } = require('./src/static-server')

runLauncher()
runElectronTray()
runStaticServer()
37 changes: 32 additions & 5 deletions src/electron.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,44 @@
const { app, Tray, Menu, shell } = require('electron')
const { runLauncher } = require('./launcher')
const { BeeManager } = require('./lifecycle')

let tray

function rebuildElectronTray() {
const contextMenu = Menu.buildFromTemplate([
{
label: BeeManager.isRunning() ? 'Stop Bee' : 'Start Bee',
click: () => {
if (BeeManager.isRunning()) {
BeeManager.stop()
} else {
runLauncher()
}
}
},
{ type: 'separator' },
{ label: 'Open Web UI', click: () => shell.openExternal('http://localhost:5000') },
{ type: 'separator' },
{
label: 'Exit',
click: async () => {
BeeManager.stop()
await BeeManager.waitForSigtermToFinish()
app.quit()
}
}
])
tray.setContextMenu(contextMenu)
}

function main() {
app.whenReady().then(() => {
tray = new Tray('tray.png')
const contextMenu = Menu.buildFromTemplate([
{ label: 'Open Web UI', click: () => shell.openExternal('http://localhost:5000') }
])
tray.setContextMenu(contextMenu)
rebuildElectronTray()
})
}

module.exports = {
runElectronTray: main
runElectronTray: main,
rebuildElectronTray
}
25 changes: 20 additions & 5 deletions src/launcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ const { existsSync, readFileSync, writeFileSync, mkdirSync } = require('fs')
const { exit } = require('process')
const { resolve } = require('path')
const { spawn } = require('child_process')
const { BeeManager } = require('./lifecycle')

async function main() {
const { rebuildElectronTray } = require('./electron')
const abortController = new AbortController()
if (!existsSync('bee')) {
console.error(`Please compile bee and place it as follows: ${resolve('bee')}`)
exit(1)
Expand All @@ -25,7 +28,16 @@ async function main() {
const { transaction, blockHash } = await sendTransaction(address)
writeFileSync('config.yaml', createConfiguration(transaction, blockHash))
}
launchBee()
const subprocess = launchBee(abortController).catch(reason => {
console.error(reason)
})
BeeManager.signalRunning(abortController, subprocess)
rebuildElectronTray()
await subprocess
console.log('Bee subprocess finished running')
abortController.abort()
BeeManager.signalStopped()
rebuildElectronTray()
}

async function sendTransaction(address) {
Expand Down Expand Up @@ -54,9 +66,9 @@ transaction: ${transaction}
block-hash: ${blockHash}`
}

async function launchBee() {
async function launchBee(abortController) {
const configPath = resolve('config.yaml')
return runProcess(resolve('bee'), ['start', `--config=${configPath}`], onStdout, onStderr)
return runProcess(resolve('bee'), ['start', `--config=${configPath}`], onStdout, onStderr, abortController)
}

function onStdout(data) {
Expand All @@ -67,9 +79,9 @@ function onStderr(data) {
process.stderr.write(data)
}

async function runProcess(command, args, onStdout, onStderr) {
async function runProcess(command, args, onStdout, onStderr, abortController) {
return new Promise((resolve, reject) => {
const subprocess = spawn(command, args)
const subprocess = spawn(command, args, { signal: abortController.signal, killSignal: 'SIGINT' })
subprocess.stdout.on('data', onStdout)
subprocess.stderr.on('data', onStderr)
subprocess.on('close', code => {
Expand All @@ -79,6 +91,9 @@ async function runProcess(command, args, onStdout, onStderr) {
reject(code)
}
})
subprocess.on('error', error => {
reject(error)
})
})
}

Expand Down
31 changes: 31 additions & 0 deletions src/lifecycle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const state = {
process: null,
running: false,
abortController: null
}

const BeeManager = {
signalRunning: (abortController, process) => {
state.abortController = abortController
state.process = process
state.running = true
},
signalStopped: () => {
state.running = false
},
isRunning: () => state.running || (state.abortController && !state.abortController.signal.aborted),
stop: () => {
if (state.abortController) {
state.abortController.abort()
}
},
waitForSigtermToFinish: async () => {
if (state.process) {
await state.process
}
}
}

module.exports = {
BeeManager
}

0 comments on commit 45a4648

Please sign in to comment.