| OLD | NEW |
| (Empty) | |
| 1 #!/usr/bin/python |
| 2 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer |
| 7 import logging |
| 8 import logging.handlers |
| 9 from os import path |
| 10 import subprocess |
| 11 import sys |
| 12 import threading |
| 13 import time |
| 14 import urllib2 |
| 15 |
| 16 import uploader_mail |
| 17 |
| 18 # How many seconds to wait after a cycle. |
| 19 CYCLE_DELAY = 1800 |
| 20 |
| 21 # Verifies a commit this long ago has been uploaded to devtools-frontend appspot |
| 22 CHECKER_DELAY = "12 hours" |
| 23 |
| 24 CYCLES_PER_24_HOURS = (86400 / CYCLE_DELAY) |
| 25 |
| 26 CHECKER_PATH = path.join(path.dirname(path.abspath(__file__)), '..', '..') |
| 27 CHROMIUM_CHECKOUT_PATH = path.join(CHECKER_PATH, 'src') |
| 28 LOG_PATH = path.join(CHECKER_PATH, 'logs', 'checker.log') |
| 29 |
| 30 consecutive_success = 0 |
| 31 |
| 32 def main(): |
| 33 logfile = init_logger() |
| 34 start_uptime_check_server() |
| 35 while True: |
| 36 logfile.doRollover() |
| 37 logging.info('Starting iteration cycle') |
| 38 call(['git', 'pull', 'origin', 'master']) |
| 39 yesterday_commit_hash = call(['git', 'rev-list', '-n1', '--before={}'.format
(CHECKER_DELAY), '--first-parent', 'HEAD']) |
| 40 check_commit(yesterday_commit_hash.strip()) |
| 41 logging.info('Finished iteration cycle') |
| 42 time.sleep(CYCLE_DELAY) |
| 43 |
| 44 def init_logger(): |
| 45 logger = logging.getLogger() |
| 46 logger.setLevel(logging.INFO) |
| 47 |
| 48 console = logging.StreamHandler() |
| 49 console.setLevel(logging.INFO) |
| 50 logger.addHandler(console) |
| 51 |
| 52 logfile = logging.handlers.RotatingFileHandler(LOG_PATH, backupCount=30) |
| 53 logfile.setLevel(logging.INFO) |
| 54 logger.addHandler(logfile) |
| 55 |
| 56 formatter = logging.Formatter('%(asctime)s:%(message)s', |
| 57 datefmt='%Y-%m-%d %H:%M:%S') |
| 58 console.setFormatter(formatter) |
| 59 logfile.setFormatter(formatter) |
| 60 return logfile |
| 61 |
| 62 def check_commit(commit_hash): |
| 63 global consecutive_success |
| 64 url = 'https://chrome-devtools-frontend.appspot.com/serve_file/@{}/inspector.h
tml'.format(commit_hash) |
| 65 request = urllib2.Request(url) |
| 66 try: |
| 67 response = urllib2.urlopen(request) |
| 68 summary = format_summary('Success', commit_hash, response.getcode()) |
| 69 logging.info(summary) |
| 70 # Send an email on first success after start-up or getting an error |
| 71 if consecutive_success == 0: |
| 72 send_email(subject=summary, body='Response body:\n' + response.read()) |
| 73 consecutive_success += 1 |
| 74 if consecutive_success >= CYCLES_PER_24_HOURS: |
| 75 consecutive_success = 0 |
| 76 except urllib2.HTTPError as error: |
| 77 summary = format_summary('Error', commit_hash, error.code) |
| 78 logging.info(summary) |
| 79 send_email(subject=summary, body='Error message:\n' + error.reason) |
| 80 consecutive_success = 0 |
| 81 |
| 82 def send_email(subject, body): |
| 83 mail_config = uploader_mail.ParseMailConfig(call( |
| 84 ['gsutil', 'cat', 'gs://chrome-devtools-frontend/mail_config'], log_output
=False)) |
| 85 uploader_mail.SendMail(mail_config, subject, body) |
| 86 logging.info('Sent email with subject: {} and body: {}'.format(subject, body)) |
| 87 |
| 88 def call(args, log_output=True, cwd=CHROMIUM_CHECKOUT_PATH): |
| 89 process = subprocess.Popen(args, stdout=subprocess.PIPE, |
| 90 stderr=subprocess.STDOUT, cwd=cwd) |
| 91 out, err = process.communicate() |
| 92 if process.returncode != 0: |
| 93 logging.info('Error {} from {}'.format(process.returncode, args)) |
| 94 if log_output: |
| 95 logging.info(out) |
| 96 return out |
| 97 |
| 98 def format_summary(status, commit_hash, status_code): |
| 99 return '[devtools-frontend-checker] {} for commit {} - HTTP status {}'.format( |
| 100 status, commit_hash, status_code) |
| 101 |
| 102 def start_uptime_check_server(): |
| 103 class Handler(BaseHTTPRequestHandler): |
| 104 def do_GET(self): |
| 105 self.send_response(200) |
| 106 self.send_header('Content-Type', 'text/html') |
| 107 self.end_headers() |
| 108 server = HTTPServer(('', 80), Handler) |
| 109 thread = threading.Thread(target=server.serve_forever) |
| 110 thread.daemon = True |
| 111 thread.start() |
| 112 |
| 113 if __name__ == '__main__': |
| 114 sys.exit(main()) |
| OLD | NEW |