Exemplo n.º 1
0
def format_job(job):
    """Turns job (from `format_scheduler`) -> slack attachment."""
    attachment_fields = [
        {
            'title': "Entrypoint",
            "value": job.name,
            'short': utils.is_short(job.name),
        },
        {
            "title": "State",
            "value": job.state,
            "short": True,
        },
        {
            "title": "Trigger",
            "short": utils.is_short(job.trigger),
            "value": job.trigger,
        },
    ]
    if job.runs_in is not None:
        job_runs_in = utils.format_seconds(job.runs_in)
        attachment_fields.append({
            "title": "Runs in",
            "value": job_runs_in,
            "short": utils.is_short(job_runs_in),
        })
    return {
        "pretext": u"• Job `%s`" % (job.id),
        'mrkdwn_in': ['pretext'],
        'text': job.description,
        'fields': attachment_fields,
    }
Exemplo n.º 2
0
 def report_on_it(dead, ara_reports_url,
                  gists, gist_mapping,
                  stdout_path, stderr_path):
     started_at = timeutils.now()
     last_report = timeutils.now()
     last_gist_update = timeutils.now()
     emitted_ara = False
     while not dead.is_set():
         now = timeutils.now()
         secs_since_last_report = now - last_report
         if secs_since_last_report >= self.report_period:
             if not emitted_ara and ara_reports_url:
                 replier("Progress can be"
                         " watched at: %s" % ara_reports_url)
                 emitted_ara = True
             if pbar is not None:
                 pbar.update(
                     'Your playbook has been running for'
                     ' %s...' % utils.format_seconds(now - started_at))
             last_report = now
         secs_since_last_gist_update = now - last_gist_update
         if secs_since_last_gist_update >= self.gist_update_period:
             update_or_create_gist(gists, gist_mapping,
                                   stdout_path, stderr_path)
             last_gist_update = now
         dead.wait(min([self.gist_update_period, self.report_period]))
Exemplo n.º 3
0
 def _format_handler(handler):
     h_message_kind = reflection.get_class_name(handler.message)
     h_state = handler.state
     h_fields = [
         {
             'title': 'State',
             'value': h_state,
             'short': utils.is_short(h_state),
         },
         {
             'title': 'Message type',
             'value': h_message_kind,
             'short': utils.is_short(h_message_kind),
         },
     ]
     h_elapsed = None
     try:
         h_elapsed = handler.watch.elapsed()
     except RuntimeError:
         pass
     if h_elapsed is not None:
         h_elapsed = utils.format_seconds(h_elapsed)
         h_fields.append({
             'title': 'Elapsed',
             'value': h_elapsed,
             'short': utils.is_short(h_elapsed),
         })
     try:
         h_started_user = handler.message.body.user_name
     except AttributeError:
         pass
     else:
         if h_started_user:
             h_fields.append({
                 'title': 'Started by',
                 'value': h_started_user,
                 'short': utils.is_short(h_started_user),
             })
     try:
         h_quick_link = handler.message.body.quick_link
     except AttributeError:
         pass
     else:
         if h_quick_link:
             h_fields.append({
                 'title': 'Link',
                 'value': h_quick_link,
                 'short': utils.is_short(h_quick_link),
             })
     return {
         "pretext": u"• Handler `%s`" % reflection.get_class_name(handler),
         'mrkdwn_in': ['pretext'],
         'fields': h_fields,
     }
Exemplo n.º 4
0
 def _run(self, **kwargs):
     started_at = self.bot.started_at
     replier = self.message.reply_text
     if started_at is None:
         replier(
             "I am not alive, how are you sending this?",
             threaded=True, prefixed=False)
     else:
         now = self.date_wrangler.get_now()
         diff = now - started_at
         replier(
             "I have been alive"
             " for %s." % utils.format_seconds(diff.total_seconds()),
             threaded=True, prefixed=False)
Exemplo n.º 5
0
 def _run(self, **kwargs):
     me = pkg_resources.get_distribution('padre')
     bot_name = self.bot.name
     if not bot_name:
         bot_name = "???"
     started_at = self.bot.started_at
     now = self.date_wrangler.get_now()
     diff = now - started_at
     total_secs = int(max(0, diff.total_seconds()))
     attachments = [
         {
             'pretext': "I am known as `%s`." % bot_name,
             'mrkdwn_in': ['pretext'],
         },
         {
             'pretext': "I am %s version `%s`." % (me.key, me.version),
             'mrkdwn_in': ['pretext'],
         },
         {
             'pretext': "I have been alive for"
                        " %s." % utils.format_seconds(total_secs),
             'mrkdwn_in': [],
         },
     ]
     active_handlers = list(self.bot.active_handlers)
     if active_handlers:
         handlers_text = '`%s` handler' % len(active_handlers)
         if len(active_handlers) >= 2:
             handlers_text += "s"
         handlers_text += ":"
     else:
         handlers_text = 'no handlers.'
     attachments.append({
         'pretext': "I am running %s" % handlers_text,
         'mrkdwn_in': ['pretext'],
     })
     for h in active_handlers:
         attachments.append(self._format_handler(h))
     self.message.reply_attachments(
         attachments=attachments,
         log=LOG, link_names=False,
         as_user=True, text=random.choice(self.hello_messages),
         thread_ts=self.message.body.ts,
         channel=self.message.body.channel,
         unfurl_links=False)
Exemplo n.º 6
0
def _format_pr_fields(gh_pr, now=None):
    fields = [
        {
            "title": "State",
            "value": str(gh_pr.state.title()),
            "short": True,
        },
        {
            "title": "Mergeable",
            "value": str(gh_pr.mergeable),
            "short": True,
        },
    ]
    if gh_pr.additions > 0:
        add_val = "+" + str(gh_pr.additions)
    else:
        add_val = "0"
    if gh_pr.deletions > 0:
        del_val = "-" + str(gh_pr.deletions)
    else:
        del_val = "0"
    fields.extend([
        {
            "title": "Additions",
            "value": add_val,
            "short": True,
        },
        {
            "title": "Deletions",
            "value": del_val,
            "short": True,
        },
    ])
    if now is not None:
        created_at_diff = now - gh_pr.created_at
        created_at_diff_secs = created_at_diff.total_seconds()
        created_at = utils.format_seconds(created_at_diff_secs)
        fields.append({
            'title': 'Age',
            'value': created_at,
            "short": utils.is_short(created_at),
        })
    return sorted(fields, key=lambda v: v['title'].lower())
Exemplo n.º 7
0
def _done_listener(scheduler, event):
    job_id = event.job_id
    job = scheduler.get_job(job_id, jobstore=event.jobstore)
    if job is None:
        job_name = "???"
        job_next = None
    else:
        job_name = job.name.splitlines()[0]
        now = du.get_now(tz=scheduler.timezone)
        job_next_diff = job.next_run_time - now
        job_next = utils.format_seconds(job_next_diff.total_seconds())
    if event.exception is not None:
        LOG.error("Failed run of job '%s' [%s]",
                  job_id,
                  job_name,
                  exc_info=True)
    else:
        LOG.debug("Happily ran job '%s' [%s]", job_id, job_name)
    if job_next is not None:
        LOG.debug("Job '%s' [%s] will"
                  " run again in %s", job_id, job_name, job_next)
Exemplo n.º 8
0
    def test_elapsed(self):
        secs_elapsed = 0
        self.assertEqual("0 seconds", utils.format_seconds(secs_elapsed))

        secs_elapsed = 1
        self.assertEqual("1 second", utils.format_seconds(secs_elapsed))

        secs_elapsed = 60 * 60 * 24 * 7 * 52
        self.assertEqual("1 year", utils.format_seconds(secs_elapsed))

        secs_elapsed = 60 * 60 * 24 * 7 * 52 * 2
        self.assertEqual("2 years", utils.format_seconds(secs_elapsed))

        secs_elapsed = (60 * 60 * 24 * 7 * 52 * 2) + 1
        self.assertEqual("2 years and 1 second",
                         utils.format_seconds(secs_elapsed))

        secs_elapsed = (60 * 60 * 24 * 7 * 52 * 1) + (60 * 60) + 60 + 1
        self.assertEqual('1 year, 1 hour, 1 minute and 1 second',
                         utils.format_seconds(secs_elapsed))

        secs_elapsed = (60 * 60 * 24 * 7 * 52 * 1) + (60 * 60) + 180 + 1
        self.assertEqual('1 year, 1 hour, 3 minutes and 1 second',
                         utils.format_seconds(secs_elapsed))

        secs_elapsed = (60 * 60 * 24 * 7 * 52 * 2) + (60 * 60) + 180 + 1
        self.assertEqual('2 years, 1 hour, 3 minutes and 1 second',
                         utils.format_seconds(secs_elapsed))

        secs_elapsed = 60 * 60 * 24 * 7
        self.assertEqual("1 week", utils.format_seconds(secs_elapsed))

        secs_elapsed = 60 * 60 * 24 * 7 * 4
        self.assertEqual("4 weeks", utils.format_seconds(secs_elapsed))

        secs_elapsed = (60 * 60 * 24 * 7) + (60 * 60 * 24)
        self.assertEqual("1 week and 1 day",
                         utils.format_seconds(secs_elapsed))

        secs_elapsed = 60 * 60 * 24
        self.assertEqual("1 day", utils.format_seconds(secs_elapsed))

        secs_elapsed = 60 * 60 * 24 * 2
        self.assertEqual("2 days", utils.format_seconds(secs_elapsed))

        secs_elapsed = 60 * 60 * 24 + (60 * 60 * 2 + 61)
        self.assertEqual("1 day, 2 hours, 1 minute and 1 second",
                         utils.format_seconds(secs_elapsed))

        secs_elapsed = 60 * 60
        self.assertEqual("1 hour", utils.format_seconds(secs_elapsed))

        secs_elapsed = 60 * 60 * 2
        self.assertEqual("2 hours", utils.format_seconds(secs_elapsed))

        secs_elapsed = 60
        self.assertEqual("1 minute", utils.format_seconds(secs_elapsed))

        secs_elapsed = 60 * 2
        self.assertEqual("2 minutes", utils.format_seconds(secs_elapsed))

        secs_elapsed = 61
        self.assertEqual("1 minute and 1 second",
                         utils.format_seconds(secs_elapsed))

        secs_elapsed = 60 * 60 + 61
        self.assertEqual("1 hour, 1 minute and 1 second",
                         utils.format_seconds(secs_elapsed))