/*
 * Copyright 2018 ThoughtWorks, Inc.
 *
 * 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 com.thoughtworks.go.server.service;

import com.thoughtworks.go.util.SystemEnvironment;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.ScheduledFuture;

@Component
public class ModeAwareThreadPoolTaskScheduler implements TaskScheduler, InitializingBean {
    private final SystemEnvironment systemEnvironment;
    private final ThreadPoolTaskScheduler scheduler;

    @Autowired
    public ModeAwareThreadPoolTaskScheduler(@Value("${go.executor.pool.size:10}") int poolSize, SystemEnvironment systemEnvironment) {
        this.systemEnvironment = systemEnvironment;
        this.scheduler = new ThreadPoolTaskScheduler();
        this.scheduler.setPoolSize(poolSize);
    }

    @Override
    public ScheduledFuture schedule(Runnable task, Trigger trigger) {
        return scheduler.schedule(wrap(task), trigger);
    }

    @Override
    public ScheduledFuture schedule(Runnable task, Date startTime) {
        return scheduler.schedule(wrap(task), startTime);
    }

    @Override
    public ScheduledFuture scheduleAtFixedRate(Runnable task, Date startTime, long period) {
        return scheduler.scheduleAtFixedRate(wrap(task), startTime, period);
    }

    @Override
    public ScheduledFuture scheduleAtFixedRate(Runnable task, long period) {
        return scheduler.scheduleAtFixedRate(wrap(task), period);
    }

    @Override
    public ScheduledFuture scheduleWithFixedDelay(Runnable task, Date startTime, long delay) {
        return scheduler.scheduleWithFixedDelay(wrap(task), startTime, delay);
    }

    @Override
    public ScheduledFuture scheduleWithFixedDelay(Runnable task, long delay) {
        return scheduler.scheduleWithFixedDelay(wrap(task), delay);
    }

    private Runnable wrap(Runnable original) {
        return new ModeAwareRunnable(original, systemEnvironment);
    }

    @Override
    public void afterPropertiesSet() {
        scheduler.afterPropertiesSet();
    }
}

/*
 * Copyright 2018 ThoughtWorks, Inc.
 *
 * 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 com.thoughtworks.go.server.service;

import com.thoughtworks.go.util.SystemEnvironment;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.ScheduledFuture;

@Component
public class ModeAwareThreadPoolTaskScheduler implements TaskScheduler, InitializingBean {
    private final SystemEnvironment systemEnvironment;
    private final ThreadPoolTaskScheduler scheduler;

    @Autowired
    public ModeAwareThreadPoolTaskScheduler(@Value("${go.executor.pool.size:10}") int poolSize, SystemEnvironment systemEnvironment) {
        this.systemEnvironment = systemEnvironment;
        this.scheduler = new ThreadPoolTaskScheduler();
        this.scheduler.setPoolSize(poolSize);
    }

    @Override
    public ScheduledFuture<?> schedule(Runnable task, Trigger trigger) {
        return scheduler.schedule(wrap(task), trigger);
    }

    @Override
    public ScheduledFuture<?> schedule(Runnable task, Date startTime) {
        return scheduler.schedule(wrap(task), startTime);
    }

    @Override
    public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Date startTime, long period) {
        return scheduler.scheduleAtFixedRate(wrap(task), startTime, period);
    }

    @Override
    public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, long period) {
        return scheduler.scheduleAtFixedRate(wrap(task), period);
    }

    @Override
    public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Date startTime, long delay) {
        return scheduler.scheduleWithFixedDelay(wrap(task), startTime, delay);
    }

    @Override
    public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, long delay) {
        return scheduler.scheduleWithFixedDelay(wrap(task), delay);
    }

    private Runnable wrap(Runnable original) {
        return new ModeAwareRunnable(original, systemEnvironment);
    }

    @Override
    public void afterPropertiesSet() {
        scheduler.afterPropertiesSet();
    }
}

Fix compiler warnings