Run scheduled scripts

This commit is contained in:
Magnus Åhall 2025-08-08 09:52:32 +02:00
parent 6d05152ab2
commit ef0a20ffe0
7 changed files with 294 additions and 28 deletions

View file

@ -1048,19 +1048,29 @@ class ScriptHooks extends Component {
renderHooks() {// {{{
this.scriptGrid.innerHTML = ''
let prevGroup = null
let curGroupName = null
let group = document.createElement('div')
group.classList.add('script-group')
for (const hook of this.hooks) {
if (hook.Script.Group !== prevGroup) {
if (hook.Script.Group !== curGroupName) {
const g = document.createElement('div')
g.classList.add('script-group')
g.classList.add('script-group-title')
g.innerText = hook.Script.Group
this.scriptGrid.append(g)
prevGroup = hook.Script.Group
group = document.createElement('div')
group.classList.add('script-group')
group.append(g)
this.scriptGrid.append(group)
curGroupName = hook.Script.Group
}
const h = new ScriptHook(hook, this)
this.scriptGrid.append(h.render())
group.append(h.render())
}
if (group.children.length > 1)
this.scriptGrid.append(group)
}// }}}
hookScript(script) {// {{{
_app.query(`/nodes/hook`, {
@ -1077,7 +1087,9 @@ class ScriptHook extends Component {
super()
this.hook = hook
this.parentList = parentList
this.element_ssh = null
this.elementSSH = null
this.elementSchedule = null
this.scheduleDisable = false
}// }}}
renderComponent() {// {{{
const tmpl = document.createElement('template')
@ -1085,14 +1097,15 @@ class ScriptHook extends Component {
<div class="script-name">${this.hook.Script.Name}</div>
<div class="script-ssh"></div>
<div class="script-unhook"><img src="/images/${_VERSION}/node_modules/@mdi/svg/svg/trash-can.svg" /></div>
<div class="script-run"><img src="/images/${_VERSION}/node_modules/@mdi/svg/svg/play-box.svg" /></div>
<div class="script-schedule"><img src="/images/${_VERSION}/node_modules/@mdi/svg/svg/play-box.svg" /></div>
`
this.element_ssh = tmpl.content.querySelector('.script-ssh')
this.element_ssh.innerText = this.hook.SSH
this.elementSchedule = tmpl.content.querySelector('.script-schedule')
this.elementSSH = tmpl.content.querySelector('.script-ssh')
this.elementSSH.innerText = `[ ${this.hook.SSH} ]`
tmpl.content.querySelector('.script-ssh').addEventListener('click', () => this.update())
tmpl.content.querySelector('.script-unhook').addEventListener('click', () => this.delete())
tmpl.content.querySelector('.script-run').addEventListener('click', () => this.run())
tmpl.content.querySelector('.script-schedule').addEventListener('click', () => this.run())
return tmpl.content
}// }}}
@ -1121,7 +1134,7 @@ class ScriptHook extends Component {
return
}
this.hook.SSH = ssh
this.element_ssh.innerText = this.hook.SSH
this.elementSSH.innerText = this.hook.SSH
})
.catch(err => showError(err))
}// }}}
@ -1141,8 +1154,22 @@ class ScriptHook extends Component {
.catch(err => showError(err))
}// }}}
run() {// {{{
if (this.scheduleDisable)
return
this.scheduleDisable = true
this.elementSchedule.classList.add('disabled')
const start = Date.now()
_app.query(`/hooks/schedule/${this.hook.ID}`)
.catch(err => showError(err))
.finally(() => {
const duration = Date.now() - start
setTimeout(() => {
this.scheduleDisable = false
this.elementSchedule.classList.remove('disabled')
}, 250 - duration)
})
}// }}}
}