11import lockfile , { Options } from 'lockfile' ;
22import { promisify } from 'util' ;
33
4+ import cluster from 'cluster' ;
45import debug from './debug' ;
56import log from './log' ;
6- import { delay , workerIdLabel } from './utils' ;
7+ import { delay } from './utils' ;
78
89const lockfileLockAsync = promisify < string , Options > ( lockfile . lock ) ;
910const lockfileUnlockAsync = promisify ( lockfile . unlock ) ;
@@ -48,8 +49,7 @@ const lockfileOptions = {
4849} ;
4950
5051export async function unlock ( lockfileName : string ) {
51- debug ( 'Worker %s: About to unlock %s' , workerIdLabel ( ) , lockfileName ) ;
52- log . info ( 'Worker %s: About to unlock %s' , workerIdLabel ( ) , lockfileName ) ;
52+ log . info ( 'About to unlock %s' , lockfileName ) ;
5353
5454 await lockfileUnlockAsync ( lockfileName ) ;
5555}
@@ -69,22 +69,21 @@ type LockResult = {
6969
7070export async function lock ( filename : string ) : Promise < LockResult > {
7171 const lockfileName = `${ filename } .lock` ;
72- const workerId = workerIdLabel ( ) ;
7372
7473 try {
75- debug ( 'Worker %s: About to request lock %s' , workerId , lockfileName ) ;
76- log . info ( 'Worker %s: About to request lock %s' , workerId , lockfileName ) ;
74+ log . info ( 'About to request lock %s' , lockfileName ) ;
7775 await lockfileLockAsync ( lockfileName , lockfileOptions ) ;
7876
77+ const workerId = cluster . worker ?. id ?? 'NO WORKER ID' ;
7978 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- the const may be changed to test threading
8079 if ( TEST_LOCKFILE_THREADING ) {
8180 debug ( 'Worker %i: handleNewBundleProvided sleeping 5s' , workerId ) ;
8281 await delay ( 5000 ) ;
8382 debug ( 'Worker %i: handleNewBundleProvided done sleeping 5s' , workerId ) ;
8483 }
85- debug ( 'After acquired lock in pid' , lockfileName ) ;
84+ debug ( 'Worker %i: After acquired lock %s' , workerId , lockfileName ) ;
8685 } catch ( error ) {
87- log . info ( 'Worker %s: Failed to acquire lock %s, error %s ', workerId , lockfileName , error ) ;
86+ log . info ( error , ' Failed to acquire lock %s', lockfileName ) ;
8887 return { lockfileName, wasLockAcquired : false , errorMessage : error as Error } ;
8988 }
9089 return { lockfileName, wasLockAcquired : true , errorMessage : null } ;
0 commit comments