PostRestoreHook implements IPostRestoreHook { public void execute() throws Exception { if (config.isPostRestoreHookEnabled()) { logger.debug("Started PostRestoreHook execution"); File tempLockFile = File.createTempFile( PriamPostRestoreHookFilePrefix, PriamPostRestoreHookFileSuffix); RandomAccessFile raf = new RandomAccessFile(tempLockFile.getPath(), "rw"); FileChannel fileChannel = raf.getChannel(); FileLock lock = fileChannel.lock(); try { if (lock.isValid()) { logger.info("Lock on RestoreHookFile acquired"); int countOfProcessStarts = 0; while (true) { if (doneFileExists()) { logger.info( "Not starting PostRestoreHook since DONE file already exists."); break; } String postRestoreHook = config.getPostRestoreHook(); postRestoreHook = postRestoreHook + PostRestoreHookCommandDelimiter + PriamPostRestoreHookFileOptionName + tempLockFile.getAbsolutePath(); String[] processCommandArguments = postRestoreHook.split(PostRestoreHookCommandDelimiter); ProcessBuilder processBuilder = new ProcessBuilder(processCommandArguments); Process process = processBuilder.inheritIO().start(); logger.info( "Started PostRestoreHook: {} - Attempt#{}", postRestoreHook, ++countOfProcessStarts); monitorPostRestoreHookHeartBeat(process); if (!process.waitFor( config.getPostRestoreHookTimeOutInDays(), TimeUnit.DAYS)) { logger.info( "PostRestoreHook process did not complete within {} days. Forcefully terminating the process.", config.getPostRestoreHookTimeOutInDays()); process.destroyForcibly(); } if (process.exitValue() == 0) { logger.info("PostRestoreHook process completed successfully"); break; } logger.warn("PostRestoreHook process exited unsuccessfully"); } logger.debug("Completed PostRestoreHook execution"); } else { throw new PostRestoreHookException( String.format( "Could not acquire lock on a temp file necessary for PostRestoreHook to execute. Path to temp file: %s", tempLockFile.getAbsolutePath())); } } finally { lock.release(); fileChannel.close(); raf.close(); tempLockFile.delete(); } } } @Inject  PostRestoreHook(IConfiguration config, Sleeper sleeper); boolean hasValidParameters(); void execute();  }
@Test public void testPostRestoreHookExecuteHappyPath() throws Exception { Injector inject = Guice.createInjector(new TestModule()); IPostRestoreHook postRestoreHook = inject.getInstance(IPostRestoreHook.class); IConfiguration configuration = inject.getInstance(IConfiguration.class); startHeartBeatThreadWithDelay( 0, configuration.getPostRestoreHookHeartbeatFileName(), configuration.getPostRestoreHookDoneFileName()); postRestoreHook.execute(); }
@Test public void testPostRestoreHookExecuteHeartBeatDelay() throws Exception { Injector inject = Guice.createInjector(new TestModule()); IPostRestoreHook postRestoreHook = inject.getInstance(IPostRestoreHook.class); IConfiguration configuration = inject.getInstance(IConfiguration.class); startHeartBeatThreadWithDelay( 1000, configuration.getPostRestoreHookHeartbeatFileName(), configuration.getPostRestoreHookDoneFileName()); postRestoreHook.execute(); }