/*
 * Copyright 2000-2011 JetBrains s.r.o.
 *
 * 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 jetbrains.buildServer.nuget.server.feed.server.process;

import jetbrains.buildServer.serverSide.BuildServerAdapter;
import jetbrains.buildServer.serverSide.BuildServerListener;
import jetbrains.buildServer.serverSide.executors.ExecutorServices;
import jetbrains.buildServer.util.EventDispatcher;
import jetbrains.buildServer.util.ExceptionUtil;
import org.jetbrains.annotations.NotNull;

import java.util.concurrent.Future;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

/**
 * @author Eugene Petrenko (eugene.petrenko@gmail.com)
 *         Date: 07.10.11 15:27
 */
public class NuGetServerCruiser {
  public NuGetServerCruiser(@NotNull final NuGetServerRunner runner,
                            @NotNull final ExecutorServices executors,
                            @NotNull final NuGetServerCruiserTask task,
                            @NotNull final EventDispatcher<BuildServerListener> events) {
    events.addListener(new BuildServerAdapter(){
      private ScheduledFuture<?> myCheckTask;

      @Override
      public void serverStartup() {
        myCheckTask = executors.getNormalExecutorService().scheduleWithFixedDelay(
                ExceptionUtil.catchAll("Check NuGet Server is running task",
                        new Runnable() {
                          public void run() {
                            task.checkNuGetServerState();
                          }
                        }), 5, 5, TimeUnit.SECONDS);
      }

      @Override
      public void serverShutdown() {
        Future<?> task = myCheckTask;
        myCheckTask = null;
        task.cancel(false);

        runner.stopServer();
      }
    });
  }
}

@@ -674,24 +674,72 @@
 process;%0D%0A%0D%0A
+import com.intellij.openapi.diagnostic.Logger;%0D%0A
 import jetbr
@@ -845,24 +845,85 @@
 rListener;%0D%0A
+import jetbrains.buildServer.serverSide.TeamCityProperties;%0D%0A
 import jetbr
@@ -1035,58 +1035,8 @@
 r;%0D%0A
-import jetbrains.buildServer.util.ExceptionUtil;%0D%0A
 impo
@@ -1104,22 +1104,24 @@
 current.
-Future
+TimeUnit
 ;%0D%0Aimpor
@@ -1147,62 +1147,28 @@
 ent.
-ScheduledFuture;%0D%0Aimport java.util.concurrent.TimeUnit
+atomic.AtomicBoolean
 ;%0D%0A%0D
@@ -1302,16 +1302,111 @@
 iser %7B%0D%0A
+  private static final Logger LOG = Logger.getInstance(NuGetServerCruiser.class.getName());%0D%0A%0D%0A
   public
@@ -1771,38 +1771,60 @@
 ate 
-ScheduledFuture%3C?%3E myCheckTask
+final AtomicBoolean myShutDown = new AtomicBoolean()
 ;%0D%0A%0D
@@ -1890,202 +1890,124 @@
     
-myCheckTask = executors.getNormalExecutorService().scheduleWithFixedDelay(%0D%0A                ExceptionUtil.catchAll(%22Check NuGet Server is running task%22,%0D%0A                        new Runnable() %7B
+final Runnable action = new Runnable() %7B%0D%0A          public void run() %7B%0D%0A            if (myShutDown.get()) return;%0D%0A
 %0D%0A  
@@ -2008,33 +2008,39 @@
 %0D%0A%0D%0A            
- 
+try %7B%0D%0A
              pub
@@ -2040,84 +2040,163 @@
     
-public void run() %7B%0D%0A                            task.
+ task.checkNuGetServerState();%0D%0A            %7D catch (Throwable t) %7B%0D%0A              LOG.warn(%22Failed to 
 check
+ 
 NuGet
+ Feed 
 Server
-S
+ s
 tate
-(
+. %22 + t.getMessage(), t
 );%0D%0A
@@ -2199,32 +2199,37 @@
 );%0D%0A            
+%7D%0D%0A%0D%0A
               %7D%0D
@@ -2228,45 +2228,170 @@
     
-  %7D%0D%0A                        %7D), 5
+if (myShutDown.get()) return;%0D%0A            executors.getNormalExecutorService().schedule(this, TeamCityProperties.getInteger(%22teamcity.nuget.server.ping.time%22
 , 5
+)
 , Ti
@@ -2404,24 +2404,72 @@
 .SECONDS);%0D%0A
+          %7D%0D%0A        %7D;%0D%0A        action.run();%0D%0A
       %7D%0D%0A%0D%0A 
@@ -2534,99 +2534,31 @@
     
-Future%3C?%3E task = myCheckTask;%0D%0A        myCheckTask = null;%0D%0A        task.cancel(fals
+myShutDown.set(tru
 e);%0D%0A
-%0D%0A
     

make NuGet server ping interval adjustable