Package org.glite.ce.creamapi.jobmanagement

Examples of org.glite.ce.creamapi.jobmanagement.JobStatus


            if (job == null) {
                return;
            }

            JobStatus lastStatus = job.getLastStatus();
            if (lastStatus == null) {
                return;
            }

            boolean update = false;

            if ("W".equalsIgnoreCase(lastStatus.getExitCode())) {
                try {
                    String exitCode = getExitCode(job.getWorkingDirectory() + "/StandardOutput", job.getLocalUser());
                    lastStatus.setExitCode(exitCode);
                } catch (Exception e) {
                    lastStatus.setExitCode(Job.NOT_AVAILABLE_VALUE);
                } finally {
                    update = true;
                }
            }

            if (lastStatus.getType() == JobStatus.DONE_FAILED || lastStatus.getType() == JobStatus.CANCELLED) {
                try {
                    String stdErrorMessage = readFile(job.getWorkingDirectory() + "/StandardError", job.getLocalUser());
                    String errorMessage = null;
                    if (stdErrorMessage != null && !stdErrorMessage.equals("")) {
                        errorMessage = lastStatus.getFailureReason();
                       
                        if (errorMessage != null && !stdErrorMessage.equals(stdErrorMessage)) {
                            errorMessage += "; " + stdErrorMessage;
                        } else {
                            errorMessage = stdErrorMessage;
                        }   
                    } else {
                        errorMessage = Job.NOT_AVAILABLE_VALUE;
                    }

                    lastStatus.setFailureReason(errorMessage);
                } catch (Exception e) {
                    if (lastStatus.getFailureReason() == null || lastStatus.getFailureReason().length() == 0) {
                        lastStatus.setFailureReason(Job.NOT_AVAILABLE_VALUE);
                    }
                } finally {
                    update = true;
                }
            }

            if(update) {
                Command statusCmd = new Command(JobCommandConstant.SET_JOB_STATUS, getCategory());
                //statusCmd.setCommandExecutorName(blahExec.getName());
                statusCmd.setAsynchronous(true);
                statusCmd.setUserId("admin");
                statusCmd.addParameter("JOB_ID", lastStatus.getJobId());
                statusCmd.addParameter("STATUS_TYPE", ""+lastStatus.getType());
               // statusCmd.addParameter("STATUS_CHANGE_TIME", lastStatus.getTimestamp());
               // statusCmd.addParameter("WORKER_NODE", workerNode);
               // statusCmd.addParameter("LRMS_JOB_ID", batchJobId);
                statusCmd.addParameter("IS_ADMIN", "true");
                statusCmd.addParameter("EXIT_CODE", lastStatus.getExitCode());
                statusCmd.addParameter("FAILURE_REASON", lastStatus.getFailureReason());
                statusCmd.setPriorityLevel(Command.MEDIUM_PRIORITY);
                statusCmd.setExecutionMode(Command.ExecutionModeValues.SERIAL);
                statusCmd.setCommandGroupId(lastStatus.getJobId());

                if (lastStatus.getTimestamp() != null) {
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                    statusCmd.addParameter("STATUS_CHANGE_TIME", dateFormat.format(lastStatus.getTimestamp().getTime()));
                }

                try {
                    getCommandManager().execute(statusCmd);
                } catch (Throwable e) {
View Full Code Here


                } 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);
                            }
View Full Code Here

                      
        Job job = (Job)command.getParameter("JOB");
       
        logger.debug("Begin jobStart for job " + job.getId());

        JobStatus status = new JobStatus(JobStatus.PENDING, job.getId());
        try {
            doOnJobStatusChanged(status, job);
        } catch (Throwable t) {
          throw new CommandException(t.getMessage());
        }

        Calendar now = Calendar.getInstance();
        CommandResult cr = null;
        String failureReason = null;
       
        for (int i=1; i<4 && cr == null; i++) {
            failureReason = null;

            try {
                cr = submit(job);
            } catch (CommandException ce) {
                failureReason = ce.getMessage();
                logger.warn("submission to BLAH failed [jobId=" + job.getId() + "; reason=" + failureReason + "; retry count=" + i + "/3]");

                synchronized(now) {
                    try {
                        logger.debug("sleeping 10 sec...");
                        now.wait(10000);
                        logger.debug("sleeping 10 sec... done");
                    } catch (InterruptedException e) {
                        logger.warn(e.getMessage());
                    }
                }
            }
        }
       
        if (cr == null) {
            status = new JobStatus(JobStatus.ABORTED, job.getId());
            status.setDescription("submission to BLAH failed [retry count=3]");
            status.setFailureReason(failureReason);

            try {
                doOnJobStatusChanged(status, job);
            } catch (Throwable te) {
                throw new CommandException(te.getMessage());
            }

            setLeaseExpired(job);

            throw new CommandException("submission to BLAH failed [retry count=3]" + (failureReason != null ? ": " + failureReason : ""));
        }

        job.setLRMSJobId(cr.getParameterAsString("LRMS_JOB_ID"));
        job.setLRMSAbsLayerJobId(cr.getParameterAsString("LRMS_ABS_JOB_ID"));

        try {
            if (isEmptyField(job.getLRMSAbsLayerJobId())) {
                status = new JobStatus(JobStatus.ABORTED, job.getId());
                status.setFailureReason("LRMSAbsLayerJobId not found!");

                doOnJobStatusChanged(status, job);

                setLeaseExpired(job);
            } else {
                jobDB.update(job);

                JobStatus lastStatus = jobDB.retrieveLastJobStatus(job.getId(), command.getUserId());
                if (lastStatus.getType() == JobStatus.PENDING) {
                    status = new JobStatus(JobStatus.IDLE, job.getId(), now);

                    doOnJobStatusChanged(status, job);
                }
            }
        } catch (Throwable te) {
View Full Code Here

                throw new CommandException("userId not defined!");
            }

            Job job = makeJobFromCmd(cmd);
           
            JobStatus status = new JobStatus(JobStatus.REGISTERED, job.getId());

            job.setUserId(userId);
            job.setLocalUser(cmd.getParameterAsString("LOCAL_USER"));
            job.setJDL(cmd.getParameterAsString("JDL"));
            job.setICEId(cmd.getParameterAsString("ICE_ID"));
            job.addCommandHistory(jobCmd);
           
            if (cmd.containsParameterKey("USER_VO")) {
                job.setVirtualOrganization(cmd.getParameterAsString("USER_VO"));
            }

            if (isEmptyField(job.getBatchSystem())) {
                throw new CommandException("\"BatchSystem\" attribute not defined into the JDL");
            }

            if (isEmptyField(job.getQueue())) {
                throw new CommandException("\"QueueName\" attribute not defined into the JDL");
            }

            if (!isBatchSystemSupported(job.getBatchSystem())) {
                throw new CommandException("Batch System " + job.getBatchSystem() + " not supported!");
            }

            String cream_sandbox_dir = getParameterValueAsString("CREAM_SANDBOX_DIR");
            if (cream_sandbox_dir == null) {
                throw new CommandException("parameter \"CREAM_SANDBOX_DIR\" not defined!");
            }

            job.setCreamURL(cmd.getParameterAsString("CREAM_URL"));
            job.setDelegationProxyId(cmd.getParameterAsString("DELEGATION_PROXY_ID"));
            job.setDelegationProxyInfo(cmd.getParameterAsString("DELEGATION_PROXY_INFO"));
            job.setDelegationProxyCertPath(cmd.getParameterAsString("DELEGATION_PROXY_PATH"));
            job.setLRMSAbsLayerJobId(Job.NOT_AVAILABLE_VALUE);
            job.setLRMSJobId(Job.NOT_AVAILABLE_VALUE);
            job.setWorkerNode(Job.NOT_AVAILABLE_VALUE);
            job.setWorkingDirectory(Job.NOT_AVAILABLE_VALUE);

            if (cmd.containsParameterKey("USER_DN")) {
                job.addExtraAttribute("USER_DN", cmd.getParameterAsString("USER_DN").replaceAll("\\s+", "\\\\ "));
            }

            if (cmd.containsParameterKey("USER_DN_X500")) {
                job.addExtraAttribute("USER_DN_X500", cmd.getParameterAsString("USER_DN_X500").replaceAll("\\s+", "\\\\ "));
            }

            if (cmd.containsParameterKey("LOCAL_USER_GROUP")) {
                job.addExtraAttribute("LOCAL_USER_GROUP", cmd.getParameterAsString("LOCAL_USER_GROUP"));
            }

            if (cmd.containsParameterKey("USER_FQAN")) {
                List<String> fqanList = cmd.getParameterMultivalue("USER_FQAN");
               
                if (fqanList != null && fqanList.size() > 0) {                   
                    StringBuffer fqanBuffer = new StringBuffer();
                   
                    for (String fqan : fqanList) {
                        fqanBuffer.append("\\\"userFQAN=").append(fqan.replaceAll("\\s+", "\\\\ ")).append("\\\"\\ ");
                    }

                    fqanBuffer.deleteCharAt(fqanBuffer.length() - 1);
                    fqanBuffer.deleteCharAt(fqanBuffer.length() - 1);
                   
                    job.addExtraAttribute("USER_FQAN", fqanBuffer.toString());
                }
            }
            
            if (this.containsParameterKey("LRMS_EVENT_LISTENER_PORT")) {
                job.setLoggerDestURI(InetAddress.getLocalHost().getHostAddress() + ":" + getParameterValueAsString("LRMS_EVENT_LISTENER_PORT"));
            }

            if (job.getCreamURL() != null) {
                try {
                    URL url = new URL(job.getCreamURL());
                    job.setCeId(url.getHost() + ":" + url.getPort() + "/cream-" + job.getBatchSystem() + "-" + job.getQueue());
                } catch (MalformedURLException e) {
                }
            }

            if (cmd.containsParameterKey("LEASE_ID")) {
                String leaseId = cmd.getParameterAsString("LEASE_ID");

                if (leaseId != null && leaseId.length() > 0) {
                    Lease lease = jobDB.retrieveJobLease(leaseId, userId);
                    if (lease != null) {
                        logger.debug("found lease \"" + leaseId + "\" = " + lease.getLeaseTime().getTime());
                        job.setLease(lease);
                    } else {
                        throw new CommandException("lease id \"" + leaseId + "\" not found!");
                    }
                }
            }

            boolean jobInserted = false;
            int count = 0;

            while (!jobInserted && count < 5) {
                try {
                    jobDB.insert(job);
                    jobInserted = true;
                } catch (DatabaseException de) {
                    if (de.getMessage().indexOf("Duplicate entry") > -1) {
                        job.setId(job.generateJobId());
                        count++;
                    } else {
                        logger.error(de.getMessage());
                        throw new CommandException("database error occurred");
                    }
                } catch (IllegalArgumentException ie) {
                    throw new CommandException(ie.getMessage());
                }
            }

            if (!jobInserted) {
                throw new CommandException("Duplicate jobId error: cannot insert the new job (" + job.getId() + ") into the database");
            }

            jobCmd.setJobId(job.getId());
            jobCmd.setStatus(JobCommand.SUCCESSFULL);
          
            if (LBLogger.isEnabled()) {
                try {
                    LBLogger.getInstance().register(job);
                } catch (Throwable e) {
                    logger.warn("LBLogger.register() failed: " + e.getMessage());
                }

                try {
                    LBLogger.getInstance().accept(job);
                } catch (Throwable e) {
                    logger.warn("LBLogger.accept() failed: " + e.getMessage());
                }
            }

            try {
                createJobSandboxDir(job, cmd.getParameterAsString("GSI_FTP_CREAM_URL"));
            } catch (Throwable e) {
                jobCmd.setStatus(JobCommand.ERROR);
                jobCmd.setFailureReason(e.getMessage());

                status.setType(JobStatus.ABORTED);
                status.setFailureReason(e.getMessage());

                doOnJobStatusChanged(status, job);
               
                throw new CommandException(e.getMessage());
            } finally {
View Full Code Here

        if (job == null) {
            logger.warn("job " + status.getJobId() + " not found!");
            return false;
        }
     
        JobStatus lastStatus = job.getLastStatus();
/*
        if (lastStatus == null) {
            throw new JobManagementException("job status " + status.getJobId() + " not found!");
        }
*/
        if (lastStatus != null && status.getType() == lastStatus.getType()) {
            if (lastStatus.getName().startsWith("DONE")) {
                try {
                    if (!"W".equalsIgnoreCase(lastStatus.getExitCode())) {
                        status.setExitCode(lastStatus.getExitCode());
                    }

                    if (status.getFailureReason() != null) {
                        if (lastStatus.getFailureReason() != null &&
                                !lastStatus.getFailureReason().equals(job.NOT_AVAILABLE_VALUE) &&
                                !status.getFailureReason().equals(lastStatus.getFailureReason())) {
                            status.setFailureReason(lastStatus.getFailureReason() + "; " + status.getFailureReason());
                        } else {
                            status.setFailureReason(status.getFailureReason());
                        }
                    }

                    status.setId(lastStatus.getId());

                    jobDB.updateStatus(status, null);

                    statusUpdated = true;

                    if (LBLogger.isEnabled()) {
                        try {
                            LBLogger.getInstance().statusChanged(job, status, null, LBLogger.START);
                        } catch (Exception e) {
                            logger.warn("LBLogger.statusChanged failed: " + e.getMessage());
                        }
                    }

                    logger.info("JOB " + status.getJobId() + " STATUS UPDATED: " + status.getName());

                    try {
                        sendNotification(job);
                    } catch (Throwable e) {
                        logger.error(e.getMessage());
                    }
                } catch (IllegalArgumentException e) {
                    logger.error(e);
                    throw new JobManagementException(e);
                } catch (DatabaseException e) {
                    logger.error(e.getMessage());
                    throw new JobManagementException("database error occurred");
                }
            }
        } else {
            if (lastStatus != null && (lastStatus.getType() == JobStatus.ABORTED || lastStatus.getType() == JobStatus.CANCELLED ||
                    lastStatus.getType() == JobStatus.DONE_OK || lastStatus.getType() == JobStatus.DONE_FAILED)) {
                return statusUpdated;
            }

            switch (status.getType()) {
            case JobStatus.ABORTED:
                setLeaseExpired(job);
                break;

            case JobStatus.CANCELLED:
                status.setDescription("Cancelled by CE admin");
                int cancelType = getCommandType(JobCommandConstant.JOB_CANCEL);

                for (int i = job.getCommandHistoryCount() - 1; i >= 0; i--) {
                    if (job.getCommandHistoryAt(i).getType() == cancelType) {
                        status.setDescription(job.getCommandHistoryAt(i).getDescription());
                        break;
                    }
                }
                setLeaseExpired(job);
                break;

            case JobStatus.DONE_OK:
            case JobStatus.DONE_FAILED:
                if (status.getType() == JobStatus.DONE_FAILED) {
                    int cancelledType = getCommandType(JobCommandConstant.JOB_CANCEL);

                    for (int i = job.getCommandHistoryCount() - 1; i >= 0; i--) {
                        if (job.getCommandHistoryAt(i).getType() == cancelledType) {                        
                            status.setType(JobStatus.CANCELLED);
                            status.setExitCode(null);
                            status.setDescription(job.getCommandHistoryAt(i).getDescription());
                            break;
                        }
                    }                 
                }
               
                if ("W".equalsIgnoreCase(status.getExitCode())) {
                    Calendar time = Calendar.getInstance();
                    time.add(Calendar.MINUTE, 1);
                    timer.schedule(new GetSTDTask(status.getJobId()), time.getTime());
                    timer.purge();
                }              

                setLeaseExpired(job);
                break;

            case JobStatus.REALLY_RUNNING:
                if (lastStatus != null && (lastStatus.getType() == JobStatus.ABORTED || lastStatus.getType() == JobStatus.CANCELLED ||
                        lastStatus.getType() == JobStatus.DONE_OK || lastStatus.getType() == JobStatus.DONE_FAILED)) {
                    return statusUpdated;
                }
                break;

            case JobStatus.RUNNING:
                if (status.getTimestamp().compareTo(lastStatus.getTimestamp()) <= 0) {
                    return statusUpdated;
                }

                if (lastStatus != null && (lastStatus.getType() == JobStatus.REALLY_RUNNING || lastStatus.getType() == JobStatus.ABORTED ||
                        lastStatus.getType() == JobStatus.CANCELLED || lastStatus.getType() == JobStatus.DONE_OK || lastStatus.getType() == JobStatus.DONE_FAILED)) {
                    return statusUpdated;
                }
                try {
                    List<JobStatus> statusList = jobDB.retrieveJobStatusHistory(status.getJobId(), null);

                    if (statusList != null && statusList.size() > 2) {
                        JobStatus oldStatus = statusList.get(statusList.size() - 2);
                        status.setType(oldStatus.getType() == JobStatus.REALLY_RUNNING ? JobStatus.REALLY_RUNNING : JobStatus.RUNNING);
                    }
                } catch (DatabaseException e) {
                    logger.error(e.getMessage());
                    throw new JobManagementException("database error occurred");
                }
View Full Code Here

                            jobDB.updateJobCommand(cmd);
                            logger.info(cmd.toString());
                        }

                        JobStatus status = job.getLastStatus();
                        status.setType(JobStatus.ABORTED);
                        status.setFailureReason("job aborted because the execution of the JOB_START command has been interrupted by the CREAM shutdown");
                        status.setTimestamp(job.getLastCommand().getExecutionCompletedTime());

                        jobDB.updateStatus(status, null);
                        logger.info("job " + job.getId() + " aborted because the execution of the JOB_START command has been interrupted by the CREAM shutdown");

                        try {
View Full Code Here

TOP

Related Classes of org.glite.ce.creamapi.jobmanagement.JobStatus

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.