<commit_before>/**
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.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://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */
package org.openmrs.scheduler.timer;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.Timer;
import java.util.TreeMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.api.APIException;
import org.openmrs.api.context.Context;
import org.openmrs.scheduler.SchedulerConstants;
import org.openmrs.scheduler.SchedulerException;
import org.openmrs.scheduler.SchedulerService;
import org.openmrs.scheduler.SchedulerUtil;
import org.openmrs.scheduler.Task;
import org.openmrs.scheduler.TaskDefinition;
import org.openmrs.scheduler.TaskFactory;
import org.openmrs.scheduler.db.SchedulerDAO;
import org.openmrs.util.InsertedOrderComparator;
import org.openmrs.util.OpenmrsMemento;

/**
 *  Simple scheduler service that uses JDK timer to trigger and execute scheduled tasks.
 */
public class TimerSchedulerServiceImpl implements SchedulerService { 
	
	/**
	 * Logger 
	 */
	private static Log log = 
		LogFactory.getLog( TimerSchedulerServiceImpl.class );

	/**
	 * Registered task list
	 */
	private Set<TaskDefinition> registeredTasks = 
		new HashSet<TaskDefinition>(); 
	
	/**
	 * Scheduled Task Map
	 */
	private static Map<Integer, TimerSchedulerTask> scheduledTasks = 
		new HashMap<Integer, TimerSchedulerTask>();

	/**
	 * A single timer used to keep track of all scheduled tasks. The Timer's
	 * associated thread should run as a daemon.  
	 * 
	 * A deamon thread is called for if the timer will be used to schedule 
	 * repeating "maintenance activities", which must be performed as long 
	 * as the application is running, but should not prolong the lifetime 
	 * of the application.
	 * 
	 * @see java.util.Timer#Timer(boolean)
	 */
	private Timer timerScheduler = new Timer(true);

	/**
	 *  Global data access object context
	 */
	private SchedulerDAO schedulerDAO;
	
	/**
	 * Gets the scheduler data access object.
	 * @return
	 */
	public SchedulerDAO getSchedulerDAO() { 
		return this.schedulerDAO;
	}
	
	/**
	 * Sets the scheduler data access object.
	 */
	public void setSchedulerDAO(SchedulerDAO dao) {
		this.schedulerDAO = dao;
	}
	
	/**
	 * Start up hook for the scheduler and all of its scheduled tasks.
	 */
	public void startup() {
		log.debug("Starting scheduler service ...");
		
		
		// Get all of the tasks in the database
		Collection <TaskDefinition> taskDefinitions = 			
			getSchedulerDAO().getTasks();
		
		// Iterate through the tasks and start them if their startOnStartup flag is true
		if (taskDefinitions != null) {
			for ( TaskDefinition taskDefinition : taskDefinitions ) { 
				try { 
					// If the task is configured to start on startup, we schedule it to run
					// Otherwise it needs to be started manually.
					if (taskDefinition.getStartOnStartup()) {
						scheduleTask(taskDefinition);
					}
					
				} catch (SchedulerException e) { 
					log.error("Failed to schedule task for class " + taskDefinition.getTaskClass(), e);
				}
			}
		}
	}
	
	/**
	 * Shutdown hook for the scheduler and all of its scheduled tasks.  
	 */
	public void shutdown() { 
		log.debug("Gracefully shutting down scheduler service ...");
		// gracefully shutdown all tasks and remove all references to the timers, scheduler
		try {<commit_msg>Catching Throwable on scheduled tasks startup to prevent complete halt of openmrs startup<commit_after>/**
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.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://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */
package org.openmrs.scheduler.timer;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.Timer;
import java.util.TreeMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.api.APIException;
import org.openmrs.api.context.Context;
import org.openmrs.scheduler.SchedulerConstants;
import org.openmrs.scheduler.SchedulerException;
import org.openmrs.scheduler.SchedulerService;
import org.openmrs.scheduler.SchedulerUtil;
import org.openmrs.scheduler.Task;
import org.openmrs.scheduler.TaskDefinition;
import org.openmrs.scheduler.TaskFactory;
import org.openmrs.scheduler.db.SchedulerDAO;
import org.openmrs.util.InsertedOrderComparator;
import org.openmrs.util.OpenmrsMemento;

/**
 *  Simple scheduler service that uses JDK timer to trigger and execute scheduled tasks.
 */
public class TimerSchedulerServiceImpl implements SchedulerService { 
	
	/**
	 * Logger 
	 */
	private static Log log = 
		LogFactory.getLog( TimerSchedulerServiceImpl.class );

	/**
	 * Registered task list
	 */
	private Set<TaskDefinition> registeredTasks = 
		new HashSet<TaskDefinition>(); 
	
	/**
	 * Scheduled Task Map
	 */
	private static Map<Integer, TimerSchedulerTask> scheduledTasks = 
		new HashMap<Integer, TimerSchedulerTask>();

	/**
	 * A single timer used to keep track of all scheduled tasks. The Timer's
	 * associated thread should run as a daemon.  
	 * 
	 * A deamon thread is called for if the timer will be used to schedule 
	 * repeating "maintenance activities", which must be performed as long 
	 * as the application is running, but should not prolong the lifetime 
	 * of the application.
	 * 
	 * @see java.util.Timer#Timer(boolean)
	 */
	private Timer timerScheduler = new Timer(true);

	/**
	 *  Global data access object context
	 */
	private SchedulerDAO schedulerDAO;
	
	/**
	 * Gets the scheduler data access object.
	 * @return
	 */
	public SchedulerDAO getSchedulerDAO() { 
		return this.schedulerDAO;
	}
	
	/**
	 * Sets the scheduler data access object.
	 */
	public void setSchedulerDAO(SchedulerDAO dao) {
		this.schedulerDAO = dao;
	}
	
	/**
	 * Start up hook for the scheduler and all of its scheduled tasks.
	 */
	public void startup() {
		log.debug("Starting scheduler service ...");
		
		
		// Get all of the tasks in the database
		Collection <TaskDefinition> taskDefinitions = 			
			getSchedulerDAO().getTasks();
		
		// Iterate through the tasks and start them if their startOnStartup flag is true
		if (taskDefinitions != null) {
			for ( TaskDefinition taskDefinition : taskDefinitions ) { 
				try { 
					// If the task is configured to start on startup, we schedule it to run
					// Otherwise it needs to be started manually.
					if (taskDefinition.getStartOnStartup()) {
						scheduleTask(taskDefinition);
					}
					
				} catch (Throwable t) { 
					log.error("Failed to schedule task for class " + taskDefinition.getTaskClass(), t);
				}
			}
		}
	}
	
	/**
	 * Shutdown hook for the scheduler and all of its scheduled tasks.  
	 */
	public void shutdown() { 
		log.debug("Gracefully shutting down scheduler service ...");
		// gracefully shutdown all tasks and remove all references to the timers, scheduler
		try {
{'fraction_non_alphanumeric': 0.08888335615585709, 'fraction_numerical': 0.0004979459728619445, 'mean_word_length': 3.695499707773232, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6440212', 'n_tokens_mistral': 2545, 'n_tokens_neox': 2263, 'n_words': 794}