// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.jdisc.component;

import com.yahoo.component.AbstractComponent;
import com.yahoo.concurrent.DaemonThreadFactory;
import com.yahoo.concurrent.ThreadFactoryFactory;
import com.yahoo.container.di.ComponentDeconstructor;
import com.yahoo.container.di.componentgraph.Provider;
import com.yahoo.jdisc.SharedResource;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
* @author tonyv
* @author gv
*/
public class Deconstructor implements ComponentDeconstructor {
    private final ScheduledExecutorService executor =
            Executors.newScheduledThreadPool(1, ThreadFactoryFactory.getDaemonThreadFactory("deconstructor"));

    private final int delay;

    public Deconstructor(boolean delayDeconstruction) {
        delay = delayDeconstruction ? 60 : 0;
    }


    @Override
    public void deconstruct(Object component) {
        if (component instanceof AbstractComponent) {
            AbstractComponent abstractComponent = (AbstractComponent) component;
            if (abstractComponent.isDeconstructable())
                executor.schedule(
                        new DestructComponentTask(abstractComponent), delay, TimeUnit.SECONDS);
        } else if (component instanceof Provider) {
            ((Provider)component).deconstruct();
        } else if (component instanceof SharedResource) {
            // No need to delay release, as jdisc does ref-counting
            ((SharedResource)component).release();
        }
    }

    private static class DestructComponentTask implements Runnable {
        private final AbstractComponent component;

        DestructComponentTask(AbstractComponent component) {
            this.component = component;
        }

        public void run() {
            component.deconstruct();
        }
    }
}

@@ -203,57 +203,8 @@
 nt;%0A
-import com.yahoo.concurrent.DaemonThreadFactory;%0A
 impo
@@ -249,16 +249,16 @@
 actory;%0A
+
 import c
@@ -528,16 +528,94 @@
 imeUnit;
+%0Aimport java.util.logging.Logger;%0A%0Aimport static com.yahoo.log.LogLevel.DEBUG;
 %0A%0A/**%0A* 
@@ -707,16 +707,104 @@
 uctor %7B%0A
+    private static final Logger log = Logger.getLogger(Deconstructor.class.getName());%0A%0A
     priv
@@ -1352,16 +1352,18 @@
 table())
+ %7B
 %0A       
@@ -1375,51 +1375,117 @@
     
-executor.schedule(%0A                        
+log.log(DEBUG, () -%3E %22Scheduling deconstruction of %22 + abstractComponent);%0A                executor.schedule(
 new 
@@ -1552,16 +1552,30 @@
 CONDS);%0A
+            %7D%0A
         
@@ -2138,30 +2138,181 @@
     
-component.deconstruct(
+log.log(DEBUG, () -%3E %22Starting deconstruction of %22 + component);%0A            component.deconstruct();%0A            log.log(DEBUG, () -%3E %22Finished deconstructing %22 + component
 );%0A 

Add debug logging for AbstractComponent deconstruction.