Search in sources :

Example 1 with JobInfo

use of org.glassfish.api.admin.progress.JobInfo in project Payara by payara.

the class ListJobsCommand method display.

public void display(Collection<JobInfo> jobInfoList, AdminCommandContext context) {
    report = context.getActionReport();
    int longestName = TITLE_NAME.length();
    int longestJobId = TITLE_JOBID.length();
    int longestTime = TITLE_TIME.length();
    int longestState = TITLE_STATE.length();
    int longestUser = TITLE_USER.length();
    int longestExitCode = TITLE_EXITCODE.length();
    for (JobInfo job : jobInfoList) {
        int jobId = job.jobId.length();
        int time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(job.commandExecutionDate).length();
        int name = job.jobName.length();
        int state = job.state.length();
        int user;
        if (job.user != null) {
            user = job.user.length();
        } else {
            user = DEFAULT_USER_STRING.length();
        }
        int exitCode = job.exitCode.length();
        if (name > longestName)
            longestName = name;
        if (time > longestTime)
            longestTime = time;
        if (jobId > longestJobId)
            longestJobId = jobId;
        if (state > longestState)
            longestState = state;
        if (user > longestUser)
            longestUser = user;
        if (exitCode > longestExitCode)
            longestExitCode = exitCode;
    }
    if (jobInfoList.size() < 1) {
        report.setMessage(TITLE_NONE);
    }
    longestName += 2;
    longestJobId += 2;
    longestState += 2;
    longestTime += 2;
    longestUser += 2;
    longestExitCode += 2;
    String formattedLine = "%-" + longestName + "s %-" + longestJobId + "s %-" + longestTime + "s %-" + longestState + "s %-" + longestExitCode + "s %-" + longestUser + "s";
    // no linefeed at the end!!!
    boolean first = true;
    MessagePart topMsg = report.getTopMessagePart();
    Properties properties = report.getExtraProperties();
    if (properties == null) {
        properties = new Properties();
        report.setExtraProperties(properties);
    }
    Collection<Map<String, Object>> details = new ArrayList<Map<String, Object>>();
    properties.put("jobs", details);
    for (JobInfo info : jobInfoList) {
        if (first) {
            topMsg.setMessage(String.format(formattedLine, TITLE_NAME, TITLE_JOBID, TITLE_TIME, TITLE_STATE, TITLE_EXITCODE, TITLE_USER));
            first = false;
        }
        MessagePart msg = topMsg.addChild();
        msg.setMessage(String.format(formattedLine, info.jobName, info.jobId, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(info.commandExecutionDate), info.state, info.exitCode, info.user));
        Map<String, Object> detail = new HashMap<String, Object>();
        details.add(detail);
        detail.put(NAME, info.jobName);
        detail.put(ID, info.jobId);
        detail.put(DATE, new Date(info.commandExecutionDate));
        if (info.commandCompletionDate == 0)
            // for a running job
            detail.put(COMPLETION_DATE, " ");
        else
            // for a completed job
            detail.put(COMPLETION_DATE, new Date(info.commandCompletionDate));
        detail.put(STATE, info.state);
        detail.put(CODE, info.exitCode);
        detail.put(MESSAGE, info.message);
        detail.put(USER, info.user);
    }
    report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
}
Also used : MessagePart(org.glassfish.api.ActionReport.MessagePart) Date(java.util.Date) JobInfo(org.glassfish.api.admin.progress.JobInfo) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with JobInfo

use of org.glassfish.api.admin.progress.JobInfo in project Payara by payara.

the class ListJobsCommand method chooseJobs.

private List<JobInfo> chooseJobs() {
    List<JobInfo> jobsToReport = new ArrayList<JobInfo>();
    if (jobID != null) {
        Job oneJob = jobManagerService.get(jobID);
        JobInfo info = null;
        if (isSingleJobOK(oneJob)) {
            List<String> userList = oneJob.getSubjectUsernames();
            ActionReport actionReport = oneJob.getActionReport();
            String message = actionReport == null ? "" : actionReport.getMessage();
            if (!StringUtils.ok(message)) {
                message = ProgressStatusClient.composeMessageForPrint(oneJob.getCommandProgress());
            }
            String exitCode = actionReport == null ? "" : actionReport.getActionExitCode().name();
            info = new JobInfo(oneJob.getId(), oneJob.getName(), oneJob.getCommandExecutionDate(), exitCode, userList.get(0), message, oneJob.getJobsFile(), oneJob.getState().name(), 0);
        } else {
            if (getCompletedJobs() != null) {
                info = getCompletedJobForId(jobID);
            }
        }
        if (info != null && !skipJob(info.jobName)) {
            jobsToReport.add(info);
        }
    } else {
        for (Iterator<Job> iterator = jobManagerService.getJobs(); iterator.hasNext(); ) {
            Job job = iterator.next();
            if (isJobEligible(job)) {
                List<String> userList = job.getSubjectUsernames();
                ActionReport actionReport = job.getActionReport();
                String message = actionReport == null ? "" : actionReport.getMessage();
                if (!StringUtils.ok(message)) {
                    message = ProgressStatusClient.composeMessageForPrint(job.getCommandProgress());
                }
                String exitCode = actionReport == null ? "" : actionReport.getActionExitCode().name();
                String user = DEFAULT_USER_STRING;
                if (userList.size() > 0) {
                    user = userList.get(0);
                }
                jobsToReport.add(new JobInfo(job.getId(), job.getName(), job.getCommandExecutionDate(), exitCode, user, message, job.getJobsFile(), job.getState().name(), 0));
            }
        }
        JobInfos completedJobs = getCompletedJobs();
        if (completedJobs != null) {
            for (JobInfo info : completedJobs.getJobInfoList()) {
                if (!skipJob(info.jobName)) {
                    jobsToReport.add(info);
                }
            }
        }
    }
    return jobsToReport;
}
Also used : JobInfo(org.glassfish.api.admin.progress.JobInfo) JobInfos(org.glassfish.api.admin.progress.JobInfos) ActionReport(org.glassfish.api.ActionReport)

Example 3 with JobInfo

use of org.glassfish.api.admin.progress.JobInfo in project Payara by payara.

the class AdminCommandInstanceImpl method complete.

@Override
public void complete(ActionReport report, Payload.Outbound outbound) {
    if (commandProgress != null && report != null && report.getActionExitCode() == ExitCode.SUCCESS) {
        commandProgress.complete();
    }
    super.actionReport = report;
    this.payload = outbound;
    this.completionDate = System.currentTimeMillis();
    if (isManagedJob) {
        if (getState().equals(State.RUNNING_RETRYABLE) && failToRetryable) {
            JobManagerService jobManager = Globals.getDefaultHabitat().getService(JobManagerService.class);
            jobManager.getRetryableJobsInfo().put(id, CheckpointHelper.CheckpointFilename.createBasic(this));
            jobManager.purgeJob(id);
            setState(State.FAILED_RETRYABLE);
        } else {
            JobPersistence jobPersistenceService;
            if (scope != null) {
                jobPersistenceService = Globals.getDefaultHabitat().getService(JobPersistence.class, scope + "job-persistence");
            } else {
                jobPersistenceService = Globals.getDefaultHabitat().getService(JobPersistenceService.class);
            }
            State finalState = State.COMPLETED;
            if (getState().equals(State.REVERTING)) {
                finalState = State.REVERTED;
            }
            String user = null;
            if (subjectUsernames.size() > 0) {
                user = subjectUsernames.get(0);
            }
            jobPersistenceService.persist(new JobInfo(id, commandName, executionDate, report.getActionExitCode().name(), user, report.getMessage(), getJobsFile(), finalState.name(), completionDate));
            if (getState().equals(State.RUNNING_RETRYABLE) || getState().equals(State.REVERTING)) {
                JobManagerService jobManager = Globals.getDefaultHabitat().getService(JobManagerService.class);
                File jobFile = getJobsFile();
                if (jobFile == null) {
                    jobFile = jobManager.getJobsFile();
                }
                jobManager.deleteCheckpoint(jobFile.getParentFile(), getId());
            }
            setState(finalState);
        }
    } else {
        setState(State.COMPLETED);
    }
}
Also used : JobInfo(org.glassfish.api.admin.progress.JobInfo) File(java.io.File) JobPersistence(org.glassfish.api.admin.progress.JobPersistence)

Example 4 with JobInfo

use of org.glassfish.api.admin.progress.JobInfo in project Payara by payara.

the class JobManagerService method purgeCompletedJobForId.

/**
 * This method looks for the completed jobs
 * and purges a job which is marked with the jobId
 * @param jobId the job to purge
 * @return  the new list of completed jobs
 */
public JobInfos purgeCompletedJobForId(String jobId, File file) {
    JobInfos completedJobInfos = getCompletedJobs(file);
    synchronized (file) {
        CopyOnWriteArrayList<JobInfo> jobList = new CopyOnWriteArrayList<JobInfo>();
        if (completedJobInfos != null) {
            jobList.addAll(completedJobInfos.getJobInfoList());
            for (JobInfo jobInfo : jobList) {
                if (jobInfo.jobId.equals(jobId)) {
                    jobList.remove(jobInfo);
                }
            }
        }
        JobInfos jobInfos = new JobInfos();
        // if (jobList.size() > 0)    {
        try {
            if (jaxbContext == null)
                jaxbContext = JAXBContext.newInstance(JobInfos.class);
            jobInfos.setJobInfoList(jobList);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
            jaxbMarshaller.marshal(jobInfos, file);
        } catch (JAXBException e) {
            throw new RuntimeException(adminStrings.getLocalString("error.purging.completed.job", "Error purging completed job ", jobId, e.getLocalizedMessage()), e);
        }
        // }
        return jobInfos;
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) JobInfos(org.glassfish.api.admin.progress.JobInfos) JobInfo(org.glassfish.api.admin.progress.JobInfo) JAXBException(javax.xml.bind.JAXBException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 5 with JobInfo

use of org.glassfish.api.admin.progress.JobInfo in project Payara by payara.

the class JobManagerService method getExpiredJobs.

/**
 * This will return a list of jobs which have crossed the JOBS_RETENTION_PERIOD
 * and need to be purged
 * @return  list of jobs to be purged
 */
public ArrayList<JobInfo> getExpiredJobs(File file) {
    ArrayList<JobInfo> expiredJobs = new ArrayList<JobInfo>();
    synchronized (file) {
        JobInfos jobInfos = getCompletedJobs(file);
        for (JobInfo job : jobInfos.getJobInfoList()) {
            long executedTime = job.commandExecutionDate;
            long currentTime = System.currentTimeMillis();
            long jobsRetentionPeriod = 86400000;
            managedJobConfig = domain.getExtensionByType(ManagedJobConfig.class);
            jobsRetentionPeriod = convert(managedJobConfig.getJobRetentionPeriod());
            if (currentTime - executedTime > jobsRetentionPeriod && (job.state.equals(AdminCommandState.State.COMPLETED.name()) || job.state.equals(AdminCommandState.State.REVERTED.name()))) {
                expiredJobs.add(job);
            }
        }
    }
    return expiredJobs;
}
Also used : JobInfo(org.glassfish.api.admin.progress.JobInfo) JobInfos(org.glassfish.api.admin.progress.JobInfos) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ManagedJobConfig(com.sun.enterprise.config.serverbeans.ManagedJobConfig)

Aggregations

JobInfo (org.glassfish.api.admin.progress.JobInfo)10 JobInfos (org.glassfish.api.admin.progress.JobInfos)4 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)3 File (java.io.File)2 JAXBException (javax.xml.bind.JAXBException)2 ManagedJobConfig (com.sun.enterprise.config.serverbeans.ManagedJobConfig)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 Matcher (java.util.regex.Matcher)1 JAXBContext (javax.xml.bind.JAXBContext)1 Marshaller (javax.xml.bind.Marshaller)1 ActionReport (org.glassfish.api.ActionReport)1 MessagePart (org.glassfish.api.ActionReport.MessagePart)1 Job (org.glassfish.api.admin.Job)1 JobPersistence (org.glassfish.api.admin.progress.JobPersistence)1