@@ -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 ,
@@ -172,16 +179,8 @@ async def run_job(ws: Websocket, full_job: dict, url: str, warc_prefix: str, ua:
172179 res = json .loads (res )
173180 type = res ['type' ]
174181 payload = res ['payload' ]
175- if type in ("status_code" , "outlinks" , "final_url" , "requisites" , "custom_js" ):
176- await ws .store_result (id , type , tries , payload )
177- elif type == "screenshot" :
178- # Don't tell the tracker to decode the thumbnail
179- # if there isn't a thumbnail
180- decode_fields = [k for k in ("full" , "thumb" ) if payload [k ]]
181- await ws .store_result (id , type , tries , payload , decode_fields )
182- elif type == "cjs_screenshot" :
183- decode_fields = ["full" ]
184- await ws .store_result (id , type , tries , payload , decode_fields )
182+ if type in ("status_code" , "outlinks" , "final_url" , "requisites" , "custom_js" , "screenshot" , "cjs_screenshot" ):
183+ await ws .store_result (attempt_id , type , payload )
185184 elif type == "error" :
186185 # Cancel tasks, since both stdout and pread are about to get closed.
187186 # Failing to do this results in a "Task exception was never retrieved"
@@ -231,6 +230,7 @@ async def ping_occasionally():
231230 pass
232231 await asyncio .sleep (15 )
233232
233+ # Don't change this without changing the hardcoded slot=0 below and the hardcoded num_slots=1 in tracker.py.
234234 MAX_WORKERS = 1
235235 workers : dict [asyncio .Task , tuple [TaskType , dict | None ]] = dict ()
236236
@@ -264,41 +264,36 @@ def handle_sigint():
264264 logger .debug ("not spinning up new item as we are pending a stop" )
265265 continue
266266 logger .debug ("spinning up worker" )
267- resp = await ws .claim_item ()
267+ resp = await ws .claim_item (0 )
268268 if resp :
269- item , info_url = resp
270- id = item [ 'id' ]
271- logger . info ( f"Starting task { id } " )
272- url = item [ 'item ' ]
273- assert "_" not in id
274- prefix = "mnbot-brozzler-" + id . replace ( "-" , "_ " )
269+ claim , info_url = resp
270+ print ( claim , info_url )
271+ attempt_id = claim [ 'attempt_id' ]
272+ job_id = claim [ 'job_id ' ]
273+ page_id = claim [ 'page_id' ]
274+ logger . info ( f"Starting claim { attempt_id } (for { job_id } : { page_id } " )
275275 task = asyncio .create_task (run_job (
276276 ws ,
277- item ,
278- url ,
279- prefix ,
280- item ['metadata' ]['ua' ],
281- item ['metadata' ]['custom_js' ],
282- info_url
277+ claim ,
278+ info_url ,
283279 ))
284- task .set_name (id )
285- workers [task ] = (TaskType .ITEM , item )
280+ task .set_name (attempt_id )
281+ workers [task ] = (TaskType .ITEM , claim )
286282 else :
287283 to_sleep = random .randint (10 , 30 )
288- logger .info (f"No items found, blocking this worker for { to_sleep } seconds." )
284+ logger .info (f"No tasks found, blocking this worker for { to_sleep } seconds." )
289285 task = asyncio .create_task (asyncio .sleep (to_sleep ))
290286 workers [task ] = (TaskType .SLEEP , None )
291287 done : set [asyncio .Task ] = (await asyncio .wait (workers , return_when = asyncio .FIRST_COMPLETED ))[0 ]
292288 for finished_task in done :
293289 logger .debug (f"checking finished task { finished_task } " )
294- task_type , item = workers [finished_task ]
290+ task_type , task_claim = workers [finished_task ]
295291 del workers [finished_task ]
296292 if task_type != TaskType .ITEM :
297293 logger .debug ("nevermind, not an item" )
298294 continue
299- # item can't be None at this point
300- id = item ['id' ]
301- tries = item ['_current_attempt' ]
295+ # claim can't be None at this point
296+ attempt_id = task_claim ['attempt_id' ]
302297 try :
303298 _dedup_bucket , _stats_bucket = finished_task .result ()
304299 except Exception as e :
@@ -307,14 +302,14 @@ def handle_sigint():
307302 fatal = e .fatal
308303 else :
309304 fatal = False
310- logger .exception (f"failed task { id } :" )
305+ logger .exception (f"failed task { attempt_id } :" )
311306 fmt = io .StringIO ()
312307 finished_task .print_stack (file = fmt )
313308 message = f"Caught exception!\n { fmt .getvalue ()} "
314- await ws .fail_item (id , message , tries , fatal )
309+ await ws .fail_item (attempt_id , message , fatal )
315310 else :
316311 logger .info (f"task { id } was successful!" )
317- await ws .finish_item (id )
312+ await ws .finish_item (attempt_id )
318313 logger .debug ("creating cleanup task" )
319314 task = asyncio .create_task (warcprox_cleanup ())
320315 workers [task ] = (TaskType .CLEANUP , None )
0 commit comments