def listen(self):
        '''
        The main event loop where we receive the events and
        start the workers that dump our data into the database
        '''
        # log on to saltstacks event-bus
        event = salt.utils.event.SaltEvent(
            self.node,
            self.sock_dir,
        )

        # we store our events in a list, we dont really care about an order
        # or what kind of data is put in there. all that is configured with the
        # templates configured in the configfile
        event_queue = []

        # start our timers
        self.ev_timer.start()
        self.state_timer.start()

        # this is for logline chronology so the timer-message always comes
        # _before_ the actual startup-message of the listening loop below :-)
        time.sleep(1)

        log.info("Entering main event loop")
        log.info("Listening on: {0}".format(event.puburi))

        # read everything we can get our hands on
        while True:
            # the zmq-socket does not like ^C very much, make the error
            # a little more graceful. alright, alright, ignore the damn thing,
            # we're exiting anyways...
            try:
                ret = event.get_event(full=True)
            except zmq.ZMQError:
                pass
            except __HOLE__:
                log.info('Received CTRL+C, shutting down')
                self.stop(signal.SIGTERM, None)

            # if we have not received enough events in to reach event_limit
            # and the timer has fired, dump the events collected so far
            # to the workers
            if(self.ev_timer_ev):
                if (len(self.running_workers) < self.max_workers) and \
                   (len(event_queue) > 0):

                    self._init_worker(event_queue)

                    # reset our queue to prevent duplicate entries
                    del event_queue[:]

                    # we reset the timer.ev_timer_ev  at the end of the loop
                    # so we can update the stats that are logged

            if ret is None:
                continue

            # filter only the events we're interested in. all events have a tag
            # we can filter them by. we match with a precompiled regex
            if 'tag' in ret:
                # filter out events with an empty tag. those are special
                if ret['tag'] != '':
                    # run through our configured events and try to match the
                    # current events tag against the ones we're interested in
                    for key in self.event_map.keys():
                        if self.event_map[key]['tag'].match(ret['tag']):
                            log.debug("Matching on {0}:{1}".format(key, ret['tag']))

                            prio = self.event_map[key].get('prio', 0)

                            # push prio1-events directly into a worker
                            if prio > 0:
                                log.debug('Prio1 event found, pushing immediately!')
                                self.events_han += 1
                                self._init_worker([ret])
                            else:
                                event_queue.append(ret)
                                self.events_han += 1

            # once we reach the event_limit, start a worker that
            # writes that data in to the database
            if len(event_queue) >= self.event_limit:
                # only start a worker if not too many workers are running
                if len(self.running_workers) < self.max_workers:
                    self._init_worker(event_queue)
                    # reset the timer
                    self.ev_timer.reset()

                    # reset our queue to prevent duplicate entries
                    del event_queue[:]
                else:
                    # FIXME: we need to handle this situation somehow if
                    # too many workers are running. just flush the events?
                    # there really is no sane way except queueing more and more
                    # until some sort of limit is reached and we care more about
                    # our saltmaster than about the collected events!
                    log.critical("Too many workers running, loosing data!!!")

            # a list for the workers that are still running
            clean_workers = []

            # run through all the workers and join() the ones
            # that have finished dumping their data and keep
            # the running ones on our list
            for worker in self.running_workers:
                if worker.isAlive():
                    clean_workers.append(worker)
                else:
                    worker.join()
                    log.debug("Joined worker #{0}".format(worker.getName()))
                    self.threads_join += 1

            # get rid of the old reference  and set a new one
            # FIXME: is this really neccessary?
            del self.running_workers

            self.running_workers = clean_workers
            self.events_rec += 1

            if(self.ev_timer_ev):
                self.ev_timer_ev = False

        log.info("Listen loop ended...")
dataset/ETHPy150Open felskrone/salt-eventsd/salteventsd/daemon.py/SaltEventsDaemon.listen