@@ -1344,43 +1344,46 @@ def cache_lock(self, url):
1344
1344
cpath = self .url2cachedir (url )
1345
1345
if cpath :
1346
1346
lock_file = os .path .join (cpath , '.lock' )
1347
- try :
1348
- if not os .path .isdir (cpath ):
1349
- os .makedirs (cpath )
1350
-
1351
- can_lock = False
1352
- # this loop is handling a lock file from another process if exists
1353
- for i in range (300 ):
1354
- if i :
1355
- time .sleep (1 )
1356
- if os .path .isfile (lock_file ):
1357
- try :
1358
- # lock file exists, but we need to check pid as well in case the process died
1359
- with open (lock_file , 'r' , 0 ) as f :
1360
- pid = f .read (8 )
1361
- if pid and int (pid ) != os .getpid ():
1362
- if self .pid_exists (pid ):
1363
- continue
1364
- else :
1365
- info ("Cache lock file exists, but process is dead. Cleaning up" )
1366
- os .remove (lock_file )
1347
+ if not os .path .isdir (cpath ):
1348
+ os .makedirs (cpath )
1349
+
1350
+ can_lock = False
1351
+ # this loop is handling a lock file from another process if exists
1352
+ for i in range (300 ):
1353
+ if i :
1354
+ time .sleep (1 )
1355
+ if os .path .isfile (lock_file ):
1356
+ try :
1357
+ # lock file exists, but we need to check pid as well in case the process died
1358
+ with open (lock_file , 'r' , 0 ) as f :
1359
+ pid = f .read (8 )
1360
+ if pid and int (pid ) != os .getpid ():
1361
+ if self .pid_exists (pid ):
1362
+ info ("Cache lock file exists and process %s is alive." % pid )
1367
1363
continue
1368
- except (IOError , OSError ):
1364
+ else :
1365
+ info ("Cache lock file exists, but process is dead. Cleaning up" )
1366
+ os .remove (lock_file )
1369
1367
continue
1370
- pass
1368
+ except (IOError , OSError ):
1369
+ continue
1370
+ pass
1371
1371
1372
- can_lock = True
1373
- break
1372
+ can_lock = True
1373
+ break
1374
1374
1375
- if can_lock :
1375
+ if can_lock :
1376
+ try :
1376
1377
with open (lock_file , 'wb' , 0 ) as f :
1378
+ info ("Writing cache lock file for pid %s" % os .getpid ())
1377
1379
f .write (str (os .getpid ()))
1378
1380
f .flush ()
1381
+ os .fsync (f )
1382
+ except (IOError , OSError ):
1383
+ error ("Unable to lock cache dir \" %s\" " % (cpath ))
1379
1384
1380
- else :
1381
- error ("Exceeded 5 minutes limit while waiting for other process to finish caching" )
1382
- except Exception :
1383
- error ("Unable to lock cache dir \" %s\" " % (cpath ))
1385
+ else :
1386
+ error ("Exceeded 5 minutes limit while waiting for other process to finish caching" )
1384
1387
return False
1385
1388
1386
1389
def cache_unlock (self , url ):
0 commit comments