This project has retired. For details please refer to its Attic page.
Source code
001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.reef.runtime.common.evaluator;
020
021import org.apache.reef.util.OSUtils;
022import org.apache.reef.wake.EventHandler;
023import org.apache.reef.wake.time.runtime.event.RuntimeStart;
024
025import javax.annotation.concurrent.GuardedBy;
026import javax.annotation.concurrent.ThreadSafe;
027import javax.inject.Inject;
028import java.io.File;
029import java.io.FileNotFoundException;
030import java.io.PrintWriter;
031import java.io.UnsupportedEncodingException;
032import java.util.logging.Level;
033import java.util.logging.Logger;
034
035/**
036 * This Handler writes the Process ID (PID) to a file with a name given in PID_FILE_NAME to the local working directory.
037 */
038@ThreadSafe
039public final class PIDStoreStartHandler implements EventHandler<RuntimeStart> {
040  /**
041   * The file name of the PID file created in the current working directory of the process.
042   */
043  public static final String PID_FILE_NAME = "PID.txt";
044
045  private static final Logger LOG = Logger.getLogger(PIDStoreStartHandler.class.getName());
046
047  @GuardedBy("this")
048  private boolean pidIsWritten = false;
049
050  @Inject
051  private PIDStoreStartHandler() {
052  }
053
054  /**
055   * This call is idempotent: It will only write the PID exactly once per instance.
056   *
057   * @param startTime
058   */
059  @Override
060  public synchronized void onNext(final RuntimeStart startTime) {
061    if (this.isPidNotWritten()) {
062      final long pid = OSUtils.getPID();
063      final File outfile = new File(PID_FILE_NAME);
064      LOG.log(Level.FINEST, "Storing pid `" + pid + "` in file " + outfile.getAbsolutePath());
065      try (final PrintWriter p = new PrintWriter(PID_FILE_NAME, "UTF-8")) {
066        p.write(String.valueOf(pid));
067        p.write("\n");
068      } catch (final FileNotFoundException | UnsupportedEncodingException e) {
069        LOG.log(Level.WARNING, "Unable to create PID file.", e);
070      }
071      this.pidIsWritten = true;
072    } else {
073      LOG.log(Level.FINEST, "PID already written.");
074    }
075  }
076
077
078  /**
079   * @return true, if the PID hasn't been written yet.
080   */
081  private synchronized boolean isPidNotWritten() {
082    return !this.pidIsWritten;
083  }
084}