Skip to content

Commit

Permalink
Merge pull request #21 from NASA-AMMOS/duplicate-script-exec-debug
Browse files Browse the repository at this point in the history
Issue #20 - Fix duplicate script execution
  • Loading branch information
MJJoyce authored Apr 21, 2018
2 parents b73ce37 + 2a284a7 commit 87158cd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
24 changes: 18 additions & 6 deletions bliss/gui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,14 +878,22 @@ def handle():
:statuscode 400: When the script name cannot be located
"""
global _RUNNING_SCRIPT
with Sessions.current() as session:
script_name = bottle.request.forms.get('scriptPath')
script_path = os.path.join(ScriptRoot, script_name)

if not os.path.exists(script_path):
bottle.abort(400, "Script cannot be located")
if _RUNNING_SCRIPT is None:
with Sessions.current() as session:
script_name = bottle.request.forms.get('scriptPath')
script_path = os.path.join(ScriptRoot, script_name)

_RUNNING_SCRIPT = gevent.spawn(bgExecScript, script_path)
if not os.path.exists(script_path):
bottle.abort(400, "Script cannot be located")

_RUNNING_SCRIPT = gevent.spawn(bgExecScript, script_path)
else:
msg = (
'Attempted to execute script while another script is running. '
'Please wait until the previous script completes and try again'
)
log.warn(msg)


@App.route('/script/run', method='PUT')
Expand Down Expand Up @@ -926,6 +934,8 @@ def handle():


def bgExecScript(script_path):
global _RUNNING_SCRIPT

debugger = BlissDB()
with open(script_path) as infile:
script = infile.read()
Expand All @@ -940,6 +950,8 @@ def bgExecScript(script_path):
e
))
Sessions.addEvent('script:error', str(e))
finally:
_RUNNING_SCRIPT = None


class BlissDB(bdb.Bdb):
Expand Down
21 changes: 21 additions & 0 deletions bliss/gui/static/js/bliss/gui/Script.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,24 @@ const ScriptLoadModal = {
const ScriptExecCtrl = {
oninit(vnode) {
this._script_state = vnode.attrs.scriptState
this._script_exec_triggered = false
},

view(vnode) {
this._script_state = vnode.attrs.scriptState

// We need to track whether we have internally triggered a script
// execution and compare it against the "global" execution state
// to ensure that we're preventing functionality from being
// triggered multiple times. Most importantly, we need this to
// ensure that a script cannot be triggered twice via double
// clicking the execution button
//
// If the global state indicates that a script is running we know
// that we can safely say that the execution has propagated and
// we can stop preventing functionality.
if (this._script_state === 'running') this._script_exec_triggered = false

// Invert the script execution state to give us the button display
// classes / states
const btnDisplayState = vnode.attrs.scriptState === 'running' ? 'pause' : 'play'
Expand All @@ -233,13 +246,21 @@ const ScriptExecCtrl = {
url: '/script/run'
})
} else {
if (this._script_exec_triggered) return

if (vnode.attrs.ScriptSelectionData.selected !== null) {
this._script_exec_triggered = true
let data = new FormData()
data.append('scriptPath', vnode.attrs.ScriptSelectionData.selected)
m.request({
method: 'POST',
url: '/script/run',
data: data
}).catch(() => {
// If something failed when we were trying to run the script we
// need to update internal logic so we're not out of sync. E.g.,
// the script might have not been loaded so nothing executed.
this._script_exec_triggered = false
})
}
}
Expand Down

0 comments on commit 87158cd

Please sign in to comment.