Merge pull request #17240 from mgwilliams/ext_processes

Ext processes
This commit is contained in:
Thomas S Hatch 2014-11-06 16:46:15 -07:00
commit e9cda43070
2 changed files with 72 additions and 0 deletions

View file

@ -0,0 +1,59 @@
.. _ext_processes:
===============================
Running Custom Master Processes
===============================
In addition to the processes that the Salt Master automatically spawns,
it is possible to configure it to start additional custom processes.
This is useful if a dedicated process is needed that should run throughout
the life of the Salt Master. For periodic independent tasks, a
:doc:`scheduled runner <../jobs/schedule.rst>` may be more appropriate.
Processes started in this way will be restarted if they die and will be
killed when the Salt Master is shut down.
Example Configureation
======================
Processes are declared in the master config file with the `ext_processes`
option. Processes will be started in the order they are declared.
.. code-block:: yaml
ext_processes:
- mymodule.TestProcess
- mymodule.AnotherProcess
Example Process Class
=====================
.. code-block:: python
# Import python libs
import time
import logging
from multiprocessing import Process
# Import Salt libs
from salt.utils.event import SaltEvent
log = logging.getLogger(__name__)
class TestProcess(Process):
def __init__(self, opts):
Process.__init__(self)
self.opts = opts
def run(self):
self.event = SaltEvent('master', self.opts['sock_dir'])
i = 0
while True:
self.event.fire_event({'iteration': i}, 'ext_processes/test{0}')
time.sleep(60)

View file

@ -348,6 +348,19 @@ class Master(SMaster):
log.info('Creating master reactor process')
process_manager.add_process(salt.utils.event.Reactor, args=(self.opts,))
ext_procs = self.opts.get('ext_processes', [])
for proc in ext_procs:
log.info('Creating ext_processes process: {0}'.format(proc))
try:
mod = '.'.join(proc.split('.')[:-1])
cls = proc.split('.')[-1]
_tmp = __import__(mod, globals(), locals(), [cls], -1)
cls = _tmp.Test
process_manager.add_process(cls, args=(self.opts,))
except Exception as e:
log.warning(('Error creating ext_processes '
'process: {0}').format(proc))
if HAS_HALITE and 'halite' in self.opts:
log.info('Creating master halite process')
process_manager.add_process(Halite, args=(self.opts['halite'],))