Skip to content
This repository has been archived by the owner on Sep 8, 2024. It is now read-only.

Service hooks #2601

Merged
merged 4 commits into from
Jul 27, 2020
Merged

Service hooks #2601

merged 4 commits into from
Jul 27, 2020

Conversation

forslund
Copy link
Collaborator

@forslund forslund commented Jun 1, 2020

Description

This adds a simple system of callback hooks to run mycroft as a service or through a single executable. It allows the definition of a wrapper script with handlers for ready states, error states and a watchdog to ensure the system is still running.

Example wrapper script for use with systemd:

import sdnotify

from mycroft.skills.__main__ import main


n = sdnotify.SystemdNotifier()

def on_ready():
    n.notify("READY=1")

def on_stopped():
    n.notify("READY=0")

main(ready_hook=on_ready, stop_hook=on_stopped)

The current watchdog implementations are currently only for the speech client (voice data is received from the mic) and the skills process (skills are updated) due to their natural cyclic structure.

The reason for doing this is to be able to keep startup system requirements outside of core and allowing flexibility. For example these startup hooks can be done for a desktop launcher just as well as for a system service like systemd or finit.

How to test

Create a similar wrapper but instead of sdnotify a simple custom print statement is used in the hooks.

Contributor license agreement signed?

CLA [ Yes ]

@devs-mycroft devs-mycroft added the CLA: Yes Contributor License Agreement exists (see https://github.com/MycroftAI/contributors) label Jun 1, 2020
@devops-mycroft
Copy link

Voight Kampff Integration Test Failed (Results)

@pep8speaks
Copy link

pep8speaks commented Jun 1, 2020

Hello @forslund! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2020-07-24 20:15:47 UTC

@devops-mycroft
Copy link

Voight Kampff Integration Test Failed (Results)

@j1nx
Copy link

j1nx commented Jun 2, 2020

Perfect!

I have created systemd files that make use of this wrapper. You can use and test it wih those files;

https://github.com/j1nx/mycroft-systemd

@j1nx
Copy link

j1nx commented Jun 6, 2020

Will watchdog support be added?

@forslund
Copy link
Collaborator Author

forslund commented Jun 7, 2020

The skills service and the voice client has watchdogs since those have cyclic sections easily checked for activity, the other services could also get watchdog support in the future but some mechanism would need to be created to make those reliable.

I haven't been able to figure out why the integration tests are failing on Jenkins. Trying to reproduce the issue but haven't succeeded yet. After that the PR might be considered for inclusion.

@devops-mycroft
Copy link

Voight Kampff Integration Test Failed (Results)

@devops-mycroft
Copy link

Voight Kampff Integration Test Failed (Results)

@devops-mycroft
Copy link

Voight Kampff Integration Test Failed (Results)

Copy link
Contributor

@krisgesling krisgesling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, just one critical typo and a possible addition. Otherwise looks great -should be really useful!

mycroft/client/enclosure/__main__.py Outdated Show resolved Hide resolved
mycroft/audio/__main__.py Outdated Show resolved Hide resolved
@forslund
Copy link
Collaborator Author

That typo explains the vk issues facepalm. I had some reason for leaving out the stopping, but I can't remember it so I'll add one for completeness. Will do this as soon as I get the opportunity.

- skills
- audio
- speech client
- messagebus service
- enclosure

The main functions now accepts the arguments ready_hook and error_hook
allowing a service or runner script to catch these states and perform
actions accordingly.

This is useful for things like systemd or a desktop launcher.

Fix audio service startup
If no watchdog is provided a dummy function will be called
@forslund
Copy link
Collaborator Author

PR updated and rebased according to comments, hope VK is happy now!

(Sent from a camping chair at N59° 31,487' E015° 02,690')

@devops-mycroft
Copy link

Voight Kampff Integration Test Succeeded (Results)

@j1nx
Copy link

j1nx commented Jul 22, 2020

Nice progress!

Still hope Watchdog finds it way into the other services as well. Especially the messagebus as everything relies on that service to be available.

@forslund
Copy link
Collaborator Author

forslund commented Jul 22, 2020

I will work on adding that in a followup PR, gonna dig into tornado and the ws library and see if it can be achieved in some way...if not perhaps a cyclic message from the messahebus service triggering the wd in the respective clients is a way to add some additional reliability

@j1nx
Copy link

j1nx commented Jul 22, 2020

I remember you where not very keen on adding a cyclic function into mycroft-core, so if the messagebus doesn't have something similar as the voice and skill service, perhaps the other way around?

Send a message to the messagebus from the wrapper and if a proper reply is received, petting the dog...

@krisgesling
Copy link
Contributor

I think we'll need to look further into the Audio Service. It's reporting ready before loading any of the services:

2020-07-24 14:52:16.226 | INFO     |  8905 | __main__:<module>:68 | __main__
2020-07-24 14:52:16.227 | INFO     |  8905 | mycroft.messagebus.load_config:load_message_bus_config:33 | Loading message bus configs
2020-07-24 14:52:16.230 | INFO     |  8905 | mycroft.tts.mimic2_tts:__init__:176 | Getting Pre-loaded cache
2020-07-24 14:52:16.236 | INFO     |  8905 | mycroft.tts.mimic2_tts:__init__:178 | Successfully downloaded Pre-loaded cache
2020-07-24 14:52:16.236 | INFO     |  8905 | __main__:main:50 | Starting Audio Services
2020-07-24 14:52:16.237 | INFO     |  8905 | __main__:on_ready:30 | Audio service is ready.
2020-07-24 14:52:16.238 | INFO     |  8905 | mycroft.messagebus.client.client:on_open:114 | Connected
2020-07-24 14:52:16.238 | INFO     |  8905 | mycroft.audio.audioservice:get_services:56 | Loading services from /home/gez/mycroft-core/mycroft/audio/services/
2020-07-24 14:52:16.239 | INFO     |  8905 | mycroft.audio.audioservice:load_services:100 | Loading chromecast
2020-07-24 14:52:21.325 | INFO     |  8905 | mycroft.audio.audioservice:load_services:100 | Loading mopidy
2020-07-24 14:52:21.326 | INFO     |  8905 | mycroft.audio.audioservice:load_services:100 | Loading mplayer
2020-07-24 14:52:21.331 | ERROR    |  8905 | mplayer__init__:<module>:20 | install py_mplayer with pip install git+https://github.com/JarbasAl/py_mplayer
2020-07-24 14:52:21.331 | ERROR    |  8905 | mycroft.audio.audioservice:load_services:106 | Failed to import module mplayer
ModuleNotFoundError("No module named 'py_mplayer'",)
2020-07-24 14:52:21.331 | INFO     |  8905 | mycroft.audio.audioservice:load_services:100 | Loading simple
2020-07-24 14:52:21.333 | INFO     |  8905 | mycroft.audio.audioservice:load_services:100 | Loading vlc
2020-07-24 14:52:21.367 | INFO     |  8905 | mycroft.audio.audioservice:load_services_callback:168 | Finding default backend...
2020-07-24 14:52:21.368 | INFO     |  8905 | mycroft.audio.audioservice:load_services_callback:172 | Found local

My interpretation of ready would be that at least the default service is loaded and can output sound.

The others seem fine.

@krisgesling
Copy link
Contributor

I meant to say, don't think my previous comment needs to be addressed in this PR.

@forslund
Copy link
Collaborator Author

I can fix that quickly, the audio service loading is done in the background I'll add a status allowing the main function to check if loading has completed.

@j1nx
Copy link

j1nx commented Jul 24, 2020

Gez also meant that the messgaebus watchdog don't need to be addressed in this PR😜😂

The new wait_for_loaded() method will wait until services has been
loaded. It has a 3 minute timeout (default)

This improves the audio service readiness indication to not trigger
until the services are loaded.
@forslund
Copy link
Collaborator Author

Updated PR with a commit adding functionality to the audioservice for checking load status and only calling the ready_hook after waiting for services to be loaded.

@devops-mycroft
Copy link

Voight Kampff Integration Test Succeeded (Results)

@krisgesling
Copy link
Contributor

Yeah, this is great!

Thanks again - merging!

@krisgesling krisgesling merged commit 273da88 into MycroftAI:dev Jul 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
CLA: Yes Contributor License Agreement exists (see https://github.com/MycroftAI/contributors)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants