/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.parallax.server.common.cloudsession.service.impl;

import com.github.kevinsawicki.http.HttpRequest;
import com.google.common.base.Strings;
import com.google.inject.Inject;
import com.parallax.server.common.cloudsession.db.utils.DataSourceSetup;
import com.parallax.server.common.cloudsession.db.utils.NeedsDataSource;
import com.parallax.server.common.cloudsession.service.AuthenticationTokenService;
import com.parallax.server.common.cloudsession.service.DatabaseMaintenanceService;
import java.sql.Connection;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import javax.sql.DataSource;
import org.apache.commons.configuration.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Michel
 */
public class DatabaseMaintenanceServiceImpl implements DatabaseMaintenanceService, NeedsDataSource, Runnable {

    private final Logger log = LoggerFactory.getLogger(DatabaseMaintenanceServiceImpl.class);

    private ScheduledExecutorService service;

//    private UserDao userDao;
    private AuthenticationTokenService authenticationTokenService;

    private Configuration configuration;

    private DataSource dataSource;

    public DatabaseMaintenanceServiceImpl() {
        DataSourceSetup.registerDataSourceUsers(this);
    }

//    @Inject
//    public void setUserDao(UserDao userDao) {
//        this.userDao = userDao;
//        setup();
//    }
    @Inject
    public void setAuthenticationTokenService(AuthenticationTokenService authenticationTokenService) {
        this.authenticationTokenService = authenticationTokenService;
        setup();
    }

    @Inject
    public void setConfiguration(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public void setup() {
        this.service = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread thread = new Thread(r);
                thread.setDaemon(true);
                //thread.setDaemon(false);
                return thread;
            }
        });
        this.service.scheduleAtFixedRate(this, 5, 5, TimeUnit.MINUTES);
    }

    @Override
    public void run() {
        //  log.info("Keep db active: {}", userDao.count());
        try {
            log.info("Clean authentication tokens and keep db active: removed {}", authenticationTokenService.cleanExpiredAutheticationTokens());
        } catch (Throwable t) {
            log.error("ALERT: Problem cleaning authentication tokens and keeping db active", t);
        }
        if (this.dataSource != null) {
            try {
                Connection connection = dataSource.getConnection();
                connection.prepareStatement("SELECT 1").executeQuery();
            } catch (Throwable t) {
                log.error("ALERT: Problem keeping db active", t);
            }
        }

        if (configuration != null) {
            String keepAliveUrl = configuration.getString("keepalive.url");
            if (!Strings.isNullOrEmpty(keepAliveUrl)) {
                try {
                    HttpRequest httpRequest = HttpRequest.get(keepAliveUrl);
                    log.info(httpRequest.body());
                } catch (Throwable t) {
                    log.error("ALERT: Problem calling keep-alive url", t);
                }
            }
        }
    }

}

@@ -678,36 +678,8 @@
 ce;%0A
-import java.sql.Connection;%0A
 impo
@@ -1866,32 +1866,16 @@
 ervice;%0A
-        setup();
 %0A    %7D%0A%0A
@@ -1994,16 +1994,33 @@
 ration;%0A
+        setup();%0A
     %7D%0A%0A 
@@ -2679,24 +2679,26 @@
 o.count());%0A
+//
         try 
@@ -2691,32 +2691,34 @@
 //        try %7B%0A
+//
             log.
@@ -2843,24 +2843,26 @@
 nTokens());%0A
+//
         %7D ca
@@ -2873,32 +2873,34 @@
 (Throwable t) %7B%0A
+//
             log.
@@ -2972,32 +2972,34 @@
 db active%22, t);%0A
+//
         %7D%0A      
@@ -2984,32 +2984,34 @@
 t);%0A//        %7D%0A
+//
         if (this
@@ -3025,32 +3025,34 @@
 urce != null) %7B%0A
+//
             try 
@@ -3045,32 +3045,34 @@
           try %7B%0A
+//
                 
@@ -3119,24 +3119,26 @@
 nnection();%0A
+//
             
@@ -3193,24 +3193,26 @@
 uteQuery();%0A
+//
             
@@ -3227,32 +3227,34 @@
 (Throwable t) %7B%0A
+//
                 
@@ -3299,24 +3299,26 @@
 ctive%22, t);%0A
+//
             
@@ -3311,32 +3311,34 @@
 //            %7D%0A
+//
         %7D%0A%0A     
@@ -3328,24 +3328,26 @@
 /        %7D%0A%0A
+//
         if (
@@ -3367,28 +3367,24 @@
  != null) %7B%0A
-    
         Stri
@@ -3447,28 +3447,24 @@
 %22);%0A        
- 
-   
 if (!Strings
@@ -3503,28 +3503,24 @@
             
-    
 try %7B%0A      
@@ -3529,20 +3529,16 @@
         
-  
-  
 HttpRequ
@@ -3594,36 +3594,32 @@
                 
-    
 log.info(httpReq
@@ -3636,36 +3636,32 @@
 ));%0A            
-  
-  
 %7D catch (Throwab
@@ -3664,28 +3664,24 @@
 owable t) %7B%0A
-    
             
@@ -3747,34 +3747,26 @@
             
-    %7D%0A    
+%7D%0A
         %7D%0A  
@@ -3755,32 +3755,34 @@
     %7D%0A        %7D%0A
+//
         %7D%0A    %7D%0A

Fix keep alive