/*
 * Copyright 2013 Twitter, Inc. and other contributors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jenkinsci.plugins.mesos;

import static hudson.util.TimeUnit2.MINUTES;

import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Logger;

import hudson.model.*;
import hudson.slaves.SlaveComputer;
import org.joda.time.DateTimeUtils;
import hudson.slaves.OfflineCause;
import org.kohsuke.stapler.DataBoundConstructor;

import hudson.slaves.RetentionStrategy;
import hudson.util.TimeUnit2;

/**
 * This is inspired by {@link hudson.slaves.CloudRetentionStrategy}.
 */
public class MesosRetentionStrategy extends RetentionStrategy<MesosComputer> implements ExecutorListener {

  /**
   * Number of minutes of idleness before an instance should be terminated. A
   * value of zero indicates that the instance should never be automatically
   * terminated.
   */
  public final int idleTerminationMinutes;
  private ReentrantLock checkLock = new ReentrantLock(false);

  private static final Logger LOGGER = Logger
      .getLogger(MesosRetentionStrategy.class.getName());

  public MesosRetentionStrategy(int idleTerminationMinutes) {
    this.idleTerminationMinutes = idleTerminationMinutes;
  }


  @Override
  public long check(MesosComputer c) {
    if (!checkLock.tryLock()) {
      return 1;
    } else {
      try {
        return checkInternal(c);
      } finally {
        checkLock.unlock();
      }
    }
  }

  /**
   * Checks if the computer has expired and marks it for deletion.
   * {@link org.jenkinsci.plugins.mesos.MesosCleanupThread} will then come around and terminate those tasks
   * @param c The Mesos Computer
   * @return The number of minutes to check again afterwards
   */
  private long checkInternal(MesosComputer c) {
    if (c.getNode() == null) {
      return 1;
    }

    // If we just launched this computer, check back after 1 min.
    // NOTE: 'c.getConnectTime()' refers to when the Jenkins slave was launched.
    if ((DateTimeUtils.currentTimeMillis() - c.getConnectTime()) <
        MINUTES.toMillis(idleTerminationMinutes < 1 ? 1 : idleTerminationMinutes)) {
      return 1;
    }

    final long idleMilliseconds =
            DateTimeUtils.currentTimeMillis() - c.getIdleStartMilliseconds();
    // Terminate the computer if it is idle for longer than
    // 'idleTerminationMinutes'.
    if (isTerminable() && c.isIdle()) {

      if (idleMilliseconds > MINUTES.toMillis(idleTerminationMinutes)) {
        LOGGER.info("Disconnecting idle computer " + c.getName());
        c.getNode().setPendingDelete(true);

        if (!c.isOffline()) {
          c.setTemporarilyOffline(true, OfflineCause.create(Messages._DeletedCause()));
        }
      }
    }

    return 1;
  }

  /**
   * Try to connect to it ASAP to launch the slave agent.
   */
  @Override
  public void start(MesosComputer c) {
    c.connect(false);
  }

  @Override
  public void taskAccepted(Executor executor, Queue.Task task) {
    Node node = executor.getOwner().getNode();
    if(node != null && node instanceof MesosSlave) {
      MesosSlave mesosSlave = (MesosSlave) executor.getOwner().getNode();
      if (mesosSlave.getSlaveInfo().isUseSlaveOnce()) {
        // Force Use Once Only on all executors
        ((SlaveComputer) mesosSlave.getComputer()).setAcceptingTasks(false);
      }
    }
  }

  @Override
  public void taskCompleted(Executor executor, Queue.Task task, long l) {
    Node node = executor.getOwner().getNode();
    if (node != null && node instanceof MesosSlave) {
      try {
        MesosSlave mesosSlave = (MesosSlave) node;
        if(mesosSlave.getSlaveInfo().isUseSlaveOnce()) {
          // Force Use Once Only on all executors
          mesosSlave.getComputer().setTemporarilyOffline(true, OfflineCause.create(Messages._SingleUseCause()));
          mesosSlave.setPendingDelete(true);
        }
      } catch (Exception e) {
        LOGGER.warning("Exception while trying to set Computer temporarily Offline: " + e);
        e.printStackTrace();
      }
    }
  }

  @Override
  public void taskCompletedWithProblems(Executor executor, Queue.Task task, long l, Throwable throwable) {
    LOGGER.warning("Task completed with Problems " + throwable.getMessage() + " on " + executor.getDisplayName());
    throwable.printStackTrace();
    taskCompleted(executor, task, l);
  }

  /**
   * No registration since this retention strategy is used only for Mesos nodes
   * that we provision automatically.
   */
  public static class DescriptorImpl extends Descriptor<RetentionStrategy<?>> {
    @Override
    public String getDisplayName() {
      return "MESOS";
    }
  }

  boolean isTerminable() {
    return idleTerminationMinutes != 0;
  }
}

@@ -4212,32 +4212,35 @@
 cutors%0A         
+ //
  mesosSlave.getC
@@ -4336,16 +4336,19 @@
         
+ //
  mesosSl

disable setTemporarilyOffline / pending delete on task exec fin.