/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014 robert.gruendler@dubture.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.dubture.jenkins.digitalocean;

import com.myjeeva.digitalocean.pojo.Droplet;
import hudson.model.Descriptor;
import hudson.slaves.CloudSlaveRetentionStrategy;
import hudson.util.TimeUnit2;

/**
 *
 * The {@link RetentionStrategy} is mainly used to determine
 * when an idle {@link Droplet} can be destroyed.
 *
 * @author robert.gruendler@dubture.com
 */
public class RetentionStrategy extends CloudSlaveRetentionStrategy<Computer> {

    public static class DescriptorImpl extends Descriptor<hudson.slaves.RetentionStrategy<?>> {
        @Override
        public String getDisplayName() {
            return "DigitalOcean";
        }
    }

    public void start(Computer computer) {
        computer.connect(false);
    }

    @Override
    protected boolean isIdleForTooLong(Computer computer) {

        int idleTerminationTime = computer.getNode().getIdleTerminationTime();

        if (idleTerminationTime == 0) {
            return false;
        }

        return System.currentTimeMillis() - computer.getIdleStartMilliseconds() > TimeUnit2.MINUTES.toMillis(idleTerminationTime);
    }
}

@@ -1963,24 +1963,98 @@
 omputer) %7B%0A%0A
+    @Override%0A    protected boolean isIdleForTooLong(Computer computer) %7B%0A
         int 
@@ -2194,24 +2194,67 @@
 %0A        %7D%0A%0A
+        if (idleTerminationTime %3E 0) %7B%0A    
         retu
@@ -2368,20 +2368,662 @@
 ationTime);%0A
+        %7D else if (idleTerminationTime %3C 0 && computer.isIdle()) %7B%0A            // DigitalOcaen charges for the next hour at 1:30, 2:30, 3:30, etc. up time, so kill the node%0A            // if it idles and is about to get charged for next hour%0A            long uptimeMinutes = TimeUnit2.MILLISECONDS.toMinutes(System.currentTimeMillis() - computer.getStartTimeMillis());%0A%0A            if (uptimeMinutes %3C 60) %7B%0A                return false;%0A            %7D%0A%0A            while (uptimeMinutes %3E= 60) %7B%0A                uptimeMinutes -= 60;%0A            %7D%0A%0A            return uptimeMinutes %3E= 25 && uptimeMinutes %3C 30;%0A        %7D%0A%0A        return false;%0A
     %7D%0A%7D%0A

Save money by letting a slave run until the paid hour runs out