From d522316ee2fefa9ca0a4311df2fd973f95d47cc2 Mon Sep 17 00:00:00 2001 From: James Barnsley Date: Mon, 30 Dec 2019 21:13:15 +1300 Subject: [PATCH] Adding async await where possible, need to revist system tasks --- mopidy_iris/core.py | 4 ++-- mopidy_iris/handlers.py | 17 +++++++++++++---- mopidy_iris/system.py | 38 +++++++++++++++++++++++++++----------- mopidy_iris/system.sh | 16 +++++++++------- 4 files changed, 51 insertions(+), 24 deletions(-) diff --git a/mopidy_iris/core.py b/mopidy_iris/core.py index 527a0ccb..d9b21602 100755 --- a/mopidy_iris/core.py +++ b/mopidy_iris/core.py @@ -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" diff --git a/mopidy_iris/handlers.py b/mopidy_iris/handlers.py index 58eeb9bf..821ca3c4 100755 --- a/mopidy_iris/handlers.py +++ b/mopidy_iris/handlers.py @@ -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"}) diff --git a/mopidy_iris/system.py b/mopidy_iris/system.py index 994f5250..9df4ea73 100755 --- a/mopidy_iris/system.py +++ b/mopidy_iris/system.py @@ -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) ## diff --git a/mopidy_iris/system.sh b/mopidy_iris/system.sh index 5ab92506..8efc6eee 100755 --- a/mopidy_iris/system.sh +++ b/mopidy_iris/system.sh @@ -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}"