How to use the circuits.core.handlers.handler function in circuits

To help you get started, we’ve selected a few circuits examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github circuits / circuits / circuits / protocols / stomp / client.py View on Github external
    @handler("send")
    def send(self, event, destination, body, headers=None, receipt=None):
        LOG.debug("send()")
        if not self.connected:
            LOG.error("Can't send when Stomp is disconnected")
            self.fire(on_stomp_error(None, Exception("Message send attempted with stomp disconnected")))
            event.success = False
            return
        try:
            self._client.send(destination, body=body.encode('utf-8'), headers=headers, receipt=receipt)
            LOG.debug("Message sent")
        except StompConnectionError as err:
            event.success = False
            self.fire(disconnected())
        except StompError as err:
            LOG.error("Error sending ack")
            event.success = False
github circuits / circuits / circuits / protocols / websocket.py View on Github external
            @handler("disconnect", channel=parent.channel)
            def _on_disconnect(self, *args):
                if self._sock is not None:
                    if args[0] != self._sock:
                        return
                self.unregister()
            self.addHandler(_on_disconnect)
github circuits / circuits / circuits / protocols / stomp / client.py View on Github external
    @handler("connect")
    def connect(self, event, host=None, *args, **kwargs):
        """ connect to Stomp server """
        LOG.info("Connect to Stomp...")
        try:
            self._client.connect(heartBeats=self._heartbeats,
                                 host=host,
                                 versions=self._accept_versions,
                                 connectTimeout=self._connect_timeout,
                                 connectedTimeout=self._connected_timeout)
            LOG.info("State after Connection Attempt: %s", self._client.session.state)
            if self.connected:
                LOG.info("Connected to %s", self._stomp_server)
                self.fire(connected())
                self.start_heartbeats()
                return "success"
github ibmresilient / resilient-community-apps / examples / resilientsystems.com / python / Circuits / post_tweet / twitterpost.py View on Github external
    @handler("post_to_twitter")
    def _post_tweet(self, event, *args, **kwargs):
        # Get the incident ID and tweet body
        properties = event.message['properties']
        tweet_body = properties['tweet_body']

        LOG.info(u'the tweet is: %s', tweet_body)
        twitter = Twitter(auth=OAuth(self.options['oauth_token'],
                                     self.options['oauth_secret'],
                                     self.options['consumer_key'],
                                     self.options['consumer_secret']))
        twitter.statuses.update(status=unicode(tweet_body))

        # Log output
        LOG.info("Posted tweet to twitter! :D")

        yield "Tweet Posted"
github circuits / circuits / circuits / protocols / websocket.py View on Github external
            @handler("disconnect", channel=parent.channel)
            def _on_disconnect(self, *args):
                if self._sock is not None:
                    if args[0] != self._sock:
                        return
                self.unregister()
            self.addHandler(_on_disconnect)
github ibmresilient / resilient-community-apps / examples / resilientsystems.com / python / Circuits / phase_milestone / phase_milestone.py View on Github external
    @handler("phase_milestone")
    def _workfunction(self, event, source=None, headers=None, message=None):
        """Do the work"""

        client = self.rest_client()
        incident = message["incident"]
        inc_id = incident["id"]
        LOG.info(u"AutoPhase: incident %s: '%s'", inc_id, incident["name"])

        timestamp = int(headers.get("timestamp"))
        phase_id = int(incident["phase_id"])
        phase_name = self.phase_names.get(phase_id, str(phase_id))
        title = u"Phase changed to '{}'".format(phase_name)

        # Find the previous phase by looking at the newsfeed.
        prev_phase = None
        news_url = "/incidents/{}/newsfeed?object_type=INCIDENT&entry_type=PHASE_CHANGE&limit=1".format(inc_id)
github ibmresilient / resilient-community-apps / resilientsystems.com / python / assign_tasks / assigntasks.py View on Github external
    @handler("assign_tasks")
    def _assign_tasks(self, event, *args, **kwargs):
        """Function to assign tasks in an incident"""
        # get information about the incident, such as its ID
        incident = event.message["incident"]
        inc_id = incident["id"]
        LOG.info("Got incident info!")
        # get the tasks and groups from resilient
        mytasks = self.rest_client().get('/incidents/'+str(inc_id)+'/tasktree')
        mygroups = self.rest_client().get('/groups')
        LOG.info("Got incident "+str(inc_id)+" tasktree and groups!")
        # make a list of all the members and owner of an incident
        idlist = [incident['owner_id']]+incident['members']
        # if the id belongs to a group, remove the group and replace it with
        # its constituent members
        for group in mygroups:
            if group['id'] in idlist:
github circuits / circuits / circuits / core / pollers.py View on Github external
    @handler("generate_events", priority=-9)
    def _on_generate_events(self, event):
        """
        Pollers have slightly higher priority than the default handler
        from Manager to ensure that they are invoked before the
        default handler. They act as event filters to avoid the additional
        invocation of the default handler which would be unnecessary
        overhead.
        """

        event.stop()
        self._generate_events(event)
github ibmresilient / resilient-community-apps / examples / resilientsystems.com / python / Circuits / new_incident / newincident.py View on Github external
    @handler("escalate_incident_between_orgs")
    def _add_incident(self, event, *args, **kwargs):
        """Function to add an incident to another org."""

        # get information about the incident, such as its ID
        incident = event.message["incident"]
        inc_id = incident["id"]

        # set verify option
        verify = self.options['verify']
        if self.options['verify'] == "False":
            verify = False
        elif self.options['verify'] == "True":
            verify = True

        LOG.info("Connecting to alternate org...")
        resilient_client = resilient.SimpleClient(self.options['to_org_name'],
github circuits / circuits / circuits / core / manager.py View on Github external
state.task_event,
                        (e for e in (ExceptionWrapper(TimeoutError()),)),
                        state.parent
                    )
                )
                self.removeHandler(_on_done_handler, "%s_done" % event_name)
                self.removeHandler(_on_tick_handler, "generate_events")
            elif state.timeout > 0:
                state.timeout -= 1

        if not channels:
            channels = (None,)

        for channel in channels:
            _on_event_handler = self.addHandler(
                handler(event_name, channel=channel)(_on_event))
            _on_done_handler = self.addHandler(
                handler("%s_done" % event_name, channel=channel)(_on_done))
            if state.timeout >= 0:
                _on_tick_handler = state.tick_handler = self.addHandler(
                    handler("generate_events", channel=channel)(_on_tick))

        yield state

        self.removeHandler(_on_done_handler, "%s_done" % event_name)

        if state.event is not None:
            yield CallValue(state.event.value)