Source:
<code>public void start() { final File taskFile = Preconditions.checkNotNull(config.getTaskFile(), "taskFile"); final File statusFile = Preconditions.checkNotNull(config.getStatusFile(), "statusFile"); final File portFile = Preconditions.checkNotNull(config.getPortFile(), "portFile"); try(OutputStream outputStream = new FileOutputStream(portFile)){ <START> outputStream.write(String.format("%d", druidNode.getPort()).getBytes("UTF-8")); <END> } catch (IOException e) { throw Throwables.propagate(e); } final Task task; try { task = jsonMapper.readValue(taskFile, Task.class); log.info( "Running with task: %s", jsonMapper.writerWithDefaultPrettyPrinter().writeValueAsString(task) ); } catch (IOException e) { throw Throwables.propagate(e); } try { if (!task.isReady(taskActionClientFactory.create(task))) { throw new ISE("Task is not ready to run yet!", task.getId()); } } catch (Exception e) { throw new ISE(e, "Failed to run isReady", task.getId()); } statusFuture = Futures.transform( taskRunner.run(task), new Function<TaskStatus, TaskStatus>() { @Override public TaskStatus apply(TaskStatus taskStatus) { try { log.info( "Task completed with status: %s", jsonMapper.writerWithDefaultPrettyPrinter().writeValueAsString(taskStatus) ); final File statusFileParent = statusFile.getParentFile(); if (statusFileParent != null) { statusFileParent.mkdirs(); } jsonMapper.writeValue(statusFile, taskStatus); return taskStatus; } catch (Exception e) { throw Throwables.propagate(e); } } } ); }</code><technical_language>I easy tasks dead killing a middleManager useful. do want die. IMO removing this means some other way. middleManager heartbeat tasks, exit if a heartbeat in a amount of time? long enough cover reasonable update + restart time</technical_language>

Target:
public void start() { final File taskFile = Preconditions.checkNotNull(config.getTaskFile(), "taskFile"); final File statusFile = Preconditions.checkNotNull(config.getStatusFile(), "statusFile"); final File portFile = Preconditions.checkNotNull(config.getPortFile(), "portFile"); try(final FileChannel portFileChannel = FileChannel.open(portFile.toPath(), StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)) { final String portStr = String.format("%d", druidNode.getPort()); final FileLock lock = portFileChannel.lock(); try { CharsetEncoder encoder = Charsets.UTF_8.newEncoder(); portFileChannel.write(encoder.encode(CharBuffer.wrap(portStr))); }finally{ lock.release(); } } catch (IOException e) { throw Throwables.propagate(e); } final Task task; try { task = jsonMapper.readValue(taskFile, Task.class); log.info( "Running with task: %s", jsonMapper.writerWithDefaultPrettyPrinter().writeValueAsString(task) ); } catch (IOException e) { throw Throwables.propagate(e); } try { if (!task.isReady(taskActionClientFactory.create(task))) { throw new ISE("Task is not ready to run yet!", task.getId()); } } catch (Exception e) { throw new ISE(e, "Failed to run isReady", task.getId()); } statusFuture = Futures.transform( taskRunner.run(task), new Function<TaskStatus, TaskStatus>() { @Override public TaskStatus apply(TaskStatus taskStatus) { try { log.info( "Task completed with status: %s", jsonMapper.writerWithDefaultPrettyPrinter().writeValueAsString(taskStatus) ); final File statusFileParent = statusFile.getParentFile(); if (statusFileParent != null) { statusFileParent.mkdirs(); } jsonMapper.writeValue(statusFile, taskStatus); return taskStatus; } catch (Exception e) { throw Throwables.propagate(e); } } } ); }