| OLD | NEW |
| (Empty) |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 """Out of band HTTP push.""" | |
| 5 | |
| 6 import Queue | |
| 7 import json | |
| 8 import logging | |
| 9 import os | |
| 10 import threading | |
| 11 import time | |
| 12 import traceback | |
| 13 import urllib | |
| 14 | |
| 15 import errors | |
| 16 from verification import base | |
| 17 | |
| 18 | |
| 19 class AsyncPushNoop(object): | |
| 20 url = 'http://localhost' | |
| 21 def close(self): | |
| 22 pass | |
| 23 | |
| 24 def send(self, pending, packet): | |
| 25 pass | |
| 26 | |
| 27 @staticmethod | |
| 28 def _package(pending, packet): | |
| 29 data = { | |
| 30 'done': pending.get_state() not in (base.PROCESSING, base.IGNORED), | |
| 31 'issue': pending.issue, | |
| 32 'owner': pending.owner, | |
| 33 'patchset': pending.patchset, | |
| 34 'timestamp': time.time(), | |
| 35 } | |
| 36 if packet: | |
| 37 data.update(packet) | |
| 38 return data | |
| 39 | |
| 40 | |
| 41 class AsyncPushStore(AsyncPushNoop): | |
| 42 """Saves all the events into workdir/events.json for later analysis. | |
| 43 | |
| 44 Thread-safe. | |
| 45 """ | |
| 46 def __init__(self): | |
| 47 super(AsyncPushStore, self).__init__() | |
| 48 self.lock = threading.Lock() | |
| 49 self.queue = [] | |
| 50 | |
| 51 def close(self): | |
| 52 with self.lock: | |
| 53 with open(os.path.join('workdir', 'events.json'), 'w') as f: | |
| 54 json.dump(self.queue, f, indent=2) | |
| 55 | |
| 56 def send(self, pending, packet): | |
| 57 with self.lock: | |
| 58 self.queue.append(self._package(pending, packet)) | |
| 59 | |
| 60 | |
| 61 class AsyncPush(AsyncPushNoop): | |
| 62 """Sends HTTP Post asynchronously to the tree status application. | |
| 63 | |
| 64 This object uses a background worker thread, and is thread-safe. | |
| 65 """ | |
| 66 _TERMINATE = object() | |
| 67 | |
| 68 def __init__(self, url, password, resource='/receiver'): | |
| 69 super(AsyncPush, self).__init__() | |
| 70 assert url | |
| 71 assert password | |
| 72 self.url = url | |
| 73 self.resource = resource | |
| 74 self.password = password | |
| 75 self.queue = Queue.Queue() | |
| 76 self.thread = threading.Thread(target=self._worker_thread) | |
| 77 self.thread.daemon = True | |
| 78 self.thread.start() | |
| 79 | |
| 80 def close(self): | |
| 81 self.queue.put(self._TERMINATE) | |
| 82 self.thread.join() | |
| 83 | |
| 84 def send(self, pending, packet): | |
| 85 """Queues a packet.""" | |
| 86 logging.debug('For issue %d, queuing for send: %s', pending.issue, packet) | |
| 87 self.queue.put(self._package(pending, packet)) | |
| 88 | |
| 89 def _get_items(self): | |
| 90 """Waits for an item to be queued and returns up to 10 next items if queued | |
| 91 fast enough. | |
| 92 """ | |
| 93 items = [self.queue.get()] | |
| 94 try: | |
| 95 for _ in range(9): | |
| 96 items.append(self.queue.get_nowait()) | |
| 97 except Queue.Empty: | |
| 98 pass | |
| 99 return items | |
| 100 | |
| 101 def _worker_thread(self): | |
| 102 """Sends the packets in a loop through HTTP POST.""" | |
| 103 params = { | |
| 104 'Content-type': 'application/x-www-form-urlencoded', | |
| 105 'Accept': 'text/plain' | |
| 106 } | |
| 107 done = False | |
| 108 try: | |
| 109 while not done: | |
| 110 items = self._get_items() | |
| 111 if self._TERMINATE in items: | |
| 112 done = True | |
| 113 logging.debug('Worker thread exiting') | |
| 114 items.remove(self._TERMINATE) | |
| 115 url = self.url + self.resource | |
| 116 logging.debug('Sending %d items to %s: %r', len(items), url, items) | |
| 117 try: | |
| 118 data = [('p', json.dumps(item)) for item in items] | |
| 119 data.append(('password', self.password)) | |
| 120 urllib.urlopen(url, urllib.urlencode(data), params).read() | |
| 121 except IOError as e: | |
| 122 logging.error(e) | |
| 123 for item in items: | |
| 124 self.queue.put(item) | |
| 125 if not done: | |
| 126 time.sleep(1) | |
| 127 # Don't retry if done. | |
| 128 except Exception as e: | |
| 129 traceback.print_exc() | |
| 130 errors.send_stack(e) | |
| OLD | NEW |