OLD | NEW |
---|---|
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 """Out of band HTTP push.""" | 4 """Out of band HTTP push.""" |
5 | 5 |
6 import Queue | 6 import Queue |
7 import json | 7 import json |
8 import logging | 8 import logging |
9 import os | 9 import os |
10 import threading | 10 import threading |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
52 with self.lock: | 52 with self.lock: |
53 with open(os.path.join('workdir', 'events.json'), 'w') as f: | 53 with open(os.path.join('workdir', 'events.json'), 'w') as f: |
54 json.dump(self.queue, f, indent=2) | 54 json.dump(self.queue, f, indent=2) |
55 | 55 |
56 def send(self, pending, packet): | 56 def send(self, pending, packet): |
57 with self.lock: | 57 with self.lock: |
58 self.queue.append(self._package(pending, packet)) | 58 self.queue.append(self._package(pending, packet)) |
59 | 59 |
60 | 60 |
61 class AsyncPush(AsyncPushNoop): | 61 class AsyncPush(AsyncPushNoop): |
62 """Sends HTTP Post in a background worker thread. | 62 """Sends HTTP Post asynchronously to the tree status application. |
63 | 63 |
64 Thread-safe. | 64 This object uses a background worker thread, and is thread-safe. |
65 """ | 65 """ |
66 _TERMINATE = object() | 66 _TERMINATE = object() |
67 | 67 |
68 def __init__(self, url, password): | 68 def __init__(self, url, password): |
69 super(AsyncPush, self).__init__() | 69 super(AsyncPush, self).__init__() |
70 assert url | 70 assert url |
71 assert password | 71 assert password |
72 self.url = url | 72 self.url = url |
73 self.password = password | 73 self.password = password |
74 self.queue = Queue.Queue() | 74 self.queue = Queue.Queue() |
(...skipping 29 matching lines...) Expand all Loading... | |
104 'Accept': 'text/plain' | 104 'Accept': 'text/plain' |
105 } | 105 } |
106 done = False | 106 done = False |
107 try: | 107 try: |
108 while not done: | 108 while not done: |
109 items = self._get_items() | 109 items = self._get_items() |
110 if self._TERMINATE in items: | 110 if self._TERMINATE in items: |
111 done = True | 111 done = True |
112 logging.debug('Worker thread exiting') | 112 logging.debug('Worker thread exiting') |
113 items.remove(self._TERMINATE) | 113 items.remove(self._TERMINATE) |
114 url = self.url + '/receiver' | 114 url = self.url + '/receiver' # application-specific |
Paweł Hajdan Jr.
2014/01/06 11:23:26
nit: It's not obvious what this comment adds.
I'd
| |
115 logging.debug('Sending %d items to %s' % (len(items), url)) | 115 logging.debug('Sending %d items to %s' % (len(items), url)) |
116 try: | 116 try: |
117 data = [('p', json.dumps(item)) for item in items] | 117 data = [('p', json.dumps(item)) for item in items] |
118 data.append(('password', self.password)) | 118 data.append(('password', self.password)) |
119 urllib.urlopen(url, urllib.urlencode(data), params).read() | 119 urllib.urlopen(url, urllib.urlencode(data), params).read() |
120 except IOError as e: | 120 except IOError as e: |
121 logging.error(e) | 121 logging.error(e) |
122 for item in items: | 122 for item in items: |
123 self.queue.put(item) | 123 self.queue.put(item) |
124 if not done: | 124 if not done: |
125 time.sleep(1) | 125 time.sleep(1) |
126 # Don't retry if done. | 126 # Don't retry if done. |
127 except Exception as e: | 127 except Exception as e: |
128 traceback.print_exc() | 128 traceback.print_exc() |
129 errors.send_stack(e) | 129 errors.send_stack(e) |
OLD | NEW |