-
Notifications
You must be signed in to change notification settings - Fork 3
/
EventFetcher.py
executable file
·283 lines (248 loc) · 7.33 KB
/
EventFetcher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#! /usr/bin/env python
import pycurl # We need to talk to curl
import pyev # Our event model
import signal # For sigint, etc.
import socket # We'll create a socket for curl so we can watch it
import time # For some sleeping
import urlparse # To efficiently parse URLs
import logging
from cStringIO import StringIO # To fake file descriptors into strings
logger = logging.getLogger('downpour')
logger.addHandler(logging.NullHandler())
class Request(object):
MAX_RETRIES = 5
def __init__(self, url):
logger.debug('Request object for %s' % url)
self.url = url
self.fetcher = None
self.loop = None
self.sock = None
self.watcher = None
# Backoff params
self.retries = 0
self.scale = 2
def __del__(self):
logger.debug('Request for %s destroyed.' % self.url)
try:
self.watcher.stop()
self.watcher = None
except AttributeError:
pass
################
# Override these
################
def onSuccess(self, c, contents):
logger.debug('Successfully fetched %s' % self.url)
def onError(self, c, contents):
logger.debug('Error fetching %s' % self.url)
################
# The actual callbacks
################
def success(self, c, contents):
self.onSuccess(c, contents)
def error(self, c, errmsg):
self.onError(c, errmsg)
#################
# Curl callbacks
#################
def socket(self, family, socktype, protocol):
'''Undocumented in stupid pycurl documentation. Found in test scripts'''
logger.debug('Watching socket for %s' % self.url)
self.sock = socket.socket(family, socktype, protocol)
self.watcher = pyev.Io(self.sock.fileno(), pyev.EV_READ | pyev.EV_WRITE, self.loop, self.io)
self.watcher.start()
return self.sock
#################
# libev callbacks
#################
def io(self, watcher, revents):
#logger.debug('IO Event for %s' % self.url)
#self.watcher.stop()
self.fetcher.check()
#self.watcher.set(self.sock, pyev.EV_READ | pyev.EV_WRITE)
#self.watcher.start()
class Fetcher(object):
def __init__(self, poolSize=10):
self.multi = pycurl.CurlMulti()
self.multi.handles = []
self.multi.setopt(pycurl.M_SOCKETFUNCTION, self.socket)
self.multi.setopt(pycurl.M_TIMERFUNCTION, self.rescheduleTimer)
self.share = pycurl.CurlShare()
self.share.setopt(pycurl.SH_SHARE, pycurl.LOCK_DATA_DNS)
self.poolSize = poolSize
self.loop = pyev.default_loop()
self.queue = []
self.numInFlight = 0
for i in range(self.poolSize):
c = pycurl.Curl()
c.timeout = None
c.setopt(pycurl.FOLLOWLOCATION, 1)
c.setopt(pycurl.MAXREDIRS, 5)
c.setopt(pycurl.CONNECTTIMEOUT, 15)
c.setopt(pycurl.TIMEOUT, 300)
c.setopt(pycurl.NOSIGNAL, 1)
c.setopt(pycurl.FRESH_CONNECT, 1)
c.setopt(pycurl.FORBID_REUSE, 1)
c.setopt(pycurl.SHARE, self.share)
self.multi.handles.append(c)
self.pool = self.multi.handles[:]
SIGSTOP = (signal.SIGPIPE, signal.SIGINT, signal.SIGTERM)
self.watchers = [pyev.Signal(sig, self.loop, self.signal) for sig in SIGSTOP]
self.evTimer = pyev.Timer(0, 1.0, self.loop, self.timer)
self.timeoutTimer = pyev.Timer(60.0, 60.0, self.loop, self.checkTimeouts)
#self.watchers.append(pyev.Idle(self.loop, self.idle))
#################
# Inheritance Interface
#################
def __len__(self):
return self.numInFlight + len(self.queue)
def extend(self, requests):
self.queue.extend(requests)
self.checkQueue()
def pop(self):
'''Get the next request'''
return self.queue.pop(0)
def push(self, r):
'''Queue a request'''
self.queue.append(r)
self.checkQueue()
#################
# libev callbacks
#################
def idle(self, watcher, revents):
self.multi.perform()
time.sleep(0.1)
def signal(self, watcher, revents):
self.stop()
def timer(self, watcher, revents):
self.evTimer.stop()
logger.debug('Timer fired!')
self.multi.perform()
self.evTimer.start()
def checkTimeouts(self, watcher, revents):
now = time.time()
for c in self.multi.handles:
if c.timeout and c.timeout < now:
logger.debug('Application timeout')
self.multi.socket_action(pycurl.SOCKET_TIMEOUT, 0)
break
#################
# Service
#################
def start(self):
logger.info('Starting...')
self.checkQueue()
for watcher in self.watchers:
watcher.start()
self.evTimer.start()
self.timeoutTimer.start()
self.loop.start()
def stop(self):
logger.info('Stopping')
self.loop.stop(pyev.EVBREAK_ALL)
while self.watchers:
self.watchers.pop().stop()
self.evTimer.stop()
self.timeoutTimer.stop()
self.loop.stop()
#################
# Iheritable
#################
def onSuccess(self, c):
pass
def onError(self, c):
pass
def onDone(self, c):
pass
#################
# Internal callbacks
#################
def socket(self, a, b, c, d):
logger.debug('Socket action')
def success(self, c):
c.request.success(c, c.fp.getvalue())
self.onSuccess(c)
self.done(c)
#################
# libcurl callbacks
#################
def rescheduleTimer(self, timeout):
if timeout > 0:
self.evTimer.stop()
self.evTimer.set(timeout / 1000.0, 0)
self.evTimer.start()
else:
self.evTimer.reset()
def error(self, c, errno, errmsg):
logger.debug('%s : (%i) %s' % (c.request.url, errno, errmsg))
if c.request.retries <= c.request.MAX_RETRIES:
t = c.request.scale ** c.request.retries
logger.debug('Request for %s sleeping %i' % (c.request.url, t))
pyev.sleep(t)
c.fp.close()
c.fp = StringIO()
c.setopt(pycurl.WRITEFUNCTION, c.fp.write)
self.multi.remove_handle(c)
self.multi.add_handle(c)
else:
c.request.error(c, c.errstr())
self.onError(c)
self.done(c)
def done(self, c):
logger.debug('Done with %s' % c.request.url)
self.onDone(c)
c.fp.close()
c.fp = None
c.timeout = 0
self.pool.append(c)
self.numInFlight -= 1
self.multi.remove_handle(c)
self.checkQueue()
def check(self):
if self.multi.perform():
numQ, okList, errList = self.multi.info_read()
for c in okList:
# Handle successulf
self.success(c)
for c, errno, errmsg in errList:
# Handle failed
self.error(c, errno, errmsg)
self.evTimer.reset()
def checkQueue(self):
while len(self.queue) and len(self.pool):
logger.info('Remaining in queue: %i' % len(self.queue))
logger.debug('Pool: %i' % len(self.pool))
# If we have requests and handles to spare,
req = self.pop()
if req == None:
break
req.fetcher = self
req.loop = self.loop
c = self.pool.pop()
c.request = req
c.fp = StringIO()
c.setopt(pycurl.URL, c.request.url)
c.setopt(pycurl.HTTPHEADER, ['Host: %s' % urlparse.urlparse(req.url).hostname])
# When you create a socket, we must register it!
c.setopt(pycurl.OPENSOCKETFUNCTION, c.request.socket)
c.setopt(pycurl.WRITEFUNCTION, c.fp.write)
self.numInFlight += 1
self.multi.add_handle(c)
if c.timeout == None:
logger.debug('Kickstarting...')
self.multi.socket_action(pycurl.SOCKET_TIMEOUT, 0)
c.timeout = time.time() + 15.0
self.multi.perform()
if __name__ == '__main__':
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler = logging.StreamHandler()
handler.setLevel(logging.DEBUG)
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
f = file('urls.txt')
urls = f.read().strip().split()
f.close()
f = Fetcher(20)
f.extend(Request(url) for url in urls)
f.start()