}
logger.debug("executing command: " + command.toString());
if (!command.getCategory().equalsIgnoreCase(getCategory())) {
throw new CommandException("command category mismatch: found \"" + command.getCategory() + "\" required \"" + getCategory() + "\"");
}
if (command.isAsynchronous() && command.getCommandGroupId() != null && "COMPOUND".equals(command.getCommandGroupId())) {
List<String> jobIdList = null;
if (command.containsParameterKey("JOB_ID_LIST")) {
jobIdList = command.getParameterMultivalue("JOB_ID_LIST");
command.deleteParameter("JOB_ID_LIST");
} else {
jobIdList = getJobList(command).getJobIdList();
}
if (command.containsParameterKey("EXECUTION_MODE")) {
if (Command.EXECUTION_MODE_SERIAL.equals(command.getParameterAsString("EXECUTION_MODE"))) {
command.setExecutionMode(Command.ExecutionModeValues.SERIAL);
} else {
command.setExecutionMode(Command.ExecutionModeValues.PARALLEL);
}
}
if (command.containsParameterKey("PRIORITY_LEVEL")) {
command.setPriorityLevel(Integer.parseInt(command.getParameterAsString("PRIORITY_LEVEL")));
}
if (jobIdList != null) {
for (String jobId : jobIdList) {
command.addParameter("JOB_ID", jobId);
command.setCommandGroupId(jobId);
try {
getCommandManager().execute(command);
} catch (CommandManagerException e) {
logger.error(e.getMessage());
throw new CommandExecutorException(e.getMessage());
}
}
}
if (command.getExecutionCompletedTime() == null) {
command.setExecutionCompletedTime(Calendar.getInstance());
}
logger.debug("END execute");
return;
}
String userId = command.getUserId();
if (userId == null) {
throw new CommandException("userId not defined!");
}
boolean isAdmin = command.getParameterAsString("IS_ADMIN") != null && command.getParameterAsString("IS_ADMIN").equalsIgnoreCase("true");
int cmdType = getCommandType(command.getName());
if (JobCommandConstant.GET_SERVICE_INFO.equals(command.getName())) {
JobSubmissionManagerInfo jobSubmissionManagerInfo = JobSubmissionManager.getInstance().getJobSubmissionManagerInfo();
command.getResult().addParameter("ACCEPT_NEW_JOBS", "" + jobSubmissionManagerInfo.isAcceptNewJobs());
command.getResult().addParameter("SUBMISSION_THRESHOLD_MESSAGE", jobSubmissionManagerInfo.getShowMessage());
command.getResult().addParameter("SUBMISSION_ERROR_MESSAGE", jobSubmissionManagerInfo.getTestErrorMessage());
command.getResult().addParameter("SUBMISSION_EXECUTION_TIMESTAMP", jobSubmissionManagerInfo.getExecutionTimestamp());
String sensorHost = getParameterValueAsString(CREAM_JOB_SENSOR_HOST);
if (sensorHost != null && !"changeme".equals(sensorHost)) {
command.getResult().addParameter("CEMON_URL", "https://" + sensorHost + ":8443/ce-monitor/services/CEMonitor");
}
jobSubmissionManagerInfo = null; //gc
} else if (JobCommandConstant.SET_ACCEPT_NEW_JOBS.equals(command.getName())) {
String accept = command.getParameterAsString("ACCEPT_NEW_JOBS");
if (accept == null) {
throw new CommandException("ACCEPT_NEW_JOBS value not specified!");
}
if (!isAdmin) {
throw new CommandException("Operation reserved only to administrator!");
}
try {
jobSubmissionManager.enableAcceptNewJobs(Integer.parseInt(accept));
} catch (NumberFormatException nfe) {
throw new CommandException("ACCEPT_NEW_JOBS value not valid!");
}
} else if (JobCommandConstant.PROXY_RENEW.equals(command.getName())) {
logger.debug("Calling updateProxyToSandbox.");
String delegId = command.getParameterAsString("DELEGATION_PROXY_ID");
if (delegId == null) {
throw new CommandException("parameter \"DELEGATION_PROXY_ID\" not defined!");
}
String delegProxyInfo = command.getParameterAsString("DELEGATION_PROXY_INFO");
if (delegProxyInfo == null) {
throw new CommandException("parameter \"DELEGATION_PROXY_INFO\" not defined!");
}
Calendar now = Calendar.getInstance();
int[] statusType = new int[] {JobStatus.REGISTERED, JobStatus.HELD, JobStatus.IDLE, JobStatus.PENDING, JobStatus.REALLY_RUNNING, JobStatus.RUNNING};
JobCommand jobCmd = new JobCommand();
jobCmd.setCreationTime(command.getCreationTime());
//jobCmd.setDescription(command.getDescription());
jobCmd.setDescription(delegProxyInfo);
jobCmd.setStartSchedulingTime(command.getStartProcessingTime());
jobCmd.setStartProcessingTime(now);
jobCmd.setExecutionCompletedTime(now);
jobCmd.setType(cmdType);
jobCmd.setStatus(JobCommand.SUCCESSFULL);
jobCmd.setCommandExecutorName(getName());
jobCmd.setUserId(userId);
insertJobCommand(jobCmd, delegId, statusType);
} else if (JobCommandConstant.JOB_REGISTER.equals(command.getName())) {
logger.debug("Calling jobRegister.");
jobRegister(command);
} else if (JobCommandConstant.JOB_SET_LEASEID.equals(command.getName())) {
logger.debug("Calling jobSetLeaseId.");
jobSetLeaseId(command);
} else if (JobCommandConstant.DELETE_LEASE.equals(command.getName())) {
logger.debug("Calling deleteLease.");
deleteLease(command);
} else if (JobCommandConstant.SET_LEASE.equals(command.getName())) {
logger.debug("Calling setLease.");
setLease(command);
} else if (JobCommandConstant.GET_LEASE.equals(command.getName())) {
logger.debug("Calling getLease.");
getLease(command);
} else if (JobCommandConstant.JOB_INFO.equals(command.getName())) {
logger.debug("Calling jobInfo.");
getJobList(command);
} else if (JobCommandConstant.JOB_STATUS.equals(command.getName())) {
logger.debug("Calling jobStatus.");
JobEnumeration jobEnum = getJobList(command);
String user = null;
if (!isAdmin) {
user = userId;
}
try {
List<JobStatus> jobStatusList = jobDB.retrieveLastJobStatus(jobEnum.getJobIdList(), user);
command.getResult().addParameter("JOB_STATUS_LIST", jobStatusList);
if (command.containsParameterKey("JOB_ID") || command.containsParameterKey("JOB_ID_LIST")) {
if (jobStatusList.size() < jobEnum.getJobIdList().size()) {
List<String> jobIdList = new ArrayList<String>(jobStatusList.size());
for (JobStatus status : jobStatusList) {
jobIdList.add(status.getJobId());
}
jobEnum.getJobIdList().removeAll(jobIdList);
int found = 0;
List<JobIdFilterResult> jobIdFilterResultList = (List<JobIdFilterResult>) command.getResult().getParameter("JOBID_FILTER_RESULT_LIST");
for (JobIdFilterResult filterResult : jobIdFilterResultList) {
if (jobEnum.getJobIdList().contains(filterResult.getJobId())) {
filterResult.setErrorCode(JobIdFilterFailure.JOBID_ERRORCODE);
filterResult.setFailureReason(JobIdFilterFailure.failureReason[JobIdFilterFailure.JOBID_ERRORCODE]);
if (++found == jobEnum.getJobIdList().size()) {
break;
}
}
}
command.getResult().addParameter("JOB_ENUM", new JobEnumeration(jobIdList, jobDB));
}
}
} catch (DatabaseException e) {
logger.error(e.getMessage());
throw new CommandException("database error occurred");
}
} else if (JobCommandConstant.QUERY_EVENT.equals(command.getName())) {
logger.debug("Calling queryEvent.");
int maxEvents = 100;
try {
maxEvents = Integer.parseInt(command.getParameterAsString("MAX_QUERY_EVENT_RESULT_SIZE"));
} catch (Throwable t) {
logger.warn("queryEvent: wrong value for MAX_QUERY_EVENT_RESULT_SIZE");
}
int[] jobStatusTypeArray = null;
if (command.containsParameterKey("statusList")) {
List<String> statusList = command.getParameterMultivalue("statusList");
jobStatusTypeArray = new int[statusList.size()];
for (int i = 0; i < statusList.size(); i++) {
jobStatusTypeArray[i] = -1;
for (int x = 0; x < JobStatus.statusName.length; x++) {
if (statusList.get(i).equals(JobStatus.getNameByType(x))) {
jobStatusTypeArray[i] = x;
continue;
}
}
}
}
JobStatusEventManagerInterface jobStatusEventManager = (JobStatusEventManagerInterface) EventManagerFactory.getEventManager("JOB_STATUS_EVENT_MANAGER");
List<Event> eventList = null;
try {
eventList = jobStatusEventManager.getEvents(command.getParameterAsString("FROM_EVENT_ID"), command.getParameterAsString("TO_EVENT_ID"),
makeDate(command.getParameterAsString("FROM_DATE")), makeDate(command.getParameterAsString("TO_DATE")), jobStatusTypeArray,
maxEvents, userId);
} catch (EventManagerException e) {
throw new CommandException(e.getMessage());
}
command.getResult().addParameter("EVENT_LIST", eventList);
} else if (JobCommandConstant.SET_JOB_STATUS.equals(command.getName())) {
logger.debug("Calling setJobStatus.");
int statusType = Integer.valueOf(command.getParameterAsString("STATUS_TYPE"));
String jobId = command.getParameterAsString("JOB_ID");
String workerNode = command.getParameterAsString("WORKER_NODE");
String lrmsJobId = command.getParameterAsString("LRMS_JOB_ID");
String exitCode = command.getParameterAsString("EXIT_CODE");
String failureReason = command.getParameterAsString("FAILURE_REASON");
Calendar changeTime = null;
if (command.containsParameterKey("STATUS_CHANGE_TIME")) {
changeTime = new GregorianCalendar();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormat.setCalendar(changeTime);
try {
dateFormat.parse(command.getParameterAsString("STATUS_CHANGE_TIME"));
} catch (ParseException e) {
logger.error(e.getMessage());
}
}
JobStatus status = new JobStatus(statusType, jobId, changeTime);
status.setExitCode(exitCode);
status.setFailureReason(failureReason);
Job job = null;
try {
job = jobDB.retrieveJob(status.getJobId(), null);
} catch (Exception e) {
logger.warn("job " + status.getJobId() + " not found!");
return;
}
try {
if(doOnJobStatusChanged(status, job)) {
boolean updateJob = false;
if (lrmsJobId != null && (job.getLRMSJobId() == null || job.getLRMSJobId().equalsIgnoreCase("N/A"))) {
job.setLRMSJobId(lrmsJobId);
updateJob = true;
}
if (workerNode != null) {
boolean isReallyRunning = false;
if(job.getWorkerNode() != null && !job.getWorkerNode().equals("N/A") && status.getType() != JobStatus.REALLY_RUNNING) {
for(JobStatus oldStatus : job.getStatusHistory()) {
if(oldStatus.getType() == JobStatus.REALLY_RUNNING) {
isReallyRunning = true;
break;
}
}
}
if(!isReallyRunning) {
job.setWorkerNode(workerNode);
updateJob = true;
}
}
if (updateJob) {
try {
jobDB.update(job);
} catch (Throwable e) {
logger.error(e);
}
}
}
} catch (JobManagementException e) {
logger.error(e.getMessage());
}
} else if (JobCommandConstant.JOB_LIST.equals(command.getName())) {
logger.debug("Calling jobList.");
try {
String user = null;
if (!isAdmin) {
user = userId;
}
List<String> jobIdFound = jobDB.retrieveJobId(user);
JobEnumeration jobEnum = new JobEnumeration(jobIdFound, jobDB);
command.getResult().addParameter("JOB_ENUM", jobEnum);
} catch (DatabaseException e) {
logger.error(e.getMessage());
throw new CommandException("database error occurred");
}
} else {
JobEnumeration jobEnum = getJobList(command);
try {
List<Job> jobList = new ArrayList<Job>(0);
Calendar now = Calendar.getInstance();
while (jobEnum.hasMoreJobs()) {
Job job = jobEnum.nextJob();
JobCommand jobCmd = new JobCommand();
jobCmd.setJobId(job.getId());
jobCmd.setCreationTime(command.getCreationTime());
jobCmd.setDescription(command.getDescription());
jobCmd.setStartSchedulingTime(command.getStartProcessingTime());
jobCmd.setStartProcessingTime(now);
jobCmd.setType(cmdType);
jobCmd.setCommandExecutorName(getName());
if (!isAdmin || job.getUserId().equals(command.getUserId())) {
jobCmd.setUserId(command.getUserId());
}
if ((JobCommandConstant.JOB_CANCEL.equals(command.getName())) && (jobCmd.getDescription() == null)) {
if (!isAdmin || job.getUserId().equals(command.getUserId())) {
jobCmd.setDescription("Cancelled by user");
} else {
jobCmd.setDescription("Cancelled by CE admin");
}
}
logger.debug("Calling jobDB.insertJobCommand.");
try {
jobDB.insertJobCommand(jobCmd);
} catch (Throwable e) {
logger.error(e.getMessage());
continue;
}
logger.debug("jobDB.insertJobCommand has been executed.");
if (jobCmd.getStatus() != JobCommand.ERROR) {
job.addCommandHistory(jobCmd);
jobList.add(job);
}
}
for (Job j : jobList) {
JobCommand jobCmd = j.getLastCommand();
if (jobCmd == null) {
continue;
}
jobCmd.setStatus(JobCommand.PROCESSING);
if (LBLogger.isEnabled()) {
try {
LBLogger.getInstance().execute(j, command, LBLogger.START, null, null);
} catch (Throwable t) {
logger.warn("LBLogger.execute() failed: " + t.getMessage());
}
}
try {
if (JobCommandConstant.JOB_CANCEL.equals(command.getName())) {
if (j.getLastStatus() != null && j.getLastStatus().getType() == JobStatus.REGISTERED) {
JobStatus status = new JobStatus(JobStatus.CANCELLED, j.getId(), now);
status.setDescription(jobCmd.getDescription());
doOnJobStatusChanged(status, j);
} else {
cancel(j);
}
} else if (JobCommandConstant.JOB_PURGE.equals(command.getName())) {
purge(j);
} else if (JobCommandConstant.JOB_SUSPEND.equals(command.getName())) {
suspend(j);
} else if (JobCommandConstant.JOB_RESUME.equals(command.getName())) {
resume(j);
} else if (JobCommandConstant.JOB_START.equals(command.getName())) {
command.addParameter("JOB", j);
jobStart(command);
StringBuffer sb = new StringBuffer(command.toString());
if (j.getLRMSAbsLayerJobId() != null) {
sb.append(" lrmsAbsJobId=").append(j.getLRMSAbsLayerJobId()).append(";");
}
if (j.getLRMSJobId() != null) {
sb.append(" lrmsJobId=").append(j.getLRMSJobId()).append(";");
}
logger.info(sb.toString());
sb = null;
break;
// case JobCommandConstant.UPDATE_PROXY_TO_SANDBOX.equals(command.getName())) {
// logger.info(logInfo + jobCmd.toString() + " delegationId=" + j.getDelegationProxyId() + ";");
//
// String attr = j.getExtraAttribute("PROXY_RENEWAL");
// if (attr.equals("ENABLED")) {
// String delegationProxyInfo = command.getParameterAsString("DELEGATION_PROXY_INFO");
// if (delegationProxyInfo != null) {
// j.setDelegationProxyInfo(delegationProxyInfo);
// jobDB.update(j);
// }
// }
}
if (!JobCommandConstant.JOB_PURGE.equals(command.getName())) {
jobCmd.setStatus(JobCommand.SUCCESSFULL);
jobDB.updateJobCommand(jobCmd);
}
if (LBLogger.isEnabled()) {
try {
LBLogger.getInstance().execute(j, command, LBLogger.OK, j.getLRMSAbsLayerJobId(), null);
} catch (Throwable t) {
logger.warn("LBLogger.execute() failed: " + t.getMessage());
}
}
} catch (CommandException ce) {
jobCmd.setStatus(JobCommand.ERROR);
jobCmd.setFailureReason(ce.getMessage());
jobDB.updateJobCommand(jobCmd);
if (LBLogger.isEnabled()) {
try {
LBLogger.getInstance().execute(j, command, LBLogger.FAILED, null, ce);
} catch (Throwable t) {
logger.warn("LBLogger.execute() failed: " + t.getMessage());
}
}
}
}
List<String> invalidJobIdlist = (List<String>) command.getResult().getParameter("JOB_ID_LIST_STATUS_NOT_COMPATIBLE_FOUND");
if (invalidJobIdlist != null) {
for (String jobId : invalidJobIdlist) {
try {
JobCommand jobCmd = new JobCommand(cmdType, jobId);
jobCmd.setCreationTime(command.getCreationTime());
jobCmd.setDescription(command.getDescription());
jobCmd.setStartSchedulingTime(command.getStartProcessingTime());
jobCmd.setStatus(JobCommand.ERROR);
jobCmd.setFailureReason("status not compatible with the specified command!");
if (!isAdmin) {
jobCmd.setUserId(command.getUserId());
}
jobDB.insertJobCommand(jobCmd);
} catch (DatabaseException e) {
logger.error(e.getMessage());
}
}
}
} catch (DatabaseException e) {
logger.error(e.getMessage());
throw new CommandException("database error occurred");
} catch (JobManagementException e) {
throw new CommandException(e.getMessage());
}
}
if (command.getExecutionCompletedTime() == null) {
command.setExecutionCompletedTime(Calendar.getInstance());