| 1 |
# All rights reserved. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
import os |
| 16 |
|
| 17 |
from oslo_service import service |
| 18 |
import setproctitle |
| 19 |
|
| 20 |
from neutron_lib.callbacks import events |
| 21 |
from neutron_lib.callbacks import registry |
| 22 |
from neutron_lib.callbacks import resources |
| 23 |
|
| 24 |
|
| 25 |
class BaseWorker(service.ServiceBase): |
| 26 |
"""Partial implementation of the ServiceBase ABC. |
| 27 |
|
| 28 |
Subclasses will still need to add the other abstract methods defined in |
| 29 |
service.ServiceBase. See oslo_service for more details. |
| 30 |
|
| 31 |
If a plugin needs to handle synchronization with the Neutron database and |
| 32 |
do this only once instead of in every API worker, for instance, it would |
| 33 |
define a BaseWorker class and the plugin would have get_workers return |
| 34 |
an array of BaseWorker instances. For example: |
| 35 |
|
| 36 |
.. code-block:: python |
| 37 |
|
| 38 |
class MyPlugin(...): |
| 39 |
def get_workers(self): |
| 40 |
return [MyPluginWorker()] |
| 41 |
|
| 42 |
class MyPluginWorker(BaseWorker): |
| 43 |
def start(self): |
| 44 |
super(MyPluginWorker, self).start() |
| 45 |
do_sync() |
| 46 |
""" |
| 47 |
|
| 48 |
# default class value for case when super().__init__ is not called |
| 49 |
_default_process_count = 1 |
| 50 |
|
| 51 |
def __init__(self, worker_process_count=_default_process_count, |
| 52 |
set_proctitle='on', desc=None): |
| 53 |
"""Initialize a worker instance. |
| 54 |
|
| 55 |
:param worker_process_count: Defines how many processes to spawn for |
| 56 |
worker: |
| 57 |
0 - spawn 1 new worker thread, |
| 58 |
1..N - spawn N new worker processes |
| 59 |
set_proctitle: |
| 60 |
'off' - do not change process title |
| 61 |
'on' - set process title to descriptive string and parent |
| 62 |
'brief' - set process title to descriptive string |
| 63 |
desc: |
| 64 |
process descriptive string |
| 65 |
""" |
| 66 |
self._worker_process_count = worker_process_count |
| 67 |
self._my_pid = os.getpid() |
| 68 |
self._set_proctitle = set_proctitle |
| 69 |
if set_proctitle == 'on': |
| 70 |
self._parent_proctitle = setproctitle.getproctitle() |
| 71 |
self.desc = desc |
| 72 |
|
| 73 |
@property |
| 74 |
def worker_process_count(self): |
| 75 |
"""The worker's process count. |
| 76 |
|
| 77 |
:returns: The number of processes to spawn for this worker. |
| 78 |
""" |
| 79 |
return self._worker_process_count |
| 80 |
|
| 81 |
@property |
| 82 |
def set_proctitle(self): |
| 83 |
return self._set_proctitle |
| 84 |
|
| 85 |
@set_proctitle.setter |
| 86 |
def set_proctitle(self, value): |
| 87 |
self._set_proctitle = value |
| 88 |
|
| 89 |
def setproctitle(self, name="neutron-server", desc=None): |
| 90 |
if self._set_proctitle == "off" or os.getpid() == self._my_pid: |
| 91 |
return |
| 92 |
|
| 93 |
if not desc: |
| 94 |
desc = self.__class__.__name__ |
| 95 |
|
| 96 |
proctitle = f"{name}: {desc}" |
| 97 |
if self._set_proctitle == "on": |
| 98 |
proctitle += f" ({self._parent_proctitle})" |
| 99 |
|
| 100 |
setproctitle.setproctitle(proctitle) |
| 101 |
|
| 102 |
def start(self, name="neutron-server", desc=None): |
| 103 |
"""Start the worker. |
| 104 |
|
| 105 |
If worker_process_count is greater than 0, a callback notification |
| 106 |
is sent. Subclasses should call this method before doing their |
| 107 |
own start() work. |
| 108 |
|
| 109 |
Automatically sets the process title to indicate that this is a |
| 110 |
child worker, customizable via the name and desc arguments. |
| 111 |
|
| 112 |
:returns: None |
| 113 |
""" |
| 114 |
|
| 115 |
# If we are a child process, set our proctitle to something useful |
| 116 |
desc = desc or self.desc |
| 117 |
self.setproctitle(name, desc) |
| 118 |
if self.worker_process_count > 0: |
| 119 |
registry.publish(resources.PROCESS, events.AFTER_INIT, self.start) |