示例#1
0
文件: base.py 项目: vtdat/st2
    def _purge_trigger_instances(self):
        """
        Purge trigger instances which match the criteria defined in the config.
        """
        LOG.info('Performing garbage collection for trigger instances')

        utc_now = get_datetime_utc_now()
        timestamp = (utc_now -
                     datetime.timedelta(days=self._trigger_instances_ttl))

        # Another sanity check to make sure we don't delete new executions
        if timestamp > (utc_now - datetime.timedelta(days=MINIMUM_TTL_DAYS)):
            raise ValueError(
                'Calculated timestamp would violate the minimum TTL constraint'
            )

        timestamp_str = isotime.format(dt=timestamp)
        LOG.info('Deleting trigger instances older than: %s' % (timestamp_str))

        assert timestamp < utc_now

        try:
            purge_trigger_instances(logger=LOG, timestamp=timestamp)
        except Exception as e:
            LOG.exception('Failed to trigger instances: %s' %
                          (six.text_type(e)))

        return True
示例#2
0
    def _purge_trigger_instances(self):
        """
        Purge trigger instances which match the criteria defined in the config.
        """
        utc_now = get_datetime_utc_now()
        timestamp = utc_now - datetime.timedelta(
            days=self._trigger_instances_ttl)

        # Another sanity check to make sure we don't delete new executions
        if timestamp > (utc_now - datetime.timedelta(days=MINIMUM_TTL_DAYS)):
            raise ValueError(
                "Calculated timestamp would violate the minimum TTL constraint"
            )

        timestamp_str = isotime.format(dt=timestamp)
        LOG.info("Deleting trigger instances older than: %s" % (timestamp_str))

        if timestamp >= utc_now:
            raise ValueError(f"Calculated timestamp ({timestamp}) is"
                             f" later than now in UTC ({utc_now}).")

        try:
            purge_trigger_instances(logger=LOG, timestamp=timestamp)
        except Exception as e:
            LOG.exception("Failed to trigger instances: %s" %
                          (six.text_type(e)))

        return True
    def test_purge(self):
        now = date_utils.get_datetime_utc_now()

        instance_db = TriggerInstanceDB(trigger='purge_tool.dummy.trigger',
                                        payload={
                                            'hola': 'hi',
                                            'kuraci': 'chicken'
                                        },
                                        occurrence_time=now -
                                        timedelta(days=20),
                                        status=TRIGGER_INSTANCE_PROCESSED)
        TriggerInstance.add_or_update(instance_db)

        instance_db = TriggerInstanceDB(trigger='purge_tool.dummy.trigger',
                                        payload={
                                            'hola': 'hi',
                                            'kuraci': 'chicken'
                                        },
                                        occurrence_time=now -
                                        timedelta(days=5),
                                        status=TRIGGER_INSTANCE_PROCESSED)
        TriggerInstance.add_or_update(instance_db)

        self.assertEqual(len(TriggerInstance.get_all()), 2)
        purge_trigger_instances(logger=LOG, timestamp=now - timedelta(days=10))
        self.assertEqual(len(TriggerInstance.get_all()), 1)
    def test_purge(self):
        now = date_utils.get_datetime_utc_now()

        instance_db = TriggerInstanceDB(trigger='purge_tool.dummy.trigger',
                                        payload={'hola': 'hi', 'kuraci': 'chicken'},
                                        occurrence_time=now - timedelta(days=20))
        TriggerInstance.add_or_update(instance_db)

        instance_db = TriggerInstanceDB(trigger='purge_tool.dummy.trigger',
                                        payload={'hola': 'hi', 'kuraci': 'chicken'},
                                        occurrence_time=now - timedelta(days=5))
        TriggerInstance.add_or_update(instance_db)

        self.assertEqual(len(TriggerInstance.get_all()), 2)
        purge_trigger_instances(logger=LOG, timestamp=now - timedelta(days=10))
        self.assertEqual(len(TriggerInstance.get_all()), 1)
def main():
    _register_cli_opts()
    common_setup(config=config, setup_db=True, register_mq_exchanges=False)

    # Get config values
    timestamp = cfg.CONF.timestamp

    if not timestamp:
        LOG.error('Please supply a timestamp for purging models. Aborting.')
        return 1
    else:
        timestamp = datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S.%fZ')
        timestamp = timestamp.replace(tzinfo=pytz.UTC)

    # Purge models.
    try:
        purge_trigger_instances(logger=LOG, timestamp=timestamp)
    except Exception as e:
        LOG.exception(str(e))
        return FAILURE_EXIT_CODE
    finally:
        common_teardown()

    return SUCCESS_EXIT_CODE
def main():
    _register_cli_opts()
    common_setup(config=config, setup_db=True, register_mq_exchanges=False)

    # Get config values
    timestamp = cfg.CONF.timestamp

    if not timestamp:
        LOG.error('Please supply a timestamp for purging models. Aborting.')
        return 1
    else:
        timestamp = datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S.%fZ')
        timestamp = timestamp.replace(tzinfo=pytz.UTC)

    # Purge models.
    try:
        purge_trigger_instances(logger=LOG, timestamp=timestamp)
    except Exception as e:
        LOG.exception(str(e))
        return FAILURE_EXIT_CODE
    finally:
        common_teardown()

    return SUCCESS_EXIT_CODE
示例#7
0
文件: base.py 项目: StackStorm/st2
    def _purge_trigger_instances(self):
        """
        Purge trigger instances which match the criteria defined in the config.
        """
        LOG.info('Performing garbage collection for trigger instances')

        utc_now = get_datetime_utc_now()
        timestamp = (utc_now - datetime.timedelta(days=self._trigger_instances_ttl))

        # Another sanity check to make sure we don't delete new executions
        if timestamp > (utc_now - datetime.timedelta(days=MINIMUM_TTL_DAYS)):
            raise ValueError('Calculated timestamp would violate the minimum TTL constraint')

        timestamp_str = isotime.format(dt=timestamp)
        LOG.info('Deleting trigger instances older than: %s' % (timestamp_str))

        assert timestamp < utc_now

        try:
            purge_trigger_instances(logger=LOG, timestamp=timestamp)
        except Exception as e:
            LOG.exception('Failed to trigger instances: %s' % (six.text_type(e)))

        return True