Project

General

Profile

« Previous | Next » 

Revision 4971

Added by daigle almost 15 years ago

Beef up comments

View differences:

WorkflowScheduler.java
1 1
/**
2 2
 *  '$RCSfile$'
3
 *    Purpose: A Class that handles scheduling tasks 
3
 *    Purpose: A Class that handles scheduling workflow jobs 
4 4
 *  Copyright: 2009 Regents of the University of California and the
5 5
 *             National Center for Ecological Analysis and Synthesis
6 6
 *    Authors: Michael Daigle
......
31 31
import java.text.ParseException;
32 32
import java.util.Calendar;
33 33
import java.util.Date;
34
import java.util.Enumeration;
35 34
import java.util.Hashtable;
36 35
import java.util.HashMap;
37 36

  
......
54 53
import edu.ucsb.nceas.metacat.util.RequestUtil;
55 54
import edu.ucsb.nceas.shared.AccessException;
56 55

  
56
/**
57
 * @author daigle
58
 *
59
 */
60
/**
61
 * @author daigle
62
 *
63
 */
57 64
public class WorkflowScheduler extends BaseScheduler {
58 65
	
59 66
	private static WorkflowScheduler workflowScheduler = null;
......
80 87
		return workflowScheduler;
81 88
	}
82 89
	
90
	/**
91
	 * Scheduling a workflow
92
	 * 
93
	 * @param request
94
	 *            the servlet request object
95
	 * @param response
96
	 *            the servlet response object
97
	 * @param params
98
	 *            the request parameters
99
	 * @param username
100
	 *            the user
101
	 * @param groups
102
	 *            the user's group
103
	 */
83 104
	public void scheduleJob(HttpServletRequest request, HttpServletResponse response, 
84 105
			Hashtable<String, String[]> params, String username,
85 106
			String[] groups) throws MetacatSchedulerException {
......
92 113
		try {
93 114
			SchedulerService schedulerService = SchedulerService.getInstance();
94 115

  
95
			if (delays != null && delays.length > 0) {
96
				startCal = schedulerService.getStartDateFromDelay(delays[0]);
97
			} else if (startTimes != null && startTimes.length > 0) {
116
			// get start time or delay.  Start time takes precidence if both exist.
117
			if (startTimes != null && startTimes.length > 0) {
98 118
				SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z");
99 119
				Date startDate = dateFormat.parse(startTimes[0]);
100 120
				startCal = Calendar.getInstance();
101 121
				startCal.setTime(startDate);
122
			} else if (delays != null && delays.length > 0) {
123
				startCal = schedulerService.getStartDateFromDelay(delays[0]);
102 124
			} else {
103 125
				// if delay and starttime were not provided, set date to now.
104 126
				startCal = Calendar.getInstance();
105 127
			}
106 128

  
129
			// interval value must exist
107 130
			String intervalValues[] = params.get("intervalvalue");
108 131
			if (intervalValues == null || intervalValues.length == 0) {
109 132
				throw new MetacatSchedulerException("WorkflowScheduler.scheduleJob - intervalvalue field must be populated "
......
112 135
			String intervalStrValue = intervalValues[0];
113 136
			int intervalValue = Integer.parseInt(intervalStrValue);
114 137
			
138
			// interval unit must exist
115 139
			String intervalUnits[] = params.get("intervalunit");
116 140
			if (intervalUnits == null || intervalUnits.length == 0) {
117 141
				throw new MetacatSchedulerException("WorkflowScheduler.scheduleJob - intervalunit field must be populated "
......
119 143
			}
120 144
			String intervalUnit = intervalUnits[0];
121 145

  
122
			Enumeration<String> paramNames = params.keys();
123
			while (paramNames.hasMoreElements()) {
124
				String paramName = paramNames.nextElement();
125
				if (paramName.startsWith("jobparam_")) {
126
					jobParams.put(paramName.substring(9), params.get(paramName)[0]);
127
				}
146
			// workflow id unit must exist.  Add to job params
147
			String workflowids[] = params.get("workflowid");
148
			if (workflowids == null || workflowids.length == 0) {
149
				throw new MetacatSchedulerException("WorkflowScheduler.scheduleJob - workflowid field must be populated "
150
								+ "in scheduler parameters when scheduling job.");
128 151
			}
152
			jobParams.put("workflowid", workflowids[0]);
153
			
154
			// kar id must exist.  Add to job params
155
			String karids[] = params.get("karid");
156
			if (karids == null || karids.length == 0) {
157
				throw new MetacatSchedulerException("WorkflowScheduler.scheduleJob - karid field must be populated "
158
								+ "in scheduler parameters when scheduling job.");
159
			}
160
			jobParams.put("karid", karids[0]);
161
			
162
			
163
			// workflow name unit must exist.  Add to job params
164
			String workflownames[] = params.get("workflowname");
165
			if (workflownames == null || workflownames.length == 0) {
166
				throw new MetacatSchedulerException("WorkflowScheduler.scheduleJob - workflowname field must be populated "
167
								+ "in scheduler parameters when scheduling job.");
168
			}
169
			jobParams.put("workflowname", workflownames[0]);
129 170

  
130 171
			String jobName = WORKFLOW_JOB_GROUP
131 172
					+ Calendar.getInstance().getTimeInMillis();
132 173

  
174
			// Schedule the job
133 175
			String xmlResult = schedulerService.scheduleJob(jobName, startCal, intervalValue, intervalUnit,
134 176
					WORKFLOW_JOB_CLASS, WORKFLOW_JOB_GROUP, jobParams, username, groups);
135 177

  
178
			// if there is a forwardto param on the request, then send the user to the page
179
			// referenced in that param, otherwise, just send the xml back.
136 180
			String forwardtos[] = params.get("forwardto");
137 181
			String forwardto = null;
138 182
			if (forwardtos != null && forwardtos.length > 0) {
......
167 211
		}
168 212
	}
169 213
	
214
	/**
215
	 * Unschedule a job
216
	 * 
217
	 * @param request
218
	 *            the servlet request object
219
	 * @param response
220
	 *            the servlet response object
221
	 * @param params
222
	 *            the request parameters
223
	 * @param username
224
	 *            the user
225
	 * @param groups
226
	 *            the user's group
227
	 */
170 228
	public void unScheduleJob(HttpServletRequest request, HttpServletResponse response,
171 229
			Hashtable<String, String[]> params, String username, String[] groups)
172 230
			throws MetacatSchedulerException {
173 231
		try {
232
			// workflow job id must exist
174 233
			String jobNames[] = params.get("workflowjobid");
175 234
			if (jobNames == null || jobNames.length == 0) {
176 235
				throw new MetacatSchedulerException("SchedulerService.unScheduleJob - workflowjobid " 
......
178 237
			}
179 238
			String jobName = jobNames[0];
180 239

  
240
			// unschedule the job
181 241
			SchedulerService schedulerService = SchedulerService.getInstance();
182 242
			String xmlResult = schedulerService.unScheduleJob(jobName, username, groups);
183 243
			
244
			
184 245
			String forwardtos[] = params.get("forwardto");
185 246
			String forwardto = null;
186 247
			if (forwardtos != null && forwardtos.length > 0) {
187 248
				forwardto = forwardtos[0];
188 249
			}
189 250
			
251
			// if there is a forwardto param on the request, then send the user to the page
252
			// referenced in that param, otherwise, just send the xml back.
190 253
			if (forwardto != null) {
191 254
				String qformats[] = params.get("qformat");
192 255
				String qformat = null;
......
197 260
				ScheduledJobAccess jobAccess = new ScheduledJobAccess();
198 261
				ScheduledJobDAO jobDAO = jobAccess.getJobByName(jobName);
199 262
				
263
				// we need to include the workflow id in the forward url
200 264
				String workflowId = null;
201 265
				ScheduledJobParamDAO jobParamDAO = jobDAO.getJobParam("workflowid");
202 266
				if (jobParamDAO != null) {
......
221 285
		}
222 286
	}
223 287
	
288
	/**
289
	 * reschedule job
290
	 * 
291
	 * @param request
292
	 *            the servlet request object
293
	 * @param response
294
	 *            the servlet response object
295
	 * @param params
296
	 *            the request parameters
297
	 * @param username
298
	 *            the user
299
	 * @param groups
300
	 *            the user's group
301
	 */
224 302
	public void reScheduleJob(HttpServletRequest request, HttpServletResponse response, 
225 303
			Hashtable<String, String[]> params, String username,
226 304
			String[] groups) throws MetacatSchedulerException {
227 305
		 		
228 306
		try {
229
			String jobNames[] = params.get("workflowjobid");
230
	
307
			// workflow job id must exist
308
			String jobNames[] = params.get("workflowjobid");	
231 309
			if (jobNames == null || jobNames.length == 0) {
232 310
				throw new MetacatSchedulerException("WorkflowScheduler.reScheduleJob - workflowjobid field must be populated "
233 311
						+ "in scheduler parameters when rescheduling job.");
234
			}
235
			
312
			}			
236 313
			String jobName = jobNames[0];
237 314
	
238 315
			ScheduledJobAccess jobAccess = new ScheduledJobAccess();
239 316
			ScheduledJobDAO jobDAO = jobAccess.getJobByName(jobName);
240 317
			
318
			// reschedule the job
241 319
			SchedulerService schedulerService = SchedulerService.getInstance();
242 320
			String result = schedulerService.rescheduleJob(jobDAO, username, groups);
243 321

  
322
			// if there is a forwardto param on the request, then send the user to the page
323
			// referenced in that param, otherwise, just send the xml back.
244 324
			String forwardtos[] = params.get("forwardto");
245 325
			String forwardto = null;
246 326
			if (forwardtos != null && forwardtos.length > 0) {
......
254 334
					qformat = qformats[0];
255 335
				}
256 336
				
337
				// we need to include the workflow id in the forward url
257 338
				String workflowId = null;
258 339
				ScheduledJobParamDAO jobParamDAO = jobDAO.getAllJobParams().get("workflowid");
259 340
				if (jobParamDAO != null) {
......
280 361
		}
281 362
	}
282 363
	
364
	/**
365
	 * delete job - to be implemented
366
	 */
283 367
	public void deleteJob(HttpServletRequest request, HttpServletResponse response, 
284 368
			Hashtable<String, String[]> params, String username, String[] groups) throws MetacatSchedulerException {
285 369
		
286 370
	}
287 371
	
372
	/**
373
	 * get job information for a given workflow in xml format
374
	 * 
375
	 * @param request
376
	 *            the servlet request object
377
	 * @param response
378
	 *            the servlet response object
379
	 * @param params
380
	 *            the request parameters
381
	 * @param username
382
	 *            the user
383
	 * @param groups
384
	 *            the user's group
385
	 */
288 386
	public void getJobs(HttpServletRequest request, HttpServletResponse response, 
289 387
			Hashtable<String, String[]> params, String username, String[] groups) throws MetacatSchedulerException {
290 388
		
......
301 399
        }
302 400
		
303 401
		try {
402
			// get the job info in xml format
304 403
			String xmlResult = SchedulerService.getInstance().getJobsInfoXML(WORKFLOW_JOB_GROUP, "workflowid", workFlowId);
305 404
			logMetacat.debug("WorkflowScheduler.getJobs - xmlResult: " + xmlResult);
306 405
			

Also available in: Unified diff