Base class for a service
This class will be executed in a new child process/worker ServiceWorker of a ServiceManager. It registers signals to manager the reloading and the ending of the process.
Methods run(), terminate() and reload() are optional.
Create a new Service
Parameters: | worker_id (int) – the identifier of this service instance |
---|
Timeout after which a gracefully shutdown service will exit. zero means endless wait. None means same as ServiceManager that launch the service
Service name used in the process title and the log messages in additionnal of the worker_id.
Reloading of the service
This method will be executed when the Service receives a SIGHUP.
If not implemented the process will just end with status 0 and ServiceRunner will start a new fresh process for this service with the same worker_id.
Any exceptions raised by this method will be logged and the worker will exit with status 1.
Method representing the service activity
If not implemented the process will just wait to receive an ending signal.
This method is ran into the thread and can block or return as needed
Any exceptions raised by this method will be logged and the worker will exit with status 1.
Gracefully shutdown the service
This method will be executed when the Service has to shutdown cleanly.
If not implemented the process will just end with status 0.
To customize the exit code, the SystemExit exception can be used.
Any exceptions raised by this method will be logged and the worker will exit with status 1.
Manage lifetimes of services
ServiceManager acts as a master process that controls the lifetime of children processes and restart them if they die unexpectedly. It also propagate some signals (SIGTERM, SIGALRM, SIGINT and SIGHUP) to them.
Each child process (ServiceWorker) runs an instance of a Service.
An application must create only one ServiceManager class and use ServiceManager.run() as main loop of the application.
Usage:
class MyService(Service):
def __init__(self, worker_id, myconf):
super(MyService, self).__init__(worker_id)
preparing_my_job(myconf)
self.running = True
def run(self):
while self.running:
do_my_job()
def terminate(self):
self.running = False
gracefully_stop_my_jobs()
def reload(self):
restart_my_job()
class MyManager(ServiceManager):
def __init__(self):
super(MyManager, self).__init__()
self.register_hooks(on_reload=selfreload)
conf = {'foobar': 2}
self.service_id = self.add(MyService, 5, conf)
def reload(self):
self.reconfigure(self.service_id, 10)
MyManager().run()
This will create 5 children processes running the service MyService.
Creates the ServiceManager object
Parameters: | wait_interval (float) – time between each new process spawn |
---|
Add a new service to the ServiceManager
Parameters: |
|
---|---|
Returns: | a service id |
Return type: | uuid.uuid4 |
Reconfigure a service registered in ServiceManager
Parameters: |
|
---|---|
Raises: | ValueError |
Register hook methods
This can be callable multiple times to add more hooks, hooks are executed in added order. If a hook raised an exception, next hooks will be not executed.
Parameters: |
|
---|
Start and supervise services workers
This method will start and supervise all children processes until the master process asked to shutdown by a SIGTERM.
All spawned processes are part of the same unix process group.