Now handling Pulse restarts manually and exclusively from Pulse Process.

parent fd0ff12a
......@@ -63,8 +63,7 @@ xml2js = require 'xml2js'
XMLParser = xml2js.Parser
XMLBuilder = xml2js.Builder
fs = require 'fs-extra'
fs = Promise.promisifyAll(fs)
fs = Promise.promisifyAll (require 'fs-extra')
# fs.exists cannot be automatically promisified
fs.existsAsync = (filePath)->
return new Promise ((fulfill, reject) ->
......@@ -389,6 +388,8 @@ class PulseProcess
@pulseProcess: null
explicitlyAskedToKillPulseProcess: false
#
# Pulse Process Delegate interface.
# (All delegate methods are optional.)
......@@ -415,7 +416,27 @@ class PulseProcess
# The version of Pulse that is currently supported.
version: '1.0.0+14-gd2cd1f4'
# Private singleton reference
instance = null
constructor: (delegate={}, homeDirectory='') ->
if instance
console.log 'ℹ️ Pulse Process: returning singleton reference.'
instance.delegate = delegate
# (The homeDirectory should never be changed)
return instance
instance = @
# Initialise it.
@_init delegate, homeDirectory
return instance
_init: (delegate={}, homeDirectory='') ->
console.log 'ℹ️ Initiliasing Pulse Process for the first time.'
# Sanity check: make sure that the version exists
assert(@version, 'Version must exist.')
......@@ -461,6 +482,7 @@ class PulseProcess
PulseConfig.getApiKey().then (apiKey) =>
@pulseAPIKey = apiKey
# TODO: Do this internally via API.
command = "curl -X POST --header \"X-API-Key: #{@pulseAPIKey}\" http://127.0.0.1:8080/rest/shutdown"
console.log "Command: #{command}"
......@@ -473,7 +495,13 @@ class PulseProcess
# OK, Now let’s start.
@_start generate, name
else
#
# Generating, just start (as there’s no configuration to read so we can’t find the REST API key.)
#
# Generate exists the process, don’t attempt to restart when it does.
@explicitlyAskedToKillPulseProcess = true
@_start generate, name
......@@ -525,15 +553,16 @@ class PulseProcess
# This might be making Pulse brittle after putting computer to sleep.
# Commenting out to test.
#
# env = process.env
# env['STNORESTART'] = 1
env = process.env
env['STNORESTART'] = 1
# env['STTRACE'] = 'mc,discover'
#options =
# 'env': env
options =
'env': env
log.info "About to start Pulse version #{@version} at home directory: #{@homeDirectory} with arguments: #{pulseArguments}"
@pulseProcess = spawn binaryFilePath, pulseArguments #, options
@pulseProcess = spawn binaryFilePath, pulseArguments, options
@pulseProcess.stdout.on 'data', (data) =>
#
......@@ -576,14 +605,20 @@ class PulseProcess
@pulseProcess.on 'close', (code) =>
log.info '🌏👋 Exited with code ' + code
if @delegate.pulseProcessDidExitWithCode != undefined
@delegate.pulseProcessDidExitWithCode code
if @explicitlyAskedToKillPulseProcess
@explicitlyAskedToKillPulseProcess = false
if @delegate.pulseProcessDidExitWithCode != undefined
@delegate.pulseProcessDidExitWithCode code
else
log.info '🌏 Pulse process died… restarting!'
@_start false, 'Restarted Pulse process'
stop: =>
#
# Stop the Pulse process.
#
log.info '🌏 Killing Pulse process…'
@explicitlyAskedToKillPulseProcess = true
@pulseProcess?.kill 'SIGTERM'
@pulseProcess = null
......@@ -732,7 +767,7 @@ class PulseConfig
console.log "✅ Pulse instance successfully generated. Device ID: #{deviceID}"
# Stop the Pulse process
@pulseProcess.stop()
# @pulseProcess.stop()
return deviceID
)
......@@ -1069,6 +1104,8 @@ class PulseConfig
@startPulseAPI()
.then (=>
console.log "Pulse API process started, about to update configuration with the folder paths."
# Save only the paths that should be added to Pulse at initialisation.
@initialFolderPaths = [
@privateFolder,
......@@ -1093,7 +1130,24 @@ class PulseConfig
.catch ((e) ->
console.log '❌ Error while creating initial Pulse directories and configuration: '
console.log e
return false
#
# We failed, revert the changes.
#
console.log 'Reverting the folders.'
# Remove all the subfolders but recreate the Sync folder itself.
fs.removeAsync @pulseSyncDirectory
.then =>
fs.mkdirAsync @pulseSyncDirectory
.then =>
console.log 'Reverted changes.'
return false
.catch (error) =>
console.log "❌ Error while attempting to revert initialisePulse(). Could not recreate the Pulse Sync folder. #{error}"
return false
.catch (error) =>
console.log "❌ Error while attempting to revert initialisePulse(). Could not delete recursively the Pulse Sync folder. #{error}"
return false
)
......@@ -1237,12 +1291,13 @@ class PulseConfig
new Promise((resolve, reject)=>
# console.log 'Promising to start the Pulse API…'
console.log 'Promising to start the Pulse API…'
pulseProcessDelegate =
pulseProcessRestApiIsReady: =>
# console.log 'Pulse API started.'
console.log 'Pulse API started.'
resolve()
pulseProcessFailedToStartProcess: =>
console.log 'Pulse API failed to start.'
reject()
@pulseAPIProcess = new PulseProcess(pulseProcessDelegate, @homeDirectory)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment