Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/python | |
| 2 # Copyright (c) 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 | |
|
dgozman
2016/09/22 16:42:54
Why this? Ain't consecutive_success already visibl
chenwilliam
2016/09/22 20:25:53
It's visible but consecutive_success += 1 causes t
| |
| 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 send_email(subject=summary, body='Response body:\n' +response.read()) | |
| 76 consecutive_success = 0 | |
| 77 except urllib2.HTTPError as error: | |
| 78 summary = format_summary('Error', commit_hash, error.code) | |
| 79 logging.info(summary) | |
| 80 send_email(subject=summary, body='Error message:\n' + error.reason) | |
| 81 consecutive_success = 0 | |
| 82 | |
| 83 def send_email(subject, body): | |
| 84 mail_config = uploader_mail.ParseMailConfig(call( | |
| 85 ['gsutil', 'cat', 'gs://chrome-devtools-frontend/mail_config'], log_output =False)) | |
| 86 uploader_mail.SendMail(mail_config, subject, body) | |
| 87 logging.info('Sent email with subject: {} and body: {}'.format(subject, body)) | |
| 88 | |
| 89 def call(args, log_output=True, cwd=CHROMIUM_CHECKOUT_PATH): | |
| 90 process = subprocess.Popen(args, stdout=subprocess.PIPE, | |
| 91 stderr=subprocess.STDOUT, cwd=cwd) | |
| 92 out, err = process.communicate() | |
| 93 if process.returncode != 0: | |
| 94 logging.info('Error {} from {}'.format(process.returncode, args)) | |
| 95 if log_output: | |
| 96 logging.info(out) | |
| 97 return out | |
| 98 | |
| 99 def format_summary(status, commit_hash, status_code): | |
| 100 return '[devtools-frontend-checker] {} for commit {} - HTTP status {}'.format( | |
| 101 status, commit_hash, status_code) | |
| 102 | |
| 103 def start_uptime_check_server(): | |
| 104 class Handler(BaseHTTPRequestHandler): | |
| 105 def do_GET(self): | |
| 106 self.send_response(200) | |
| 107 self.send_header('Content-Type', 'text/html') | |
| 108 self.end_headers() | |
| 109 server = HTTPServer(('', 80), Handler) | |
| 110 thread = threading.Thread(target=server.serve_forever) | |
| 111 thread.daemon = True | |
| 112 thread.start() | |
| 113 | |
| 114 if __name__ == '__main__': | |
| 115 sys.exit(main()) | |
| OLD | NEW |