A framework for quick web archiving
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

317 lines
11 KiB

  1. import qwarc.aiohttp
  2. from qwarc.const import *
  3. import qwarc.utils
  4. import qwarc.warc
  5. import aiohttp as _aiohttp
  6. if _aiohttp.__version__ != '2.3.10':
  7. raise ImportError('aiohttp must be version 2.3.10')
  8. import asyncio
  9. import collections
  10. import concurrent.futures
  11. import itertools
  12. import logging
  13. import os
  14. import random
  15. import sqlite3
  16. import yarl
  17. class Item:
  18. itemType = None
  19. def __init__(self, itemValue, session, headers, warc):
  20. self.itemValue = itemValue
  21. self.session = session
  22. self.headers = headers
  23. self.warc = warc
  24. self.stats = {'tx': 0, 'rx': 0, 'requests': 0}
  25. self.childItems = []
  26. async def fetch(self, url, responseHandler = qwarc.utils.handle_response_default):
  27. '''
  28. HTTP GET a URL
  29. url: str or yarl.URL
  30. responseHandler: a callable that determines how the response is handled. See qwarc.utils.handle_response_default for details.
  31. Returns response (a ClientResponse object or None) and history (a tuple of (response, exception) tuples).
  32. response can be None and history can be an empty tuple, depending on the circumstances (e.g. timeouts).
  33. '''
  34. #TODO: Rewrite using 'async with self.session.get'
  35. url = yarl.URL(url) # Explicitly convert for normalisation, percent-encoding, etc.
  36. history = []
  37. attempt = 0
  38. #TODO redirectLevel
  39. while True:
  40. attempt += 1
  41. response = None
  42. exc = None
  43. action = ACTION_RETRY
  44. writeToWarc = True
  45. try:
  46. try:
  47. with _aiohttp.Timeout(60):
  48. logging.info('Fetching {}'.format(url))
  49. response = await self.session.get(url, headers = self.headers, allow_redirects = False)
  50. try:
  51. ret = await response.text(errors = 'surrogateescape')
  52. except:
  53. # No calling the handleResponse callback here because this is really bad. The not-so-bad exceptions (e.g. an error during reading the response) will be caught further down.
  54. response.close()
  55. raise
  56. else:
  57. tx = len(response.rawRequestData)
  58. rx = len(response.rawResponseData)
  59. logging.info('Fetched {}: {} (tx {}, rx {})'.format(url, response.status, tx, rx))
  60. self.stats['tx'] += tx
  61. self.stats['rx'] += rx
  62. self.stats['requests'] += 1
  63. except (asyncio.TimeoutError, _aiohttp.ClientError) as e:
  64. logging.error('Request for {} failed: {!r}'.format(url, e))
  65. action, writeToWarc = await responseHandler(url, attempt, response, e)
  66. exc = e # Pass the exception outward for the history
  67. else:
  68. action, writeToWarc = await responseHandler(url, attempt, response, None)
  69. history.append((response, exc))
  70. if action in (ACTION_SUCCESS, ACTION_IGNORE):
  71. return response, tuple(history)
  72. elif action == ACTION_FOLLOW_OR_SUCCESS:
  73. redirectUrl = response.headers.get('Location') or response.headers.get('URI')
  74. if not redirectUrl:
  75. return response, tuple(history)
  76. url = url.join(yarl.URL(redirectUrl))
  77. attempt = 0
  78. elif action == ACTION_RETRY:
  79. # Nothing to do, just go to the next cycle
  80. pass
  81. finally:
  82. if response:
  83. if writeToWarc:
  84. self.warc.write_client_response(response)
  85. await response.release()
  86. async def process(self):
  87. raise NotImplementedError
  88. @classmethod
  89. def generate(cls):
  90. yield from () # Generate no items by default
  91. @classmethod
  92. def _gen(cls):
  93. for x in cls.generate():
  94. yield (cls.itemType, x, STATUS_TODO)
  95. def add_item(self, itemClassOrType, itemValue):
  96. if issubclass(itemClassOrType, Item):
  97. item = (itemClassOrType.itemType, itemValue)
  98. else:
  99. item = (itemClassOrType, itemValue)
  100. if item not in self.childItems:
  101. self.childItems.append(item)
  102. class QWARC:
  103. def __init__(self, itemClasses, warcBasePath, dbPath, concurrency = 1, memoryLimit = 0, minFreeDisk = 0, warcSizeLimit = 0):
  104. '''
  105. itemClasses: iterable of Item
  106. warcBasePath: str, base name of the WARC files
  107. dbPath: str, path to the sqlite3 database file
  108. concurrency: int, number of concurrently processed items
  109. memoryLimit: int, gracefully stop when the process uses more than memoryLimit bytes of RSS; 0 disables the memory check
  110. minFreeDisk: int, pause when there's less than minFreeDisk space on the partition where WARCs are written; 0 disables the disk space check
  111. warcSizeLimit: int, size of each WARC file; 0 if the WARCs should not be split
  112. '''
  113. self._itemClasses = itemClasses
  114. self._itemTypeMap = {cls.itemType: cls for cls in itemClasses}
  115. self._warcBasePath = warcBasePath
  116. self._dbPath = dbPath
  117. self._concurrency = concurrency
  118. self._memoryLimit = memoryLimit
  119. self._minFreeDisk = minFreeDisk
  120. self._warcSizeLimit = warcSizeLimit
  121. async def obtain_exclusive_db_lock(self, db):
  122. c = db.cursor()
  123. while True:
  124. try:
  125. c.execute('BEGIN EXCLUSIVE')
  126. break
  127. except sqlite3.OperationalError as e:
  128. if str(e) != 'database is locked':
  129. raise
  130. await asyncio.sleep(1)
  131. return c
  132. def _make_item(self, itemType, itemValue, session, headers, warc):
  133. try:
  134. itemClass = self._itemTypeMap[itemType]
  135. except KeyError:
  136. raise RuntimeError('No such item type: {!r}'.format(itemType))
  137. return itemClass(itemValue, session, headers, warc)
  138. async def run(self, loop):
  139. headers = [('User-Agent', 'Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0')] #TODO: Move elsewhere
  140. tasks = set()
  141. sleepTasks = set()
  142. sessions = [] # aiohttp.ClientSession instances
  143. freeSessions = collections.deque() # ClientSession instances that are currently free
  144. for i in range(self._concurrency):
  145. session = _aiohttp.ClientSession(
  146. connector = qwarc.aiohttp.TCPConnector(loop = loop),
  147. request_class = qwarc.aiohttp.ClientRequest,
  148. response_class = qwarc.aiohttp.ClientResponse,
  149. skip_auto_headers = ['Accept-Encoding'],
  150. loop = loop
  151. )
  152. sessions.append(session)
  153. freeSessions.append(session)
  154. warc = qwarc.warc.WARC(self._warcBasePath, self._warcSizeLimit)
  155. db = sqlite3.connect(self._dbPath, timeout = 1)
  156. db.isolation_level = None # Transactions are handled manually below.
  157. db.execute('PRAGMA synchronous = OFF')
  158. try:
  159. async def wait_for_free_task():
  160. nonlocal tasks, freeSessions, db, emptyTodoSleep
  161. done, pending = await asyncio.wait(tasks, return_when = concurrent.futures.FIRST_COMPLETED)
  162. for future in done:
  163. # TODO Replace all of this with `if future.cancelled():`
  164. try:
  165. await future #TODO: Is this actually necessary? asyncio.wait only returns 'done' futures...
  166. except concurrent.futures.CancelledError as e:
  167. # Got cancelled, nothing we can do about it, but let's log a warning if it's a process task
  168. if isinstance(future, asyncio.Task):
  169. if future.taskType == 'process_item':
  170. logging.warning('Task for {}:{} cancelled: {!r}'.format(future.itemType, future.itemValue, future))
  171. elif future.taskType == 'sleep':
  172. sleepTasks.remove(future)
  173. continue
  174. if future.taskType == 'sleep':
  175. # Dummy task for empty todo list, see below.
  176. sleepTasks.remove(future)
  177. continue
  178. item = future.item
  179. logging.info('{itemType}:{itemValue} done: {requests} requests, {tx} tx, {rx} rx'.format(itemType = future.itemType, itemValue = future.itemValue, **item.stats))
  180. cursor = await self.obtain_exclusive_db_lock(db)
  181. try:
  182. cursor.execute('UPDATE items SET status = ? WHERE id = ?', (STATUS_DONE, future.id))
  183. if item.childItems:
  184. it = iter(item.childItems)
  185. while True:
  186. values = [(t, v, STATUS_TODO) for t, v in itertools.islice(it, 100000)]
  187. if not values:
  188. break
  189. cursor.executemany('INSERT INTO items (type, value, status) VALUES (?, ?, ?)', values)
  190. cursor.execute('COMMIT')
  191. except:
  192. cursor.execute('ROLLBACK')
  193. raise
  194. freeSessions.append(item.session)
  195. tasks = pending
  196. while True:
  197. while len(tasks) >= self._concurrency:
  198. emptyTodoFullReached = True
  199. await wait_for_free_task()
  200. if self._minFreeDisk and too_little_disk_space(self._minFreeDisk):
  201. logging.info('Disk space is low, sleeping')
  202. sleepTask = asyncio.sleep(random.uniform(self._concurrency / 2, self._concurrency * 1.5))
  203. sleepTask.taskType = 'sleep'
  204. tasks.add(sleepTask)
  205. sleepTasks.add(sleepTask)
  206. continue
  207. cursor = await self.obtain_exclusive_db_lock(db)
  208. try:
  209. cursor.execute('SELECT id, type, value, status FROM items WHERE status = ? LIMIT 1', (STATUS_TODO,))
  210. result = cursor.fetchone()
  211. if not result:
  212. if cursor.execute('SELECT id, status FROM items WHERE status != ? LIMIT 1', (STATUS_DONE,)).fetchone():
  213. # There is currently no item to do, but there are still some in progress, so more TODOs may appear in the future.
  214. # It would be nice if we could just await wait_for_free_task() here, but that doesn't work because those TODOs might be in another process.
  215. # So instead, we insert a dummy task which just sleeps a bit. Average sleep time is equal to concurrency, i.e. one check per second.
  216. #TODO: The average sleep time is too large if there are only few sleep tasks; scale with len(sleepTasks)/self._concurrency?
  217. sleepTask = asyncio.ensure_future(asyncio.sleep(random.uniform(self._concurrency / 2, self._concurrency * 1.5)))
  218. sleepTask.taskType = 'sleep'
  219. tasks.add(sleepTask)
  220. sleepTasks.add(sleepTask)
  221. cursor.execute('COMMIT')
  222. continue
  223. else:
  224. # Really nothing to do anymore
  225. #TODO: Another process may be running create_db, in which case we'd still want to wait...
  226. # create_db could insert a dummy item which is marked as done when the DB is ready
  227. cursor.execute('COMMIT')
  228. break
  229. emptyTodoSleep = 0
  230. id, itemType, itemValue, status = result
  231. cursor.execute('UPDATE items SET status = ? WHERE id = ?', (STATUS_INPROGRESS, id))
  232. cursor.execute('COMMIT')
  233. except:
  234. cursor.execute('ROLLBACK')
  235. raise
  236. session = freeSessions.popleft()
  237. item = self._make_item(itemType, itemValue, session, headers, warc)
  238. task = asyncio.ensure_future(item.process())
  239. #TODO: Is there a better way to add custom information to a task/coroutine object?
  240. task.taskType = 'process'
  241. task.id = id
  242. task.itemType = itemType
  243. task.itemValue = itemValue
  244. task.item = item
  245. tasks.add(task)
  246. if os.path.exists('STOP'):
  247. logging.info('Gracefully shutting down due to STOP file')
  248. break
  249. if self._memoryLimit and uses_too_much_memory(self._memoryLimit):
  250. logging.info('Gracefully shutting down due to memory usage (current = {} > limit = {})'.format(get_rss(), self._memoryLimit))
  251. break
  252. for sleepTask in sleepTasks:
  253. sleepTask.cancel()
  254. while len(tasks):
  255. await wait_for_free_task()
  256. logging.info('Done')
  257. except (Exception, KeyboardInterrupt) as e:
  258. # Kill all tasks
  259. for task in tasks:
  260. task.cancel()
  261. await asyncio.wait(tasks, return_when = concurrent.futures.ALL_COMPLETED)
  262. raise
  263. finally:
  264. for session in sessions:
  265. session.close()
  266. warc.close()
  267. db.close()
  268. def create_db(self):
  269. db = sqlite3.connect(self._dbPath, timeout = 1)
  270. db.execute('PRAGMA synchronous = OFF')
  271. with db:
  272. db.execute('CREATE TABLE items (id INTEGER PRIMARY KEY, type TEXT, value TEXT, status INTEGER)')
  273. db.execute('CREATE INDEX items_status_idx ON items (status)')
  274. it = itertools.chain(*(i._gen() for i in self._itemClasses))
  275. while True:
  276. values = tuple(itertools.islice(it, 100000))
  277. if not values:
  278. break
  279. with db:
  280. db.executemany('INSERT INTO items (type, value, status) VALUES (?, ?, ?)', values)