Project

General

Profile

1 4959 daigle
/**
2
 *  '$RCSfile$'
3 4971 daigle
 *    Purpose: A Class that handles scheduling workflow jobs
4 4959 daigle
 *  Copyright: 2009 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Michael Daigle
7
 *
8
 *   '$Author: daigle $'
9
 *     '$Date: 2009-03-25 13:41:15 -0800 (Wed, 25 Mar 2009) $'
10
 * '$Revision: 4861 $'
11
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
 */
26
27
package edu.ucsb.nceas.metacat.workflow;
28
29 4967 daigle
import java.io.IOException;
30 5027 daigle
import java.io.PrintWriter;
31 4959 daigle
import java.util.Calendar;
32
import java.util.Hashtable;
33
import java.util.HashMap;
34
35
import javax.servlet.http.HttpServletRequest;
36
import javax.servlet.http.HttpServletResponse;
37
38
import org.apache.log4j.Logger;
39
40
import edu.ucsb.nceas.metacat.scheduler.BaseScheduler;
41 4967 daigle
import edu.ucsb.nceas.metacat.scheduler.ScheduledJobAccess;
42
import edu.ucsb.nceas.metacat.scheduler.ScheduledJobDAO;
43 4959 daigle
import edu.ucsb.nceas.metacat.scheduler.SchedulerService;
44
import edu.ucsb.nceas.metacat.scheduler.MetacatSchedulerException;
45 5015 daigle
import edu.ucsb.nceas.metacat.shared.AccessException;
46
import edu.ucsb.nceas.metacat.shared.ServiceException;
47 4967 daigle
import edu.ucsb.nceas.metacat.util.ErrorSendingErrorException;
48
import edu.ucsb.nceas.metacat.util.ResponseUtil;
49 5005 daigle
import edu.ucsb.nceas.utilities.DateUtil;
50
import edu.ucsb.nceas.utilities.UtilException;
51 4959 daigle
52 4971 daigle
/**
53
 * @author daigle
54
 *
55
 */
56 4959 daigle
public class WorkflowScheduler extends BaseScheduler {
57
58
	private static WorkflowScheduler workflowScheduler = null;
59
60
	private static Logger logMetacat = Logger.getLogger(WorkflowScheduler.class);
61
62 4967 daigle
	private static String WORKFLOW_JOB_GROUP = "workflow";
63
	private static String WORKFLOW_JOB_CLASS = "edu.ucsb.nceas.metacat.workflow.WorkflowJob";
64 4959 daigle
65
	/**
66
	 * private constructor since this is a singleton
67
	 */
68
	private WorkflowScheduler()  {}
69
70
	/**
71
	 * Get the single instance of SchedulerService.
72
	 *
73
	 * @return the single instance of SchedulerService
74
	 */
75
	public static WorkflowScheduler getInstance() {
76
		if (workflowScheduler == null) {
77
			workflowScheduler = new WorkflowScheduler();
78
		}
79
		return workflowScheduler;
80
	}
81
82 4971 daigle
	/**
83
	 * Scheduling a workflow
84
	 *
85
	 * @param request
86
	 *            the servlet request object
87
	 * @param response
88
	 *            the servlet response object
89
	 * @param params
90
	 *            the request parameters
91
	 * @param username
92
	 *            the user
93
	 * @param groups
94
	 *            the user's group
95
	 */
96 4967 daigle
	public void scheduleJob(HttpServletRequest request, HttpServletResponse response,
97
			Hashtable<String, String[]> params, String username,
98 4959 daigle
			String[] groups) throws MetacatSchedulerException {
99 4967 daigle
100 4959 daigle
		String delays[] = params.get("delay");
101
		String startTimes[] = params.get("starttime");
102 5012 daigle
		String endTimes[] = params.get("endtime");
103 4959 daigle
		HashMap<String, String> jobParams = new HashMap<String, String>();
104
		Calendar startCal = null;
105 5012 daigle
		Calendar endCal = null;
106 4959 daigle
107
		try {
108
			SchedulerService schedulerService = SchedulerService.getInstance();
109
110 5005 daigle
			// get start time or delay.  Start time takes precedence if both exist.
111 4971 daigle
			if (startTimes != null && startTimes.length > 0) {
112 5011 daigle
				startCal = DateUtil.humanReadableToCalendar(startTimes[0], "MM/dd/yyyy HH:mm:ss");
113 4971 daigle
			} else if (delays != null && delays.length > 0) {
114
				startCal = schedulerService.getStartDateFromDelay(delays[0]);
115 4959 daigle
			} else {
116
				// if delay and starttime were not provided, set date to now.
117
				startCal = Calendar.getInstance();
118
			}
119 5012 daigle
120
			// get end time.  null is fine.
121
			if (endTimes != null && endTimes.length > 0) {
122
				endCal = DateUtil.humanReadableToCalendar(endTimes[0], "MM/dd/yyyy HH:mm:ss");
123
			}
124 4959 daigle
125 4971 daigle
			// interval value must exist
126 4967 daigle
			String intervalValues[] = params.get("intervalvalue");
127
			if (intervalValues == null || intervalValues.length == 0) {
128
				throw new MetacatSchedulerException("WorkflowScheduler.scheduleJob - intervalvalue field must be populated "
129 4959 daigle
								+ "in scheduler parameters when scheduling job.");
130
			}
131 4967 daigle
			String intervalStrValue = intervalValues[0];
132
			int intervalValue = Integer.parseInt(intervalStrValue);
133
134 4971 daigle
			// interval unit must exist
135 4967 daigle
			String intervalUnits[] = params.get("intervalunit");
136
			if (intervalUnits == null || intervalUnits.length == 0) {
137
				throw new MetacatSchedulerException("WorkflowScheduler.scheduleJob - intervalunit field must be populated "
138
								+ "in scheduler parameters when scheduling job.");
139
			}
140
			String intervalUnit = intervalUnits[0];
141 4959 daigle
142 5012 daigle
			// workflow id must exist.  Add to job params
143 4971 daigle
			String workflowids[] = params.get("workflowid");
144
			if (workflowids == null || workflowids.length == 0) {
145
				throw new MetacatSchedulerException("WorkflowScheduler.scheduleJob - workflowid field must be populated "
146
								+ "in scheduler parameters when scheduling job.");
147 4959 daigle
			}
148 4971 daigle
			jobParams.put("workflowid", workflowids[0]);
149
150
			// kar id must exist.  Add to job params
151
			String karids[] = params.get("karid");
152
			if (karids == null || karids.length == 0) {
153
				throw new MetacatSchedulerException("WorkflowScheduler.scheduleJob - karid field must be populated "
154
								+ "in scheduler parameters when scheduling job.");
155
			}
156
			jobParams.put("karid", karids[0]);
157
158
159
			// workflow name unit must exist.  Add to job params
160
			String workflownames[] = params.get("workflowname");
161
			if (workflownames == null || workflownames.length == 0) {
162
				throw new MetacatSchedulerException("WorkflowScheduler.scheduleJob - workflowname field must be populated "
163
								+ "in scheduler parameters when scheduling job.");
164
			}
165
			jobParams.put("workflowname", workflownames[0]);
166 4959 daigle
167 4967 daigle
			String jobName = WORKFLOW_JOB_GROUP
168 4959 daigle
					+ Calendar.getInstance().getTimeInMillis();
169
170 4971 daigle
			// Schedule the job
171 5012 daigle
			String xmlResult = schedulerService.scheduleJob(jobName, startCal, endCal, intervalValue, intervalUnit,
172 4967 daigle
					WORKFLOW_JOB_CLASS, WORKFLOW_JOB_GROUP, jobParams, username, groups);
173
174 5027 daigle
			ResponseUtil.sendSuccessXML(response, xmlResult);
175 4967 daigle
176 5005 daigle
		} catch (UtilException ue) {
177 5027 daigle
			throw new MetacatSchedulerException("WorkflowScheduler.scheduleJob - "
178
					+ "Utility issue when scheduling job: " + ue.getMessage());
179 4959 daigle
		} catch (ServiceException se) {
180 5027 daigle
			throw new MetacatSchedulerException("WorkflowScheduler.scheduleJob - "
181
					+ "Service issue when scheduling job", se);
182 4967 daigle
		} catch (ErrorSendingErrorException esee) {
183 5027 daigle
			throw new MetacatSchedulerException("WorkflowScheduler.scheduleJob - "
184
					+ "Issue sending error when scheduling job: " + esee.getMessage());
185 4959 daigle
		}
186
	}
187
188 4971 daigle
	/**
189
	 * Unschedule a job
190
	 *
191
	 * @param request
192
	 *            the servlet request object
193
	 * @param response
194
	 *            the servlet response object
195
	 * @param params
196
	 *            the request parameters
197
	 * @param username
198
	 *            the user
199
	 * @param groups
200
	 *            the user's group
201
	 */
202 4967 daigle
	public void unScheduleJob(HttpServletRequest request, HttpServletResponse response,
203
			Hashtable<String, String[]> params, String username, String[] groups)
204
			throws MetacatSchedulerException {
205
		try {
206 4971 daigle
			// workflow job id must exist
207 4967 daigle
			String jobNames[] = params.get("workflowjobid");
208
			if (jobNames == null || jobNames.length == 0) {
209
				throw new MetacatSchedulerException("SchedulerService.unScheduleJob - workflowjobid "
210
						+ "field must be populated in scheduler parameters when unscheduling job.");
211
			}
212
			String jobName = jobNames[0];
213
214 4971 daigle
			// unschedule the job
215 4967 daigle
			SchedulerService schedulerService = SchedulerService.getInstance();
216
			String xmlResult = schedulerService.unScheduleJob(jobName, username, groups);
217
218
			ResponseUtil.sendSuccessXML(response, xmlResult);
219 5027 daigle
220 4967 daigle
		} catch (ServiceException se) {
221 5027 daigle
			throw new MetacatSchedulerException("WorkflowScheduler.unScheduleJob - "
222
					+ "Service issue unscheduling job", se);
223 4967 daigle
		} catch (Exception e) {
224 5027 daigle
			throw new MetacatSchedulerException("WorkflowScheduler.unScheduleJob - "
225
					+ "Generic issue unscheduling job: " + e.getMessage());
226 4967 daigle
		}
227 4959 daigle
	}
228
229 4971 daigle
	/**
230
	 * reschedule job
231
	 *
232
	 * @param request
233
	 *            the servlet request object
234
	 * @param response
235
	 *            the servlet response object
236
	 * @param params
237
	 *            the request parameters
238
	 * @param username
239
	 *            the user
240
	 * @param groups
241
	 *            the user's group
242
	 */
243 4967 daigle
	public void reScheduleJob(HttpServletRequest request, HttpServletResponse response,
244
			Hashtable<String, String[]> params, String username,
245
			String[] groups) throws MetacatSchedulerException {
246
247
		try {
248 4971 daigle
			// workflow job id must exist
249
			String jobNames[] = params.get("workflowjobid");
250 4967 daigle
			if (jobNames == null || jobNames.length == 0) {
251
				throw new MetacatSchedulerException("WorkflowScheduler.reScheduleJob - workflowjobid field must be populated "
252
						+ "in scheduler parameters when rescheduling job.");
253 4971 daigle
			}
254 4967 daigle
			String jobName = jobNames[0];
255
256
			ScheduledJobAccess jobAccess = new ScheduledJobAccess();
257
			ScheduledJobDAO jobDAO = jobAccess.getJobByName(jobName);
258
259 4971 daigle
			// reschedule the job
260 4967 daigle
			SchedulerService schedulerService = SchedulerService.getInstance();
261
			String result = schedulerService.rescheduleJob(jobDAO, username, groups);
262
263 5027 daigle
			ResponseUtil.sendSuccessXML(response, result);
264 4967 daigle
265
		} catch (AccessException ae) {
266 5027 daigle
			throw new MetacatSchedulerException("WorkflowScheduler.reScheduleJob - "
267
					+ "DB access issue when scheduling job  : ", ae);
268 4967 daigle
		} catch (ServiceException se) {
269 5027 daigle
			throw new MetacatSchedulerException("WorkflowScheduler.reScheduleJob - "
270
					+ "Service issue scheduling job", se);
271 4967 daigle
		} catch (ErrorSendingErrorException esee) {
272 5027 daigle
			throw new MetacatSchedulerException("WorkflowScheduler.reScheduleJob - "
273
					+ "Issue sending erro when scheduling job: " + esee.getMessage());
274 4967 daigle
		}
275 4959 daigle
	}
276
277 4971 daigle
	/**
278
	 * delete job - to be implemented
279
	 */
280 4967 daigle
	public void deleteJob(HttpServletRequest request, HttpServletResponse response,
281
			Hashtable<String, String[]> params, String username, String[] groups) throws MetacatSchedulerException {
282 5013 daigle
		 try {
283 5027 daigle
			// workflow job id must exist
284
			String jobNames[] = params.get("workflowjobid");
285
			if (jobNames == null || jobNames.length == 0) {
286
				throw new MetacatSchedulerException(
287
						"WorkflowScheduler.deleteJob - workflowname field must be populated "
288
								+ "in scheduler parameters when deleting job.");
289
			}
290
			String jobName = jobNames[0];
291 5013 daigle
292 5027 daigle
			ScheduledJobAccess jobAccess = new ScheduledJobAccess();
293
			ScheduledJobDAO jobDAO = jobAccess.getJobByName(jobName);
294
295
			// delete the job
296
			SchedulerService schedulerService = SchedulerService.getInstance();
297
			String result = schedulerService.deleteJob(jobDAO, username, groups);
298
299 5013 daigle
			ResponseUtil.sendSuccessXML(response, result);
300 5027 daigle
301 5013 daigle
		} catch (AccessException ae) {
302 5027 daigle
			throw new MetacatSchedulerException("WorkflowScheduler.deleteJob - "
303
					+ "DB access issue when deleting job  : ", ae);
304 5013 daigle
		} catch (ServiceException se) {
305 5027 daigle
			throw new MetacatSchedulerException("WorkflowScheduler.deleteJob - "
306
					+ "Service issue deleting job", se);
307 5013 daigle
		} catch (ErrorSendingErrorException esee) {
308 5027 daigle
			throw new MetacatSchedulerException("WorkflowScheduler.deleteJob - "
309
					+ "Issue sending erro when deleting job: " + esee.getMessage());
310 5013 daigle
		}
311 4967 daigle
	}
312
313 4971 daigle
	/**
314
	 * get job information for a given workflow in xml format
315
	 *
316
	 * @param request
317
	 *            the servlet request object
318
	 * @param response
319
	 *            the servlet response object
320
	 * @param params
321
	 *            the request parameters
322
	 * @param username
323
	 *            the user
324
	 * @param groups
325
	 *            the user's group
326
	 */
327 4967 daigle
	public void getJobs(HttpServletRequest request, HttpServletResponse response,
328
			Hashtable<String, String[]> params, String username, String[] groups) throws MetacatSchedulerException {
329
330 5027 daigle
		String workflowId = null;
331 4967 daigle
332 5027 daigle
		String workflowIds[] = params.get("workflowid");
333
		if (workflowIds != null && workflowIds.length != 0) {
334
			workflowId = workflowIds[0];
335 4959 daigle
		}
336
337 5027 daigle
        PrintWriter out = null;
338 4959 daigle
		try {
339 4971 daigle
			// get the job info in xml format
340 5027 daigle
			out = response.getWriter();
341
			SchedulerService.getInstance().getJobsInfoXML(WORKFLOW_JOB_GROUP,
342
					"workflowid", workflowId, out);
343
		} catch (ServiceException se) {
344
			throw new MetacatSchedulerException("WorkflowScheduler.getJobs - "
345
					+ "Service error when transforming XML for workflow id: "
346
					+ workflowId + " : " + se.getMessage());
347
		} catch (IOException ioe) {
348
			throw new MetacatSchedulerException("WorkflowScheduler.getJobs - "
349
					+ "I/O error when transforming XML for workflow id: "
350
					+ workflowId + " : " + ioe.getMessage());
351
		} finally {
352
			if (out != null) {
353
				out.close();
354 4967 daigle
			}
355 5027 daigle
		}
356
	}
357 4959 daigle
}