/*
 * The MIT License
 *
 * (c) 2004-2015. Parallels IP Holdings GmbH. All rights reserved.
 *
 * 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.parallels.desktopcloud;

import hudson.slaves.RetentionStrategy;
import java.util.logging.Level;
import java.util.concurrent.locks.ReentrantLock;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.DataBoundConstructor;


public class ParallelsDesktopCloudRetentionStrategy extends RetentionStrategy<ParallelsDesktopVMSlaveComputer>
{
	private static final ParallelsLogger LOGGER = ParallelsLogger.getLogger("PDCloudRetentionStrategy");
	private transient ReentrantLock checkLock;

	@DataBoundConstructor
	public ParallelsDesktopCloudRetentionStrategy()
	{
		super();
		checkLock = new ReentrantLock();
	}

	@Override
	public long check(ParallelsDesktopVMSlaveComputer c)
	{
		if (!checkLock.tryLock())
			return 1;
		try
		{
			LOGGER.log(Level.SEVERE, "Check VM computer %s", c.getName());
			if (c.isIdle())
			{
				try
				{
					LOGGER.log(Level.SEVERE, "Disconnecting computer...");
					c.disconnect(null).get();
					c.getNode().terminate();
				}
				catch (Exception e)
				{
					LOGGER.log(Level.SEVERE, "Error: %s", e);
				}
			}
			return 1;
		}
		finally
		{
			checkLock.unlock();
		}
	}

	@Override
	public void start(ParallelsDesktopVMSlaveComputer c)
	{
		LOGGER.log(Level.SEVERE, "Starting VM computer %s", c.getName());
		c.connect(false);
	}

	@Override
	public DescriptorImpl getDescriptor()
	{
		return DESCRIPTOR;
	}

	@Restricted(NoExternalUse.class)
	public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl();
	public static final class DescriptorImpl extends hudson.model.Descriptor<RetentionStrategy<?>>
	{
		@Override
		public String getDisplayName()
		{
			return "ParallelsDesktop Cloud Retention Strategy";
		}
	}
}

@@ -1724,16 +1724,86 @@
 eckLock;
+%0A%09private static final long IDLE_TIMEOUT = 2 * 60 * 1000; // 2 minutes
 %0A%0A%09@Data
@@ -2047,88 +2047,230 @@
 %0A%09%09%09
-LOGGER.log(Level.SEVERE, %22Check VM computer %25s%22, c.getName());%0A%09%09%09if (c.isIdle()
+final long idleMillis = System.currentTimeMillis() - c.getIdleStartMilliseconds();%0A%09%09%09LOGGER.log(Level.SEVERE, %22Check VM computer %25s: idle=%25b time=%25d%22, c.getName(), c.isIdle(), idleMillis);%0A%09%09%09if (idleMillis %3E IDLE_TIMEOUT
 )%0A%09%09

Fix computer disconnects due to too early VM stop