package ca.uhn.fhir.jpa.model.sched;

/*-
 * #%L
 * HAPI FHIR Model
 * %%
 * Copyright (C) 2014 - 2019 University Health Network
 * %%
 * 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.
 * #L%
 */

import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.PersistJobDataAfterExecution;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@DisallowConcurrentExecution
@PersistJobDataAfterExecution
public abstract class FireAtIntervalJob implements Job {

	public static final String NEXT_EXECUTION_TIME = "NEXT_EXECUTION_TIME";
	private static final Logger ourLog = LoggerFactory.getLogger(FireAtIntervalJob.class);
	private final long myMillisBetweenExecutions;

	public FireAtIntervalJob(long theMillisBetweenExecutions) {
		myMillisBetweenExecutions = theMillisBetweenExecutions;
	}

	@Override
	public final void execute(JobExecutionContext theContext) {
		Long nextExecution = (Long) theContext.getJobDetail().getJobDataMap().get(NEXT_EXECUTION_TIME);

		if (nextExecution != null) {
			long cutoff = System.currentTimeMillis();
			if (nextExecution >= cutoff) {
				return;
			}
		}

		try {
			doExecute(theContext);
		} catch (Throwable t) {
			ourLog.error("Job threw uncaught exception", t);
		} finally {
			long newNextExecution = System.currentTimeMillis() + myMillisBetweenExecutions;
			theContext.getJobDetail().getJobDataMap().put(NEXT_EXECUTION_TIME, newNextExecution);
		}
	}

	protected abstract void doExecute(JobExecutionContext theContext);

}

@@ -1432,16 +1432,32 @@
 text) %7B%0A
+%09%09/** FIXME KHS%0A
 %09%09Long n
@@ -1657,16 +1657,155 @@
 toff) %7B%0A
+%09%09%09%09ourLog.info(%22Not enough time has passed since last execution of %7B%7D.  Skipping this one.%22, theContext.getJobDetail().getDescription());%0A
 %09%09%09%09retu
@@ -1817,16 +1817,18 @@
 %09%09%7D%0A%09%09%7D%0A
+*/
 %0A%09%09try %7B
@@ -1935,17 +1935,35 @@
  t);%0A%09%09%7D
- 
+%0A%09%09/** FIXME KHS%0A%09%09
 finally 
@@ -2140,16 +2140,22 @@
 n);%0A%09%09%7D%0A
+%09%09 */%0A
 %09%7D%0A%0A%09pro

disable gap time for now