예제 #1
0
 def insert_periodics(cls, bot, scheduler):
     try:
         health_report_period = bot.config.jenkins.health_report_period
     except AttributeError:
         pass
     else:
         slack_client = bot.clients.get("slack_client")
         slack_sender = bot.slack_sender
         if slack_client is not None and slack_sender is not None:
             hr = peu.make_periodic_runner("jenkins health report",
                                           cls,
                                           health_report_period,
                                           channel=bot.config.admin_channel,
                                           log=LOG)
             hr.__module__ = __name__
             hr.__name__ = "run_check_jenkins_health"
             hr_trigger = cron.CronTrigger.from_crontab(
                 health_report_period, timezone=bot.config.tz)
             hr_name = reflection.get_callable_name(hr)
             hr_description = "Periodically analyzes jenkins job health."
             scheduler.add_job(
                 hr,
                 trigger=hr_trigger,
                 jobstore='memory',
                 name="\n".join([hr_name, hr_description]),
                 id=utils.hash_pieces(
                     [health_report_period, hr_name, hr_description],
                     max_len=8),
                 args=(bot, slack_client, slack_sender),
                 coalesce=True)
예제 #2
0
 def _insert_periodics(self, scheduler):
     try:
         danger_period = self.config.danger_period
     except AttributeError:
         pass
     else:
         runner = periodics.DangerZoneDetector(self)
         if runner.is_enabled(self):
             runner_name = reflection.get_class_name(runner)
             runner_description = periodics.DangerZoneDetector.__doc__
             runner_trigger = cron.CronTrigger.from_crontab(
                 danger_period, timezone=self.config.tz)
             runner_id = utils.hash_pieces([
                 runner_name,
                 danger_period,
                 runner_description,
             ],
                                           max_len=8)
             scheduler.add_job(runner,
                               trigger=runner_trigger,
                               jobstore='memory',
                               name="\n".join(
                                   [runner_name, runner_description]),
                               id=runner_id,
                               coalesce=True)
예제 #3
0
 def insert_periodics(cls, bot, scheduler):
     try:
         jenkins_jobs = bot.config.jenkins.jobs
     except AttributeError:
         jenkins_jobs = {}
     try:
         if cls.job_name:
             job_config = jenkins_jobs[cls.job_name]
         else:
             job_config = {}
     except KeyError:
         job_config = {}
     slack_client = bot.clients.get("slack_client")
     slack_sender = bot.slack_sender
     if slack_client is not None and slack_sender is not None:
         for periodic in job_config.get("periodics", []):
             channel = bot.config.get('periodic_channel')
             if not channel:
                 channel = bot.config.admin_channel
             for periodic_params in _expand_params(periodic['parameters']):
                 jr = peu.make_periodic_runner(
                     "jenkins job targeting job `%s`" % cls.job_name,
                     cls,
                     periodic['period'],
                     channel=channel,
                     log=LOG,
                     args=periodic_params)
                 jr.__module__ = __name__
                 jr.__name__ = "run_jenkins_job"
                 jr_trigger = cron.CronTrigger.from_crontab(
                     periodic['period'], timezone=bot.config.tz)
                 jr_name = reflection.get_callable_name(jr)
                 jr_description = "\n".join([
                     ("Periodic jenkins job"
                      " targeting job '%s'" % cls.job_name),
                     "",
                     "With parameters:",
                     utils.prettify_yaml(periodic_params,
                                         explicit_end=False,
                                         explicit_start=False),
                 ])
                 scheduler.add_job(
                     jr,
                     trigger=jr_trigger,
                     jobstore='memory',
                     name="\n".join([jr_name, jr_description]),
                     id=utils.hash_pieces(
                         [periodic['period'], jr_name, jr_description],
                         max_len=8),
                     args=(bot, slack_client, slack_sender),
                     coalesce=True)
예제 #4
0
 def insert_periodics(cls, bot, scheduler):
     if not cls.periodic_config_path:
         return
     slack_client = bot.clients.get("slack_client")
     slack_sender = bot.slack_sender
     # TODO: make these optional and work without these (for say
     # when slack not connected or something...)
     if not all([slack_client, slack_sender]):
         return
     try:
         p_list = utils.dict_or_munch_extract(bot.config,
                                              cls.periodic_config_path)
     except KeyError:
         pass
     else:
         cls_name = reflection.get_class_name(cls)
         runs_what = "handler '%s'" % cls_name
         for i, p in enumerate(p_list):
             runner_channel = (p.get("channel")
                               or bot.config.get('periodic_channel')
                               or bot.config.admin_channel)
             runner = peu.make_periodic_runner(runs_what,
                                               cls,
                                               p.period,
                                               runner_channel,
                                               log=LOG)
             runner_trigger = cron.CronTrigger.from_crontab(
                 p.period, timezone=bot.config.tz)
             runner.__module__ = getattr(cls, '__module__', __name__)
             runner_name = p.get("name", cls_name + ".run()")
             runner_description = p.get("description") or "\n".join([
                 "Periodic run of %s" % runs_what,
                 "",
                 "To channel: %s" % runner_channel,
                 "",
                 "With period: %s" % p.period,
             ])
             runner_id = p.get("id") or utils.hash_pieces(
                 [runner_name, runner_description, p.period,
                  str(i)],
                 max_len=8)
             scheduler.add_job(runner,
                               trigger=runner_trigger,
                               jobstore='memory',
                               coalesce=True,
                               name="\n".join(
                                   [runner_name, runner_description]),
                               id=runner_id,
                               args=(bot, slack_client, slack_sender))
예제 #5
0
    def insert_periodics(cls, bot, scheduler):
        def refresh_projects(jira_client):
            """Periodic loads and caches jira projects."""
            try:
                projects = jira_client.projects()
            except jira.JIRAError:
                LOG.warn("Failed fetching jira projects", exc_info=True)
            else:
                projects_matchers = []
                for p in projects:
                    p_key = p.key
                    if not p_key:
                        p_matcher = None
                    else:
                        p_matcher = re.compile(re.escape(p_key) + r"[-]\d+")
                    projects_matchers.append(p_matcher)
                cls.cache = (projects, projects_matchers)

        try:
            jira_client = bot.clients.jira_client
        except AttributeError:
            pass
        else:
            refresh_projects_name = reflection.get_callable_name(
                refresh_projects)
            refresh_projects_description = refresh_projects.__doc__
            scheduler.add_job(
                refresh_projects,
                trigger=cron.CronTrigger.from_crontab("*/10 * * * *",
                                                      timezone=bot.config.tz),
                args=(jira_client, ),
                jobstore='memory',
                name="\n".join(
                    [refresh_projects_name, refresh_projects_description]),
                # Run right when scheduler starts up...
                next_run_time=bot.date_wrangler.get_now(),
                id=utils.hash_pieces(
                    [refresh_projects_name, refresh_projects_description],
                    max_len=8),
                coalesce=True)
예제 #6
0
    def insert_periodics(bot, scheduler):
        try:
            ping_period = bot.config.slack.ping_period
            slack_client = bot.clients.slack_client
        except AttributeError:
            pass
        else:

            def ping_slack_rtm(slack_client):
                try:
                    rtm_connected = getattr(slack_client, 'rtm_connected')
                except AttributeError:
                    rtm_connected = False
                if rtm_connected and slack_client.server:
                    slack_client.server.ping()
                else:
                    LOG.warn("Slack RTM not connected, ping skipped")

            ping_slack_rtm.__doc__ = ('Periodically pings slack'
                                      ' via RTM/websocket'
                                      ' channel (to ensure they know we'
                                      ' are alive).')
            ping_slack_rtm_name = reflection.get_callable_name(ping_slack_rtm)
            ping_slack_rtm_description = ('Periodically pings slack'
                                          ' via RTM/websocket'
                                          ' channel (to ensure they know we'
                                          ' are alive).')
            scheduler.add_job(
                ping_slack_rtm,
                trigger=cron.CronTrigger.from_crontab(ping_period,
                                                      timezone=bot.config.tz),
                jobstore='memory',
                name="\n".join(
                    [ping_slack_rtm_name, ping_slack_rtm_description]),
                id=utils.hash_pieces(
                    [ping_slack_rtm_name, ping_slack_rtm_description],
                    max_len=8),
                args=(slack_client, ),
                coalesce=True)