public LockResult tryLock ( final TaskLockType lockType , final Task task , final Interval interval ) { giant . lock ( ) ; try { if ( ! activeTasks . contains ( task . getId ( ) ) ) { throw new ISE ( "Unable to grant lock to inactive Task [%s]" , task . getId ( ) ) ; } Preconditions . checkArgument ( interval . toDurationMillis ( ) > 0 , "interval empty" ) ; final TaskLockPosse posseToUse = createOrFindLockPosse ( task , interval , lockType ) ; if ( posseToUse != null && ! posseToUse . getTaskLock ( ) . isRevoked ( ) ) { // Add to existing TaskLockPosse, if necessary if ( posseToUse . addTask ( task ) ) { log . info ( "Added task[%s] to TaskLock[%s]" , task . getId ( ) , posseToUse . getTaskLock ( ) . getGroupId ( ) ) ; // Update task storage facility. If it fails, revoke the lock. try { taskStorage . addLock ( task . getId ( ) , posseToUse . getTaskLock ( ) ) ; return LockResult . ok ( posseToUse . getTaskLock ( ) ) ; } catch ( Exception e ) { log . makeAlert ( "Failed to persist lock in storage" ) . addData ( "task" , task . getId ( ) ) . addData ( "dataSource" , posseToUse . getTaskLock ( ) . getDataSource ( ) ) . addData ( "interval" , posseToUse . getTaskLock ( ) . getInterval ( ) ) . addData ( "version" , posseToUse . getTaskLock ( ) . getVersion ( ) ) . emit ( ) ; unlock ( task , interval ) ; return LockResult . fail ( false ) ; } } else { log . info ( "Task[%s] already present in TaskLock[%s]" , task . getId ( ) , posseToUse . getTaskLock ( ) . getGroupId ( ) ) ; return LockResult . ok ( posseToUse . getTaskLock ( ) ) ; } } else { final boolean lockRevoked = posseToUse != null && posseToUse . getTaskLock ( ) . isRevoked ( ) ; return LockResult . fail ( lockRevoked ) ; } } finally { giant . unlock ( ) ; } }
Attempt to acquire a lock for a task without removing it from the queue . Can safely be called multiple times on the same task until the lock is preempted .