@@ -105,20 +105,24 @@ async def warcprox_cleanup():
105105 except Exception :
106106 pass
107107
108- async def run_job (ws : Websocket , full_job : dict , url : str , warc_prefix : str , ua : str , custom_js : typing .Optional [str ], info_url : str ):
109- tries = full_job ['_current_attempt' ]
110- id = full_job ['id' ]
108+ async def run_job (ws : Websocket , full_job : dict , info_url : str ):
109+ job_id = full_job ['job_id' ]
110+ page_id = full_job ['page_id' ]
111+ attempt_id = full_job ['attempt_id' ]
112+ assert "_" not in job_id
113+ warc_prefix = "mnbot-brozzler-" + job_id .replace ("-" , "_" )
111114 #dedup_bucket = f"dedup-{id}-{tries}"
112115 dedup_bucket = ""
113- stats_bucket = f"stats-{ id } - { tries } "
116+ stats_bucket = f"stats-{ job_id } "
114117 job = Job (
118+ attempt_id = attempt_id ,
115119 full_job = full_job ,
116- url = url ,
120+ url = full_job [ 'payload' ] ,
117121 warc_prefix = warc_prefix ,
118122 dedup_bucket = dedup_bucket ,
119123 stats_bucket = stats_bucket ,
120- ua = ua ,
121- custom_js = custom_js ,
124+ ua = full_job [ 'settings' ][ 'ua' ] ,
125+ custom_js = full_job [ 'settings' ][ ' custom_js' ] ,
122126 cookie_jar = None ,
123127 mnbot_info_url = info_url
124128 )
@@ -129,7 +133,10 @@ async def run_job(ws: Websocket, full_job: dict, url: str, warc_prefix: str, ua:
129133 PYTHON ,
130134 os .path .join (os .path .dirname (sys .argv [0 ]), "browse.py" ),
131135 str (pwrite ),
132- id , # useful for ps
136+ # These arguments are passed only because they are useful for ps
137+ job_id ,
138+ page_id ,
139+ attempt_id ,
133140 stdin = subprocess .PIPE ,
134141 stdout = subprocess .PIPE ,
135142 stderr = subprocess .STDOUT ,
@@ -174,16 +181,8 @@ async def run_job(ws: Websocket, full_job: dict, url: str, warc_prefix: str, ua:
174181 res = json .loads (res )
175182 type = res ['type' ]
176183 payload = res ['payload' ]
177- if type in ("status_code" , "outlinks" , "final_url" , "requisites" , "custom_js" ):
178- await ws .store_result (id , type , tries , payload )
179- elif type == "screenshot" :
180- # Don't tell the tracker to decode the thumbnail
181- # if there isn't a thumbnail
182- decode_fields = [k for k in ("full" , "thumb" ) if payload [k ]]
183- await ws .store_result (id , type , tries , payload , decode_fields )
184- elif type == "cjs_screenshot" :
185- decode_fields = ["full" ]
186- await ws .store_result (id , type , tries , payload , decode_fields )
184+ if type in ("status_code" , "outlinks" , "final_url" , "requisites" , "custom_js" , "screenshot" , "cjs_screenshot" ):
185+ await ws .store_result (attempt_id , type , payload )
187186 elif type == "error" :
188187 # Cancel tasks, since both stdout and pread are about to get closed.
189188 # Failing to do this results in a "Task exception was never retrieved"
@@ -233,6 +232,7 @@ async def ping_occasionally():
233232 pass
234233 await asyncio .sleep (15 )
235234
235+ # Don't change this without changing the hardcoded slot=0 below and the hardcoded num_slots=1 in tracker.py.
236236 MAX_WORKERS = 1
237237 workers : dict [asyncio .Task , tuple [TaskType , dict | None ]] = dict ()
238238
@@ -266,41 +266,36 @@ def handle_sigint():
266266 logger .debug ("not spinning up new item as we are pending a stop" )
267267 continue
268268 logger .debug ("spinning up worker" )
269- resp = await ws .claim_item ()
269+ resp = await ws .claim_item (0 )
270270 if resp :
271- item , info_url = resp
272- id = item [ 'id' ]
273- logger . info ( f"Starting task { id } " )
274- url = item [ 'item ' ]
275- assert "_" not in id
276- prefix = "mnbot-brozzler-" + id . replace ( "-" , "_ " )
271+ claim , info_url = resp
272+ print ( claim , info_url )
273+ attempt_id = claim [ 'attempt_id' ]
274+ job_id = claim [ 'job_id ' ]
275+ page_id = claim [ 'page_id' ]
276+ logger . info ( f"Starting claim { attempt_id } (for { job_id } : { page_id } " )
277277 task = asyncio .create_task (run_job (
278278 ws ,
279- item ,
280- url ,
281- prefix ,
282- item ['metadata' ]['ua' ],
283- item ['metadata' ]['custom_js' ],
284- info_url
279+ claim ,
280+ info_url ,
285281 ))
286- task .set_name (id )
287- workers [task ] = (TaskType .ITEM , item )
282+ task .set_name (attempt_id )
283+ workers [task ] = (TaskType .ITEM , claim )
288284 else :
289285 to_sleep = random .randint (10 , 30 )
290- logger .info (f"No items found, blocking this worker for { to_sleep } seconds." )
286+ logger .info (f"No tasks found, blocking this worker for { to_sleep } seconds." )
291287 task = asyncio .create_task (asyncio .sleep (to_sleep ))
292288 workers [task ] = (TaskType .SLEEP , None )
293289 done : set [asyncio .Task ] = (await asyncio .wait (workers , return_when = asyncio .FIRST_COMPLETED ))[0 ]
294290 for finished_task in done :
295291 logger .debug (f"checking finished task { finished_task } " )
296- task_type , item = workers [finished_task ]
292+ task_type , task_claim = workers [finished_task ]
297293 del workers [finished_task ]
298294 if task_type != TaskType .ITEM :
299295 logger .debug ("nevermind, not an item" )
300296 continue
301- # item can't be None at this point
302- id = item ['id' ]
303- tries = item ['_current_attempt' ]
297+ # claim can't be None at this point
298+ attempt_id = task_claim ['attempt_id' ]
304299 try :
305300 _dedup_bucket , _stats_bucket = finished_task .result ()
306301 except Exception as e :
@@ -309,14 +304,14 @@ def handle_sigint():
309304 fatal = e .fatal
310305 else :
311306 fatal = False
312- logger .exception (f"failed task { id } :" )
307+ logger .exception (f"failed task { attempt_id } :" )
313308 fmt = io .StringIO ()
314309 finished_task .print_stack (file = fmt )
315310 message = f"Caught exception!\n { fmt .getvalue ()} "
316- await ws .fail_item (id , message , tries , fatal )
311+ await ws .fail_item (attempt_id , message , fatal )
317312 else :
318313 logger .info (f"task { id } was successful!" )
319- await ws .finish_item (id )
314+ await ws .finish_item (attempt_id )
320315 logger .debug ("creating cleanup task" )
321316 task = asyncio .create_task (warcprox_cleanup ())
322317 workers [task ] = (TaskType .CLEANUP , None )
0 commit comments