Adding async await where possible, need to revist system tasks

This commit is contained in:
James Barnsley
2019-12-30 21:13:15 +13:00
parent 6d72047106
commit d522316ee2
4 changed files with 51 additions and 24 deletions

View File

@ -522,11 +522,11 @@ class IrisCore(pykka.ThreadingActor):
# Run a mopidy local scan
# Essetially an alias to "mopidyctl local scan"
##
def local_scan(self, *args, **kwargs):
async def local_scan(self, *args, **kwargs):
callback = kwargs.get('callback', False)
# Trigger the action
IrisSystemThread('local_scan', self.local_scan_callback).start()
await IrisSystemThread('local_scan', self.local_scan_callback).start()
self.broadcast(data={
'method': "local_scan_started"

View File

@ -3,7 +3,7 @@ from __future__ import unicode_literals
from datetime import datetime
from tornado.escape import json_encode, json_decode
import tornado.ioloop, tornado.web, tornado.websocket, tornado.template
import random, string, logging, uuid, subprocess, pykka, ast, logging, json, urllib, requests, time
import random, string, logging, uuid, subprocess, pykka, ast, logging, json, urllib, requests, time, asyncio
from .mem import iris
@ -67,7 +67,10 @@ class WebsocketHandler(tornado.websocket.WebSocketHandler):
# make sure the method exists
if hasattr(iris, message['method']):
try:
getattr(iris, message['method'])(data=params, callback=lambda response, error=False: self.handle_result(id=id, method=message['method'], response=response, error=error))
if asyncio.iscoroutinefunction(getattr(iris, message['method'])):
await getattr(iris, message['method'])(data=params, callback=lambda response, error=False: self.handle_result(id=id, method=message['method'], response=response, error=error))
else:
getattr(iris, message['method'])(data=params, callback=lambda response, error=False: self.handle_result(id=id, method=message['method'], response=response, error=error))
except Exception as e:
logger.error(str(e))
@ -144,7 +147,10 @@ class HttpHandler(tornado.web.RequestHandler):
# make sure the method exists
if hasattr(iris, slug):
try:
await getattr(iris, slug)(request=self, callback=lambda response, error=False: self.handle_result(id=id, method=slug, response=response, error=error))
if asyncio.iscoroutinefunction(getattr(iris, slug)):
await getattr(iris, slug)(request=self, callback=lambda response, error=False: self.handle_result(id=id, method=slug, response=response, error=error))
else:
getattr(iris, slug)(request=self, callback=lambda response, error=False: self.handle_result(id=id, method=slug, response=response, error=error))
except Exception as e:
logger.error(str(e))
@ -165,7 +171,10 @@ class HttpHandler(tornado.web.RequestHandler):
# make sure the method exists
if hasattr(iris, slug):
try:
await getattr(iris, slug)(data=params, request=self.request, callback=lambda response=False, error=False: self.handle_result(id=id, method=slug, response=response, error=error))
if asyncio.iscoroutinefunction(getattr(iris, slug)):
await getattr(iris, slug)(data=params, request=self.request, callback=lambda response=False, error=False: self.handle_result(id=id, method=slug, response=response, error=error))
else:
getattr(iris, slug)(data=params, request=self.request, callback=lambda response=False, error=False: self.handle_result(id=id, method=slug, response=response, error=error))
except HTTPError as e:
self.handle_result(id=id, error={'code': 32601, 'message': "Invalid JSON payload"})

View File

@ -1,6 +1,6 @@
from threading import Thread
import os, logging, subprocess
import os, logging, subprocess, json
# import logger
logger = logging.getLogger(__name__)
@ -15,8 +15,8 @@ class IrisSystemThread(Thread):
##
# Run the defined action
##
def run(self):
logger.info("Running system action: "+self.action)
async def run(self):
logger.info("Running system action '"+self.action+"'")
try:
self.can_run()
@ -32,15 +32,31 @@ class IrisSystemThread(Thread):
self.callback(False, error)
return
# Run the actual task (this is the process-blocking instruction)
output = subprocess.check_output(["sudo "+self.path+"/system.sh "+self.action], shell=True)
# Create subprocess
process = await asyncio.create_subprocess_shell(
self.path+"/system.sh "+self.action,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
# And then, when complete, return to our callback
if self.callback:
response = {
'output': output
}
self.callback(response, False)
# Status
print("Started:", command, "(pid = " + str(process.pid) + ")", flush=True)
# Wait for the subprocess to finish
stdout, stderr = await process.communicate()
logger.debug("System action '"+self.action+"' completed with output:")
logger.debug(stdout)
# Some kind of failure, so we can't run any commands this way
if exitCode > 0:
raise Exception("System task '"+self.action+"' failed")
else:
if self.callback:
response = {
'output': str(stdout)
}
self.callback(response, False)
##

View File

@ -1,9 +1,11 @@
#!/bin/bash
echo -e "Running $1 yeah nah"
if [[ "$(pwd)" = "/iris/mopidy_iris/system.sh" ]]; then
IS_CONTAINER = true
IS_CONTAINER=true
else
IS_CONTAINER = false
IS_CONTAINER=false
fi
if [[ $1 = "upgrade" ]]; then
@ -11,22 +13,22 @@ if [[ $1 = "upgrade" ]]; then
echo "cd /iris && git checkout master && git pull origin master"
UPGRADE="$(cd /iris && git checkout master && git pull origin master)"
else
echo "pip install --upgrade mopidy-iris"
UPGRADE="$(pip install --upgrade mopidy-iris)"
echo "sudo pip install --upgrade mopidy-iris"
UPGRADE="$(sudo pip install --upgrade mopidy-iris)"
fi
echo -e "${UPGRADE}"
elif [[ $1 = "restart" ]]; then
RESTART="$(service mopidy restart)"
RESTART="$(sudo service mopidy restart)"
echo -e "${RESTART}"
elif [[ $1 = "local_scan" ]]; then
if [[ $IS_CONTAINER ]]; then
SCAN="$(-u mopidy mopidy local scan)"
SCAN="$(sudo -u mopidy mopidy local scan)"
else
SCAN="$(mopidyctl local scan)"
SCAN="$(sudo mopidyctl local scan)"
fi
echo -e "${SCAN}"