/*
 * 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();
    }
}

@@ -1720,32 +1720,35 @@
  ScheduledFuture
+%3C?%3E
  schedule(Runnab
@@ -1771,24 +1771,24 @@
  trigger) %7B%0A
-
         retu
@@ -1870,32 +1870,35 @@
  ScheduledFuture
+%3C?%3E
  schedule(Runnab
@@ -2021,32 +2021,35 @@
  ScheduledFuture
+%3C?%3E
  scheduleAtFixed
@@ -2215,32 +2215,35 @@
  ScheduledFuture
+%3C?%3E
  scheduleAtFixed
@@ -2382,32 +2382,35 @@
  ScheduledFuture
+%3C?%3E
  scheduleWithFix
@@ -2554,32 +2554,32 @@
 %0A%0A    @Override%0A
-
     public Sched
@@ -2588,16 +2588,19 @@
 edFuture
+%3C?%3E
  schedul

Fix compiler warnings