private Thread registerShutdownHook() { Thread shutdownHook = new Thread(() -> lockFile.delete()); Runtime.getRuntime().addShutdownHook(shutdownHook); return shutdownHook; }
public boolean tryLock() { if (exists()) { String alreadyRunningMsg = "Already running agent launcher in this folder."; System.out.println(alreadyRunningMsg); LOG.error(alreadyRunningMsg); return false; } try { setHooks(); return true; } catch (IOException e) { String lockFileFailMsg = "Unable to lock file (" + lockFile.getAbsolutePath() + ")"; System.err.println(lockFileFailMsg); LOG.error(lockFileFailMsg, e); return false; } }
boolean exists() { return lockFile.exists() && lockFileChangedWithinMinutes(10); }
void setHooks() throws IOException { touch(); spawnTouchLoop(); }
public synchronized void delete() { requestTouchLoopToStop(); waitForTouchLoopThread(); try { deleteLockFile(); } catch (Exception e) { LOG.error("Error deleting lock file at {}", lockFile.getAbsolutePath(), e); } }
@Test public void shouldNotExistIfNotChangedRecently() { File mockfile = mock(File.class); Lockfile lockfile = new Lockfile(mockfile); Lockfile spy = spy(lockfile); doReturn(false).when(spy).lockFileChangedWithinMinutes(10); when(lockfile.exists()).thenReturn(true); assertThat(spy.exists(), is(false)); }
@Test public void shouldSpawnTouchLoopOnSet() throws IOException { Lockfile lockfile = mock(Lockfile.class); doCallRealMethod().when(lockfile).setHooks(); doNothing().when(lockfile).touch(); doNothing().when(lockfile).spawnTouchLoop(); lockfile.setHooks(); verify(lockfile).spawnTouchLoop(); }
@Test public void shouldReturnFalseifUnableToSetLock() throws IOException { File mockfile = mock(File.class); Lockfile lockfile = spy(new Lockfile(mockfile)); when(mockfile.exists()).thenReturn(false); when(mockfile.getAbsolutePath()).thenReturn("/abcd/dummyFile"); doThrow(new IOException("dummy")).when(lockfile).setHooks(); assertThat(lockfile.tryLock(), is(false)); verify(mockfile).exists(); }
@Test public void shouldReturnFalseIfLockFileAlreadyExists() throws IOException { File mockfile = mock(File.class); Lockfile lockfile = new Lockfile(mockfile); when(mockfile.exists()).thenReturn(true); when(mockfile.lastModified()).thenReturn(System.currentTimeMillis()); assertThat(lockfile.tryLock(), is(false)); verify(mockfile).exists(); }
public void run() { LOG.info("Using lock file: {}", lockFile.getAbsolutePath()); do { try { touch(); Thread.sleep(touchFrequency); } catch (Exception e) { return; } } while (keepRunning); }
public Lockfile(File filename) { this.lockFile = filename; this.touchFrequency = getIntProperty(TOUCH_FREQUENCY_PROPERTY, DEFAULT_TOUCH_FREQUENCY); this.sleepTimeBeforeLastModifiedCheck = getIntProperty(SLEEP_TIME_FOR_LAST_MODIFIED_CHECK_PROPERTY, DEFAULT_SLEEP_TIME_FOR_LAST_MODIFIED_CHECK); }
@Test public void shouldExistIfFileExistsAndChangedRecently() { File mockfile = mock(File.class); Lockfile lockfile = new Lockfile(mockfile); Lockfile spy = spy(lockfile); doReturn(true).when(spy).lockFileChangedWithinMinutes(10); when(lockfile.exists()).thenReturn(true); assertThat(spy.exists(), is(true)); }