/**
 * 
 */
package com.spapageo.thmmyrss;

import java.net.URI;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.skife.jdbi.v2.DBI;
import org.slf4j.LoggerFactory;

import com.spapageo.thmmyrss.client.Fetcher;
import com.spapageo.thmmyrss.health.FetcherHealthCheck;
import com.spapageo.thmmyrss.jdbi.RssDAO;
import com.spapageo.thmmyrss.resourses.FeedResource;
import com.sun.jersey.api.client.Client;
import com.yammer.dropwizard.Service;
import com.yammer.dropwizard.assets.AssetsBundle;
import com.yammer.dropwizard.client.JerseyClientBuilder;
import com.yammer.dropwizard.config.Bootstrap;
import com.yammer.dropwizard.config.Environment;
import com.yammer.dropwizard.jdbi.DBIFactory;
import com.yammer.dropwizard.jdbi.bundles.DBIExceptionsBundle;


/**
 * @author Doom
 *
 */
public class ThmmyRssService extends Service<ThmmyRssConfiguration> {

	private Fetcher fetcher;
	private DBIFactory factory;
	private DBI jdbi;
	private RssDAO dao;
	private Client client;
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
	    
		//check if we are running on heroku
	    if(System.getenv("DATABASE_URL") != null){
	    	URI dbUri = new URI(System.getenv("DATABASE_URL"));
	    	String username = dbUri.getUserInfo().split(":")[0];
	    	String password = dbUri.getUserInfo().split(":")[1];
	    	String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + dbUri.getPath();

	    	System.setProperty("dw.http.port", System.getenv("PORT"));
	    	System.setProperty("dw.http.adminPort", System.getenv("PORT"));
	    	
	    	System.setProperty("dw.database.user", username);
	    	System.setProperty("dw.database.password", password);
	    	System.setProperty("dw.database.url", dbUrl);
	    	System.setProperty("dw.standalone","false");
	    }
		new ThmmyRssService().run(args);
	}

	@Override
	public void initialize(Bootstrap<ThmmyRssConfiguration> bootstrap) {
		bootstrap.setName("Thmmy Rss");
		bootstrap.addCommand(new UpdateDbCommand(this, "updatedb", "Update the rss database."));
		bootstrap.addBundle(new DBIExceptionsBundle());
		bootstrap.addBundle(new AssetsBundle("/assets/", "/"));
	}

	@Override
	public void run(ThmmyRssConfiguration config, Environment env)
			throws Exception {
	    client = new JerseyClientBuilder().using(config.getJerseyClientConfiguration()).using(env).withProperty("PROPERTY_CHUNKED_ENCODING_SIZE", 0).build();
	    factory = new DBIFactory();
	    jdbi = factory.build(env, config.getDatabase(), "mysql");
	    dao = jdbi.onDemand(RssDAO.class);
	    try{
	    	dao.createItemsTable();
	    }catch(Exception e){
	    	LoggerFactory.getLogger(getClass()).warn("Table probably already exists. Trying to continue",e);
	    }
	    
	    if(config.getStandalone()){
		    final ScheduledExecutorService exec = env.managedScheduledExecutorService("Ethmmy Parser Executor Service 1", 2);
		    this.fetcher = new Fetcher(client,config,dao);
		    exec.scheduleWithFixedDelay(fetcher, 0 , 30, TimeUnit.MINUTES);
		    env.addHealthCheck(new FetcherHealthCheck(config, fetcher));
	    }
	    env.addResource(new FeedResource(dao,config));
	}

	public Fetcher getFetcher() {
		return fetcher;
	}

	public DBIFactory getFactory() {
		return factory;
	}

	public DBI getJdbi() {
		return jdbi;
	}

	public RssDAO getDao() {
		return dao;
	}

	public Client getClient() {
		return client;
	}

}

@@ -2826,18 +2826,64 @@
 %7D%0D%0A%09    
+this.fetcher = new Fetcher(client,config,dao);
 %0D%0A
-
 %09    if(
@@ -3033,62 +3033,8 @@
 );%0D%0A
-%09%09    this.fetcher = new Fetcher(client,config,dao);%0D%0A
 %09%09  
@@ -3097,24 +3097,31 @@
 MINUTES);%0D%0A%09
+    %7D%0D%0A
 %09    env.add
@@ -3171,32 +3171,24 @@
 fetcher));%0D%0A
-%09    %7D%0D%0A
 %09    env.add

fix health check