Example #1
0
    def test_handle_message_start(self):
        """Test Await.handle_message() with 'start' message."""
        confparser = ConfigParser()
        confparser.add_section('bureaucrat')
        confparser.set('bureaucrat', 'storage_dir', STORAGE_DIR)
        Configs.instance(confparser)
        subscriptions = [{"target": "some-id"}]
        Storage.instance().save("subscriptions", "test_event",
                                json.dumps(subscriptions))

        msg = Message(name='start', target='fake-id_0', origin='fake-id')
        self.fexpr.state = 'ready'
        result = self.fexpr.handle_message(self.ch, msg)
        self.assertEqual(result, 'consumed')
        self.assertEqual(self.fexpr.state, 'active')
        filename = os.path.join(STORAGE_DIR, "subscriptions/test_event")
        with open(filename) as fhdl:
            subscriptions.append({'target': 'fake-id_0'})
            self.assertEqual(json.load(fhdl), subscriptions)

        Configs._instance = None
        Storage._instance = None
        os.unlink(filename)
        os.rmdir(os.path.join(STORAGE_DIR, "subscriptions"))
        os.removedirs(STORAGE_DIR)
Example #2
0
 def setUp(self):
     """Set up SUT."""
     confparser = ConfigParser()
     confparser.add_section('bureaucrat')
     confparser.set('bureaucrat', 'storage_dir', STORAGE_DIR)
     Configs.instance(confparser)
     Storage.instance()
     self.ch = Mock()
     self.schedule = Schedule(self.ch)
Example #3
0
 def setUp(self):
     """Set up SUT."""
     confparser = ConfigParser()
     confparser.add_section('bureaucrat')
     confparser.set('bureaucrat', 'storage_dir', STORAGE_DIR)
     Configs.instance(confparser)
     Storage.instance()
     self.ch = Mock()
     self.schedule = Schedule(self.ch)
Example #4
0
def _subscribe(event, target):
    """Subscribe given target to event."""
    storage = Storage.instance()
    subscriptions = []
    if storage.exists("subscriptions", event):
        subscriptions = json.loads(storage.load("subscriptions", event))
    subscriptions.append({"target": target})
    storage.save("subscriptions", event, json.dumps(subscriptions))
Example #5
0
    def create_from_string(pdef, pid):
        """Create Workflow instance from process definition string."""

        LOG.debug("Creating workflow instance from string.")

        xmlelement = ET.fromstring(pdef)
        assert xmlelement.tag == 'process'

        Storage.instance().save("definition", pid, pdef)

        parent_id = ''
        if "parent" in xmlelement.attrib:
            parent_id = xmlelement.attrib["parent"]

        process = Process(parent_id, xmlelement, pid, Context())
        workflow = Workflow(process)
        workflow.save()
        return workflow
Example #6
0
def _subscribe(event, target):
    """Subscribe given target to event."""
    storage = Storage.instance()
    subscriptions = []
    if storage.exists("subscriptions", event):
        subscriptions = json.loads(storage.load("subscriptions", event))
    subscriptions.append({
        "target": target
    })
    storage.save("subscriptions", event, json.dumps(subscriptions))
Example #7
0
    def test_register(self):
        """Test Schedule.register()."""

        instant = 10000
        self.schedule.register(code="timeout",
                               instant=instant,
                               target="fake-id")
        storage = Storage.instance()
        schedules = [{"code": "timeout", "target": "fake-id"}]
        self.assertEqual(storage.load("schedule", str(instant)),
                         json.dumps(schedules))
        storage.delete("schedule", str(instant))
Example #8
0
    def register(self, code, instant, target):
        """Register new schedule."""
        LOG.debug("Register '%s' for %s at %d", code, target, instant)

        assert isinstance(instant, int)

        schedules = []
        storage = Storage.instance()
        if storage.exists("schedule", str(instant)):
            schedules = json.loads(storage.load("schedule", str(instant)))
        schedules.append({"code": code, "target": target})
        storage.save("schedule", str(instant), json.dumps(schedules))
Example #9
0
    def test_register(self):
        """Test Schedule.register()."""

        instant = 10000
        self.schedule.register(code="timeout", instant=instant,
                               target="fake-id")
        storage = Storage.instance()
        schedules = [{
            "code": "timeout",
            "target": "fake-id"
        }]
        self.assertEqual(storage.load("schedule", str(instant)),
                         json.dumps(schedules))
        storage.delete("schedule", str(instant))
Example #10
0
    def handle_alarm(self):
        """Load schedule."""

        LOG.debug("Handling alarm")
        timestamp = int(time.time())
        storage = Storage.instance()
        for key in storage.keys("schedule"):
            if timestamp >= int(key):
                schedules = json.loads(storage.load("schedule", key))
                for sch in schedules:
                    self.channel.send(Message(name=sch["code"],
                                              origin="", target=sch["target"]))
                    LOG.debug("Sent '%s' to %s", sch["code"],
                              sch["target"])
                storage.delete("schedule", key)
Example #11
0
    def register(self, code, instant, target):
        """Register new schedule."""
        LOG.debug("Register '%s' for %s at %d", code, target, instant)

        assert isinstance(instant, int)

        schedules = []
        storage = Storage.instance()
        if storage.exists("schedule", str(instant)):
            schedules = json.loads(storage.load("schedule", str(instant)))
        schedules.append({
            "code": code,
            "target": target
        })
        storage.save("schedule", str(instant), json.dumps(schedules))
Example #12
0
    def handle_alarm(self):
        """Load schedule."""

        LOG.debug("Handling alarm")
        timestamp = int(time.time())
        storage = Storage.instance()
        for key in storage.keys("schedule"):
            if timestamp >= int(key):
                schedules = json.loads(storage.load("schedule", key))
                for sch in schedules:
                    self.channel.send(
                        Message(name=sch["code"],
                                origin="",
                                target=sch["target"]))
                    LOG.debug("Sent '%s' to %s", sch["code"], sch["target"])
                storage.delete("schedule", key)
Example #13
0
    def test_handle_alarm(self):
        """Test Schedule.handle_alarm()."""

        instant = 10000
        storage = Storage.instance()
        schedules = [{"code": "timeout", "target": "fake-id"}]
        storage.save("schedule", str(instant), json.dumps(schedules))
        with patch('bureaucrat.schedule.Message') as MockMessage:
            newmsg = Message(name='timeout', target='fake-id', origin='')
            MockMessage.return_value = newmsg
            self.schedule.handle_alarm()
            MockMessage.assert_called_once_with(name='timeout',
                                                target='fake-id',
                                                origin='')
            self.schedule.channel.send.assert_called_once_with(newmsg)
            self.assertFalse(storage.exists("schedule", str(instant)))
Example #14
0
    def load(process_id):
        """Return existing workflow instance loaded from storage."""

        LOG.debug("Load a process definition from %s", process_id)
        storage = Storage.instance()
        pdef = storage.load("definition", process_id)
        xmlelement = ET.fromstring(pdef)
        assert xmlelement.tag == 'process'

        parent_id = ''
        if "parent" in xmlelement.attrib:
            parent_id = xmlelement.attrib["parent"]

        process = Process(parent_id, xmlelement, process_id, Context())
        process.reset_state(json.loads(storage.load("process", process.id)))
        return Workflow(process)
Example #15
0
    def handle_event(self, channel, method, header, body):
        """Handle event."""

        LOG.debug("Method: %r", method)
        LOG.debug("Header: %r", header)
        LOG.debug("Handling event with Body: %r", body)
        msg = json.loads(body)
        eventname = msg["event"]
        storage = Storage.instance()
        if storage.exists("subscriptions", eventname):
            subscriptions = json.loads(storage.load("subscriptions",
                                                    eventname))
            for subscr in subscriptions:
                wmsg = Message(name='triggered', target=subscr["target"],
                               origin='', payload=msg)
                ChannelWrapper(channel).send(wmsg)
            storage.delete("subscriptions", eventname)
        channel.basic_ack(method.delivery_tag)
Example #16
0
    def test_handle_alarm(self):
        """Test Schedule.handle_alarm()."""

        instant = 10000
        storage = Storage.instance()
        schedules = [{
            "code": "timeout",
            "target": "fake-id"
        }]
        storage.save("schedule", str(instant), json.dumps(schedules))
        with patch('bureaucrat.schedule.Message') as MockMessage:
            newmsg = Message(name='timeout', target='fake-id', origin='')
            MockMessage.return_value = newmsg
            self.schedule.handle_alarm()
            MockMessage.assert_called_once_with(name='timeout',
                                                target='fake-id', origin='')
            self.schedule.channel.send.assert_called_once_with(newmsg)
            self.assertFalse(storage.exists("schedule", str(instant)))
Example #17
0
    def save(self):
        """Save workflow state to storage."""

        Storage.instance().save("process", self.process.id,
                                json.dumps(self.process.snapshot()))
Example #18
0
    def delete(self):
        """Delete workflow instance from storage."""

        storage = Storage.instance()
        storage.delete("process", self.process.id)
        storage.delete("definition", self.process.id)