Nope, threading was the best approach (reinstated)
This commit is contained in:
@ -452,58 +452,68 @@ class IrisCore(pykka.ThreadingActor):
|
|||||||
# Restart Mopidy
|
# Restart Mopidy
|
||||||
# This requires sudo access to system.sh
|
# This requires sudo access to system.sh
|
||||||
##
|
##
|
||||||
async def restart(self, *args, **kwargs):
|
def restart(self, *args, **kwargs):
|
||||||
callback = kwargs.get('callback', False)
|
callback = kwargs.get('callback', False)
|
||||||
|
|
||||||
|
# Trigger the action
|
||||||
|
IrisSystemThread('restart', self).start()
|
||||||
|
|
||||||
self.broadcast(data={
|
self.broadcast(data={
|
||||||
'method': "restart_started"
|
'method': "restart_started"
|
||||||
})
|
})
|
||||||
|
|
||||||
|
response = {
|
||||||
|
'message': "Restart started"
|
||||||
|
}
|
||||||
if (callback):
|
if (callback):
|
||||||
callback({
|
callback(response)
|
||||||
'message': "Restarting..."
|
else:
|
||||||
})
|
return response
|
||||||
|
|
||||||
task_response = await IrisSystemThread('restart').run()
|
def restart_callback(self, response, error):
|
||||||
|
if error:
|
||||||
if 'error' in task_response:
|
|
||||||
self.broadcast(data={
|
self.broadcast(data={
|
||||||
'method': "restart_error",
|
'method': "restart_error",
|
||||||
'params': task_response
|
'params': error
|
||||||
})
|
})
|
||||||
else:
|
else:
|
||||||
self.broadcast(data={
|
self.broadcast(data={
|
||||||
'method': "restart_finished",
|
'method': "restart_finished"
|
||||||
'params': task_response
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
##
|
##
|
||||||
# Run an upgrade of Iris
|
# Run an upgrade of Iris
|
||||||
##
|
##
|
||||||
async def upgrade(self, *args, **kwargs):
|
def upgrade(self, *args, **kwargs):
|
||||||
callback = kwargs.get('callback', False)
|
callback = kwargs.get('callback', False)
|
||||||
|
|
||||||
self.broadcast(data={
|
self.broadcast(data={
|
||||||
'method': "upgrade_started"
|
'method': "upgrade_started"
|
||||||
})
|
})
|
||||||
|
|
||||||
if (callback):
|
# Trigger the action
|
||||||
callback({
|
IrisSystemThread('upgrade', self).start()
|
||||||
'message': "Upgrade started"
|
|
||||||
})
|
|
||||||
|
|
||||||
task_response = await IrisSystemThread('upgrade').run()
|
|
||||||
|
|
||||||
if 'error' in task_response:
|
response = {
|
||||||
|
'message': "Upgrade started"
|
||||||
|
}
|
||||||
|
|
||||||
|
if (callback):
|
||||||
|
callback(response)
|
||||||
|
else:
|
||||||
|
return response
|
||||||
|
|
||||||
|
def upgrade_callback(self, response, error):
|
||||||
|
if error:
|
||||||
self.broadcast(data={
|
self.broadcast(data={
|
||||||
'method': "upgrade_error",
|
'method': "upgrade_error",
|
||||||
'params': task_response
|
'params': error
|
||||||
})
|
})
|
||||||
else:
|
else:
|
||||||
self.broadcast(data={
|
self.broadcast(data={
|
||||||
'method': "upgrade_finished",
|
'method': "upgrade_finished",
|
||||||
'params': task_response
|
'params': response
|
||||||
})
|
})
|
||||||
self.restart()
|
self.restart()
|
||||||
|
|
||||||
@ -512,29 +522,34 @@ class IrisCore(pykka.ThreadingActor):
|
|||||||
# Run a mopidy local scan
|
# Run a mopidy local scan
|
||||||
# Essetially an alias to "mopidyctl local scan"
|
# Essetially an alias to "mopidyctl local scan"
|
||||||
##
|
##
|
||||||
async def local_scan(self, *args, **kwargs):
|
def local_scan(self, *args, **kwargs):
|
||||||
callback = kwargs.get('callback', False)
|
callback = kwargs.get('callback', False)
|
||||||
|
|
||||||
|
# Trigger the action
|
||||||
|
IrisSystemThread('local_scan', self).start()
|
||||||
|
|
||||||
self.broadcast(data={
|
self.broadcast(data={
|
||||||
'method': "local_scan_started"
|
'method': "local_scan_started"
|
||||||
})
|
})
|
||||||
|
|
||||||
|
response = {
|
||||||
|
'message': "Local scan started"
|
||||||
|
}
|
||||||
if (callback):
|
if (callback):
|
||||||
callback({
|
callback(response)
|
||||||
'message': "Local scan started"
|
else:
|
||||||
})
|
return response
|
||||||
|
|
||||||
task_response = await IrisSystemThread('local_scan').run()
|
|
||||||
|
|
||||||
if 'error' in task_response:
|
def local_scan_callback(self, response, error):
|
||||||
|
if error:
|
||||||
self.broadcast(data={
|
self.broadcast(data={
|
||||||
'method': "local_scan_error",
|
'method': "local_scan_error",
|
||||||
'params': task_response
|
'params': error
|
||||||
})
|
})
|
||||||
else:
|
else:
|
||||||
self.broadcast(data={
|
self.broadcast(data={
|
||||||
'method': "local_scan_finished",
|
'method': "local_scan_finished",
|
||||||
'params': task_response
|
'params': response
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
@ -1070,7 +1085,7 @@ class IrisCore(pykka.ThreadingActor):
|
|||||||
##
|
##
|
||||||
# Simple test method. Not for use in production for any purposes.
|
# Simple test method. Not for use in production for any purposes.
|
||||||
##
|
##
|
||||||
async def test(self, *args, **kwargs):
|
def test(self, *args, **kwargs):
|
||||||
callback = kwargs.get('callback', False)
|
callback = kwargs.get('callback', False)
|
||||||
|
|
||||||
self.broadcast(data={
|
self.broadcast(data={
|
||||||
@ -1082,15 +1097,16 @@ class IrisCore(pykka.ThreadingActor):
|
|||||||
'message': "Running test... please wait"
|
'message': "Running test... please wait"
|
||||||
})
|
})
|
||||||
|
|
||||||
task_response = await IrisSystemThread('test').run()
|
IrisSystemThread('test', self).run()
|
||||||
|
|
||||||
if 'error' in task_response:
|
def test_callback(self, response, error):
|
||||||
|
if error:
|
||||||
self.broadcast(data={
|
self.broadcast(data={
|
||||||
'method': "test_error",
|
'method': "test_error",
|
||||||
'params': task_response
|
'params': error
|
||||||
})
|
})
|
||||||
else:
|
else:
|
||||||
self.broadcast(data={
|
self.broadcast(data={
|
||||||
'method': "test_finished",
|
'method': "test_finished",
|
||||||
'params': task_response
|
'params': response
|
||||||
})
|
})
|
||||||
|
|||||||
@ -1,20 +1,23 @@
|
|||||||
|
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
import os, logging, subprocess, json, asyncio
|
import os, logging, subprocess, json
|
||||||
|
|
||||||
# import logger
|
# import logger
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class IrisSystemThread:
|
class IrisSystemThread(Thread):
|
||||||
def __init__(self, action):
|
def __init__(self, action, origin):
|
||||||
|
Thread.__init__(self)
|
||||||
self.action = action
|
self.action = action
|
||||||
|
self.origin = origin
|
||||||
self.path = os.path.dirname(__file__)
|
self.path = os.path.dirname(__file__)
|
||||||
|
|
||||||
##
|
##
|
||||||
# Run the defined action
|
# Run the defined action
|
||||||
##
|
##
|
||||||
async def run(self):
|
def run(self):
|
||||||
logger.info("Running system action '"+self.action+"'")
|
logger.info("Running system action '"+self.action+"'")
|
||||||
|
callback_name = self.action+"_callback"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.can_run()
|
self.can_run()
|
||||||
@ -40,14 +43,21 @@ class IrisSystemThread:
|
|||||||
|
|
||||||
if stderr:
|
if stderr:
|
||||||
logger.error(stderr.decode())
|
logger.error(stderr.decode())
|
||||||
return {
|
getattr(self.origin, callback_name)(
|
||||||
'error': stderr.decode()
|
None,
|
||||||
}
|
{
|
||||||
|
'error': stderr.decode()
|
||||||
|
}
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
logger.info(stdout.decode())
|
logger.info(stdout.decode())
|
||||||
return {
|
getattr(self.origin, callback_name)(
|
||||||
'output': stdout.decode()
|
{
|
||||||
}
|
'output': stdout.decode()
|
||||||
|
},
|
||||||
|
None
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
##
|
##
|
||||||
|
|||||||
@ -24,7 +24,7 @@ elif [[ $1 = "restart" ]]; then
|
|||||||
elif [[ $1 = "local_scan" ]]; then
|
elif [[ $1 = "local_scan" ]]; then
|
||||||
|
|
||||||
if [[ $IS_CONTAINER ]]; then
|
if [[ $IS_CONTAINER ]]; then
|
||||||
SCAN="$(sudo -u mopidy mopidy local scan)"
|
SCAN="$(mopidy --config /config/mopidy.conf local scan)"
|
||||||
else
|
else
|
||||||
SCAN="$(sudo mopidyctl local scan)"
|
SCAN="$(sudo mopidyctl local scan)"
|
||||||
fi
|
fi
|
||||||
@ -32,9 +32,9 @@ elif [[ $1 = "local_scan" ]]; then
|
|||||||
|
|
||||||
elif [[ $1 = "test" ]]; then
|
elif [[ $1 = "test" ]]; then
|
||||||
|
|
||||||
sleep 5
|
sleep 3
|
||||||
|
|
||||||
TEST="$(echo 'Hello, this is your bash speaking. I was sleeping for 5 seconds.')"
|
TEST="$(echo 'Hello, this is your bash speaking. I was sleeping for 3 seconds.')"
|
||||||
echo -e "${TEST}"
|
echo -e "${TEST}"
|
||||||
|
|
||||||
else
|
else
|
||||||
|
|||||||
Reference in New Issue
Block a user