
package io.bootique.job.consul;

import javax.inject.Singleton;

import io.bootique.ConfigModule;
import io.bootique.config.ConfigurationFactory;
import io.bootique.di.Binder;
import io.bootique.di.Key;
import io.bootique.di.Provides;
import io.bootique.job.consul.lock.ConsulLockHandlerProvider;
import io.bootique.job.lock.LockHandler;
import io.bootique.shutdown.ShutdownManager;

/**
 * @since 1.0.RC1
 */
public class ConsulJobModule extends ConfigModule {

    public ConsulJobModule() {
    }

    public ConsulJobModule(String configPrefix) {
        super(configPrefix);
    }

    @Override
    protected String defaultConfigPrefix() {
        return "job-consul";
    }

    @Override
    public void configure(Binder binder) {
        binder.bind(Key.get(LockHandler.class)).toProvider(ConsulLockHandlerProvider.class);
    }

    @Provides
    @Singleton
    public ConsulLockHandlerProvider provideConsulLockHandlerProvider(
            ConfigurationFactory configFactory, ShutdownManager shutdownManager) {

        ConsulJobConfig config = config(ConsulJobConfig.class, configFactory);

        return new ConsulLockHandlerProvider(
                config.getConsulHost(),
                config.getConsulPort(),
                config.getDataCenter(),
                config.getServiceGroup(),
                shutdownManager);
    }
}
