daemon queue worker for config notifications
189556
def worker_config(queue, s3_key, period, verbose):
    """"""
    logging.basicConfig(level=(verbose and logging.DEBUG or logging.INFO))
    logging.getLogger('botocore').setLevel(logging.WARNING)
    logging.getLogger('s3transfer').setLevel(logging.WARNING)

    queue, region = get_queue(queue)
    factory = SessionFactory(region)
    session = factory()
    client = session.client('sqs')
    messages = MessageIterator(client, queue, timeout=20)

    for m in messages:
        msg = unwrap(m)
        if 'configurationItemSummary' in msg:
            rtype = msg['configurationItemSummary']['resourceType']
        else:
            rtype = msg['configurationItem']['resourceType']
        if rtype not in RESOURCE_MAPPING.values():
            log.info("skipping %s" % rtype)
            messages.ack(m)
        log.info("message received %s", m)
88501
def _get_notifier(config):
    '''
    
    '''
    if 'inotify.notifier' not in __context__:
        __context__['inotify.queue'] = collections.deque()
        wm = pyinotify.WatchManager()
        __context__['inotify.notifier'] = pyinotify.Notifier(wm, _enqueue)
        if ('coalesce' in config and
                isinstance(config['coalesce'], bool) and
                config['coalesce']):
            __context__['inotify.notifier'].coalesce_events()
    return __context__['inotify.notifier']
467183
def start(self):
        """"""
        if self.worker is None:
            raise DaemonError('No worker is defined for daemon')

        if os.environ.get('DAEMONOCLE_RELOAD'):
            # If this is actually a reload, we need to wait for the
            # existing daemon to exit first
            self._emit_message('Reloading {prog} ... '.format(prog=self.prog))
            # Orhpan this process so the parent can exit
            self._orphan_this_process(wait_for_parent=True)
            pid = self._read_pidfile()
            if (pid is not None and
                    self._pid_is_alive(pid, timeout=self.stop_timeout)):
                # The process didn't exit for some reason
                self._emit_failed()
                message = ('Previous process (PID {pid}) did NOT '
                           'exit during reload').format(pid=pid)
                self._emit_error(message)
                self._shutdown(message, 1)

        # Check to see if the daemon is already running
        pid = self._read_pidfile()
        if pid is not None:
            # I don't think this should not be a fatal error
            self._emit_warning('{prog} already running with PID {pid}'.format(
                prog=self.prog, pid=pid))
            return

        if not self.detach and not os.environ.get('DAEMONOCLE_RELOAD'):
            # This keeps the original parent process open so that we
            # maintain control of the tty
            self._fork_and_supervise_child()

        if not os.environ.get('DAEMONOCLE_RELOAD'):
            # A custom message is printed for reloading
            self._emit_message('Starting {prog} ... '.format(prog=self.prog))

        self._setup_environment()

        if self.detach:
            self._detach_process()
        else:
            self._emit_ok()

        if self.pidfile is not None:
            self._write_pidfile()

        # Setup signal handlers
        signal.signal(signal.SIGINT, self._handle_terminate)
        signal.signal(signal.SIGQUIT, self._handle_terminate)
        signal.signal(signal.SIGTERM, self._handle_terminate)

        self._run()
734851
def _configure_manager(self):
        
        self._manager = QueueManager(self,
                resource_class=Queue, response_key="queue",
                uri_base="queues")
47378
function spawnWorker() {

        // Create a new worker
        const worker = cluster.fork();

        // Automatically respawn the workers on exit
        worker.on("exit", function () {
            logger("Spawning new worker to replace", worker.process.pid);
            spawnWorker();
        });

        // Listen for messages from this worker
        worker.on("message", messageFromWorker);
    }
396328
function Worker() {
    this.config = JSON.parse(process.env.WORKER_CONFIG);
    if (typeof this.config !== 'object') {
      throw new Error('WORKER_CONFIG is missing');
    }
    process.title = process.variousCluster = this.config.title;
  }
787146
def notify(notification, value=None, unset_environment=False):
    

    if not isinstance(notification, Notification):
        raise TypeError("state must be an instance of Notification")

    state = notification.value

    if state.constant is not None and value:
        raise ValueError(
            "State %s should contain only constant value %r" % (state.name, state.constant),
            state.name, state.constant
        )

    line = "%s=%s" % (
        state.name,
        state.constant if state.constant is not None else state.type(value)
    )

    log.debug("Send %r into systemd", line)

    try:
        return sd_notify(line, unset_environment)
    except Exception as e:
        log.error("%s", e)
940205
def start_daemon_thread(*args, **kwargs):
    """"""
    thread = threading.Thread(*args, **kwargs)
    thread.daemon = True
    thread.start()
    return thread
758664
def notify(args):
    
    from jcvi.utils.iter import flatten

    valid_notif_methods.extend(available_push_api.keys())

    fromaddr = get_email_address(whoami="notifier")

    p = OptionParser(notify.__doc__)
    p.add_option("--method", default="email", choices=valid_notif_methods,
                 help="Specify the mode of notification [default: %default]")
    p.add_option("--subject", default="JCVI: job monitor",
                 help="Specify the subject of the notification message")
    p.set_email()

    g1 = OptionGroup(p, "Optional `push` parameters")
    g1.add_option("--api", default="pushover", \
                  choices=list(flatten(available_push_api.values())),
                  help="Specify API used to send the push notification")
    g1.add_option("--priority", default=0, type="int",
                  help="Message priority (-1 <= p <= 2) [default: %default]")
    g1.add_option("--timestamp", default=None, type="int", \
                  dest="timestamp", \
                  help="Message timestamp in unix format [default: %default]")
    p.add_option_group(g1)

    opts, args = p.parse_args(args)

    if len(args) == 0:
        logging.error("Please provide a brief message to be sent")
        sys.exit(not p.print_help())

    subject = opts.subject
    message = " ".join(args).strip()

    if opts.method == "email":
        toaddr = opts.email.split(",")   # TO address should be in a list
        for addr in toaddr:
            if not is_valid_email(addr):
                logging.debug("Email address `{0}` is not valid!".format(addr))
                sys.exit()
        send_email(fromaddr, toaddr, subject, message)
    else:
        pushnotify(subject, message, api=opts.api, priority=opts.priority, \
                   timestamp=opts.timestamp)
26105
def reserve(cls, queues, res, worker=None, timeout=10):
        
        if isinstance(queues, string_types):
            queues = [queues]
        queue, payload = res.pop(queues, timeout=timeout)
        if payload:
            return cls(queue, payload, res, worker)
899270
def notify_systemd():
    '''
    
    '''
    try:
        import systemd.daemon
    except ImportError:
        if salt.utils.path.which('systemd-notify') \
                and systemd_notify_call('--booted'):
            # Notify systemd synchronously
            notify_socket = os.getenv('NOTIFY_SOCKET')
            if notify_socket:
                # Handle abstract namespace socket
                if notify_socket.startswith('@'):
                    notify_socket = '\0{0}'.format(notify_socket[1:])
                try:
                    sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
                    sock.connect(notify_socket)
                    sock.sendall('READY=1'.encode())
                    sock.close()
                except socket.error:
                    return systemd_notify_call('--ready')
                return True
        return False

    if systemd.daemon.booted():
        try:
            return systemd.daemon.notify('READY=1')
        except SystemError:
            # Daemon was not started by systemd
            pass
662830
def run_updater_in_background(self):
        
        thread = threading.Thread(target=self.updater_loop())
        thread.daemon = True
        thread.start()
927173
def processConfig(self, worker_config):
        
        self.config['headless'] |= worker_config.get("headless", False)
        if self.config['headless']:
            # Launch discovery process
            if not self.discovery_thread:
                self.discovery_thread = discovery.Advertise(
                    port=",".join(str(a) for a in self.getPorts()),
                )
748043
function queue(runner) {
    if (running < tasks) {
      ++running;
      runner(release);
    } else {
      pending.push(runner);
    }
  }
558781
def _mq_callback(self, message):
        
        while threading.active_count() > settings.settings['bash_runnable_count'] + self.initial_thread_count:
            time.sleep(0.1)

        t = BashRunnable(self.logger, message, self.consumer, self.performance_tracker)
        t.daemon = True
        t.start()
710759
protected static function monitorWorkersForLinux()
    {
        static::$_status = static::STATUS_RUNNING;
        while (1) {
            // Calls signal handlers for pending signals.
            pcntl_signal_dispatch();
            // Suspends execution of the current process until a child has exited, or until a signal is delivered
            $status = 0;
            $pid    = pcntl_wait($status, WUNTRACED);
            // Calls signal handlers for pending signals again.
            pcntl_signal_dispatch();
            // If a child has already exited.
            if ($pid > 0) {
                // Find out witch worker process exited.
                foreach (static::$_pidMap as $worker_id => $worker_pid_array) {
                    if (isset($worker_pid_array[$pid])) {
                        $worker = static::$_workers[$worker_id];
                        // Exit status.
                        if ($status !== 0) {
                            static::log("worker[" . $worker->name . ":$pid] exit with status $status");
                        }

                        // For Statistics.
                        if (!isset(static::$_globalStatistics['worker_exit_info'][$worker_id][$status])) {
                            static::$_globalStatistics['worker_exit_info'][$worker_id][$status] = 0;
                        }
                        static::$_globalStatistics['worker_exit_info'][$worker_id][$status]++;

                        // Clear process data.
                        unset(static::$_pidMap[$worker_id][$pid]);

                        // Mark id is available.
                        $id                              = static::getId($worker_id, $pid);
                        static::$_idMap[$worker_id][$id] = 0;

                        break;
                    }
                }
                // Is still running state then fork a new worker process.
                if (static::$_status !== static::STATUS_SHUTDOWN) {
                    static::forkWorkers();
                    // If reloading continue.
                    if (isset(static::$_pidsToRestart[$pid])) {
                        unset(static::$_pidsToRestart[$pid]);
                        static::reload();
                    }
                }
            }

            // If shutdown state and all child processes exited then master process exit.
            if (static::$_status === static::STATUS_SHUTDOWN && !static::getAllWorkerPids()) {
                static::exitAndClearAll();
            }
        }
    }
1095362
def message_worker(device):
    """"""
    _LOGGER.debug("Starting Worker Thread.")
    msg_q = device.messages

    while True:

        if not msg_q.empty():
            message = msg_q.get()

            data = {}
            try:
                data = json.loads(message.decode("utf-8"))
            except ValueError:
                _LOGGER.error("Received invalid message: %s", message)

            if 'device_id' in data:
                device_id = data.get('device_id')
                if device_id == device.device_id:
                    device.handle_event(data)
                else:
                    _LOGGER.warning("Received message for unknown device.")
            msg_q.task_done()

        time.sleep(0.2)