Stop a service when addon needs updated
#1
I have a simple service that acts as a web server:
python:
# -*- coding: utf-8 -*-
# Module: default
# Author: TheDiamondPicks / RedactedRetracted
# Created on: 30.12.2017
# License: GPL v.2 https://www.gnu.org/licenses/old-license....0.en.html

import SimpleHTTPServer
import SocketServer
import xbmcaddon

import os

PORT = 5123

addon = xbmcaddon.Addon()
os.chdir(os.path.join(addon.getAddonInfo('path').decode('utf-8'), "resources", "web"))

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler

httpd = SocketServer.TCPServer(("", PORT), Handler)

print "Serving at port", PORT
httpd.serve_forever()

However, when I try to update the addon via a zip file the addon fails to install, as the service python file is in use. How can I interrupt the execution of the script, so that the addon could be updated?
Reply
#2
Use Kodi monitor loop to poll for an abort request then close your webserver connect.
Pseudocode

python:
While not xbmc.Monitor(). abortrequested ();
xbmc.Monitor().wait for abort(2)
Webbserver.close()
Image Lunatixz - Kodi / Beta repository
Image PseudoTV - Forum | Website | Youtube | Help?
Reply
#3
(2018-01-21, 08:19)Lunatixz Wrote: Use Kodi monitor loop to poll for an abort request then close your webserver connect.
Pseudocode

python:
While not xbmc.Monitor(). abortrequested ();
xbmc.Monitor().wait for abort(2)
Webbserver.close()
 The problem is that no code is executed after httpd.serve_forever(), so I couldn't put the while loop there, and I couldn't put it before httpd.serve_forever(), as the serve command would never be issued. Would I have to use a seperate file to house the abort code, or is there something else I can do?
Reply
#4
python:

monitor = xbmc.Monitor()
httpd.timeout = 0.1
while not monitor.abortRequested():
    httpd.handle_request()
httpd.socket.close()
Reply
#5
Hmm, doesn't seem to work correctly for me. I had updated my code to this:
python:
...
print "Serving at port", PORT
monitor = xbmc.Monitor()
httpd.timeout = 0.1
while not monitor.abortRequested():
      httpd.handle_request()
print("Web server aborted")
httpd.socket.close()

However, when I attempt to update the addon via zip file it doesn't close the web server, and 'Web server aborted' isn't printed. However when Kodi closes it is printed. Is an abort only requested when Kodi is closing, or is it also requested whenever an addon needs upgraded, uninstalled etc.
Reply
#6
Bump. Still unsure about this, does anyone have any ideas?
Reply
#7
Make sure you are starting the service during login, via addon.xml
python:
<extension point="xbmc.service" library="service.py" start="login"/>

If you want deeper troubleshooting you'll have to start providing logs.
Image Lunatixz - Kodi / Beta repository
Image PseudoTV - Forum | Website | Youtube | Help?
Reply
#8
you cant use serve_forever() you need to use httpd.handle_request() in a loop.

see bottom of page
https://docs.python.org/2/library/basehttpserver.html

But also you need to call your service one last time to let it exit the handle_request() blocking call.

Ie.
in another thread monitor for exit request and if exit requested set the stop flag in your http service and then call your "localhost" http service will a dummy call. This will allow it to exit. I have used this approach in the past.
Reply

Logout Mark Read Team Forum Stats Members Help
Stop a service when addon needs updated0