/*
 * Copyright 2017 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.config.CaseInsensitiveString;
import com.thoughtworks.go.config.CruiseConfig;
import com.thoughtworks.go.config.PipelineConfig;
import com.thoughtworks.go.config.TimerConfig;
import com.thoughtworks.go.listener.ConfigChangedListener;
import com.thoughtworks.go.listener.EntityConfigChangedListener;
import com.thoughtworks.go.server.scheduling.BuildCauseProducerService;
import com.thoughtworks.go.server.service.result.ServerHealthStateOperationResult;
import com.thoughtworks.go.serverhealth.HealthStateScope;
import com.thoughtworks.go.serverhealth.HealthStateType;
import com.thoughtworks.go.serverhealth.ServerHealthService;
import com.thoughtworks.go.serverhealth.ServerHealthState;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.List;

import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.JobKey.jobKey;
import static org.quartz.TriggerBuilder.newTrigger;
import static org.quartz.TriggerKey.triggerKey;

/**
 * @understands scheduling pipelines based on a timer
 */
@Component
public class TimerScheduler implements ConfigChangedListener {
    private static final Logger LOG = LoggerFactory.getLogger(TimerScheduler.class);

    private GoConfigService goConfigService;
    private BuildCauseProducerService buildCauseProducerService;
    private ServerHealthService serverHealthService;
    private Scheduler quartzScheduler;
    protected static final String PIPELINE_TRIGGGER_TIMER_GROUP = "PIPELINE_TRIGGGER_TIMER_GROUP";
    protected static final String BUILD_CAUSE_PRODUCER_SERVICE = "BuildCauseProducerService";
    protected static final String PIPELINE_CONFIG = "PipelineConfig";

    @Autowired
    public TimerScheduler(Scheduler scheduler, GoConfigService goConfigService,
                          BuildCauseProducerService buildCauseProducerService, ServerHealthService serverHealthService) {
        this.goConfigService = goConfigService;
        this.buildCauseProducerService = buildCauseProducerService;
        this.quartzScheduler = scheduler;
        this.serverHealthService = serverHealthService;
    }

    public void initialize() {
        scheduleAllJobs(goConfigService.getAllPipelineConfigs());
        goConfigService.register(this);
        goConfigService.register(pipelineConfigChangedListener());
    }

    protected EntityConfigChangedListener<PipelineConfig> pipelineConfigChangedListener() {
        return new EntityConfigChangedListener<PipelineConfig>() {
            @Override
            public void onEntityConfigChange(PipelineConfig pipelineConfig) {
                unscheduleJob(CaseInsensitiveString.str(pipelineConfig.name()));
                scheduleJob(quartzScheduler, pipelineConfig);
            }
        };
    }

    private void scheduleAllJobs(List<PipelineConfig> pipelineConfigs) {
        for (PipelineConfig pipelineConfig : pipelineConfigs) {
            scheduleJob(quartzScheduler, pipelineConfig);
        }
    }

    private void scheduleJob(Scheduler scheduler, PipelineConfig pipelineConfig) {
        TimerConfig timer = pipelineConfig.getTimer();
        if (timer != null) {
            try {
                CronTrigger trigger = newTrigger()
                        .withIdentity(triggerKey(CaseInsensitiveString.str(pipelineConfig.name()), PIPELINE_TRIGGGER_TIMER_GROUP))
                        .withSchedule(cronSchedule(new CronExpression(timer.getTimerSpec())))
                        .build();


                JobDetail jobDetail = newJob()
                        .withIdentity(jobKey(CaseInsensitiveString.str(pipelineConfig.name()), PIPELINE_TRIGGGER_TIMER_GROUP))
                        .ofType(SchedulePipelineQuartzJob.class)
                        .usingJobData(jobDataMapFor(pipelineConfig))
                        .build();

                scheduler.scheduleJob(jobDetail, trigger);
                LOG.info("Initialized timer for pipeline {} with {}", pipelineConfig.name(), timer.getTimerSpec());
            } catch (ParseException e) {
                showPipelineError(pipelineConfig, e,
                        "Bad timer specification for timer in Pipeline: " + pipelineConfig.name(),
                        "Cannot schedule pipeline using the timer");
            } catch (SchedulerException e) {
                showPipelineError(pipelineConfig, e,
                        "Could not register pipeline '" + pipelineConfig.name() + "' with timer",
                        "");
            }
        }
    }

    private void showGlobalError(String msg, SchedulerException e) {
        LOG.error(msg, e);
        serverHealthService.update(
                ServerHealthState.error(msg, "Cannot schedule pipelines using the timer",
                        HealthStateType.general(HealthStateScope.GLOBAL)));
    }

    private void showPipelineError(PipelineConfig pipelineConfig, Exception e, String msg, String description) {
        LOG.error(msg, e);
        serverHealthService.update(
                ServerHealthState.error(msg,
                        description,
                        HealthStateType.general(HealthStateScope.forPipeline(CaseInsensitiveString.str(pipelineConfig.name())))));
    }

    private JobDataMap jobDataMapFor(PipelineConfig pipelineConfig) {
        JobDataMap map = new JobDataMap();
        map.put(BUILD_CAUSE_PRODUCER_SERVICE, buildCauseProducerService);
        map.put(PIPELINE_CONFIG, pipelineConfig);
        return map;
    }

    public void onConfigChange(CruiseConfig newCruiseConfig) {
        unscheduleAllJobs();
        scheduleAllJobs(newCruiseConfig.getAllPipelineConfigs());
    }

    private void unscheduleAllJobs() {
        try {
            quartzScheduler.clear();
        } catch (SchedulerException e) {
            LOG.error("Could not unschedule quartz jobs", e);
        }
    }

    private void unscheduleJob(String pipelineName) {
        try {
            JobKey jobKey = jobKey(pipelineName, PIPELINE_TRIGGGER_TIMER_GROUP);
            if (quartzScheduler.getJobDetail(jobKey) != null) {
                quartzScheduler.unscheduleJob(triggerKey(pipelineName, PIPELINE_TRIGGGER_TIMER_GROUP));
                quartzScheduler.deleteJob(jobKey);
            }
        } catch (SchedulerException e) {
            LOG.error("Could not unschedule quartz jobs", e);
        }
    }

    public static class SchedulePipelineQuartzJob implements Job {
        public void execute(JobExecutionContext context) {
            JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
            BuildCauseProducerService buildCauseProducerService = (BuildCauseProducerService) jobDataMap.get(BUILD_CAUSE_PRODUCER_SERVICE);
            PipelineConfig pipelineConfig = (PipelineConfig) jobDataMap.get(PIPELINE_CONFIG);
            buildCauseProducerService.timerSchedulePipeline(pipelineConfig, new ServerHealthStateOperationResult());
        }
    }
}

@@ -16,9 +16,9 @@
  201
-7
+8
  Tho
@@ -1603,16 +1603,38 @@
 il.List;
+%0Aimport java.util.Set;
 %0A%0Aimport
@@ -1869,16 +1869,81 @@
 ggerKey;
+%0Aimport static org.quartz.impl.matchers.GroupMatcher.groupEquals;
 %0A%0A/**%0A *
@@ -5456,313 +5456,8 @@
  %7D%0A%0A
-    private void showGlobalError(String msg, SchedulerException e) %7B%0A        LOG.error(msg, e);%0A        serverHealthService.update(%0A                ServerHealthState.error(msg, %22Cannot schedule pipelines using the timer%22,%0A                        HealthStateType.general(HealthStateScope.GLOBAL)));%0A    %7D%0A%0A
     
@@ -6346,32 +6346,208 @@
     
-quartzScheduler.clear();
+Set%3CJobKey%3E jobKeys = quartzScheduler.getJobKeys(groupEquals(PIPELINE_TRIGGGER_TIMER_GROUP));%0A            for (JobKey jobKey : jobKeys) %7B%0A                unscheduleJob(jobKey.getName());%0A            %7D
 %0A   

Clear the quartz jobs in the pipeline timer namespace, not all jobs