Search in sources :

Example 21 with JobStatus

use of org.candlepin.pinsetter.core.model.JobStatus in project candlepin by candlepin.

the class PinsetterKernel method cancelJobs.

/**
 * Cancels the specified jobs by deleting the jobs and all triggers from the scheduler.
 * Assumes that the jobs are already marked as CANCELED in the JobStatus table.
 *
 * @param toCancel the JobStatus records of the jobs to cancel.
 * @throws PinsetterException if there is an error deleting the jobs from the schedule.
 *
 * @return
 *  The number of jobs cancelled as a result of a call to this method
 */
public int cancelJobs(Collection<JobStatus> toCancel) throws PinsetterException {
    List<JobKey> jobsToDelete = new LinkedList<>();
    for (JobStatus status : toCancel) {
        JobKey key = jobKey(status.getId(), status.getGroup());
        log.debug("Job {} from group {} will be deleted from the scheduler.", key.getName(), key.getGroup());
        jobsToDelete.add(key);
    }
    if (jobsToDelete.size() > 0) {
        log.info("Deleting {} cancelled jobs from scheduler", jobsToDelete.size());
        try {
            scheduler.deleteJobs(jobsToDelete);
        } catch (SchedulerException se) {
            throw new PinsetterException("Problem canceling jobs.", se);
        }
        log.info("Finished deleting jobs from scheduler");
    }
    return jobsToDelete.size();
}
Also used : JobStatus(org.candlepin.pinsetter.core.model.JobStatus) JobKey(org.quartz.JobKey) SchedulerException(org.quartz.SchedulerException) LinkedList(java.util.LinkedList)

Example 22 with JobStatus

use of org.candlepin.pinsetter.core.model.JobStatus in project candlepin by candlepin.

the class UnpauseJobTest method ensureJobCancelledWhenJobClassNoLongerExists.

@Test
public void ensureJobCancelledWhenJobClassNoLongerExists() throws Exception {
    JobDetail detail = Mockito.mock(JobDetail.class);
    JobKey key = new JobKey("fake-job");
    when(detail.getKey()).thenReturn(key);
    // Allowing setting the value of JobStatus.jobClass since it is private.
    // Do not want to expose the setter.
    Class<? extends JobStatus> statusClass = JobStatus.class;
    Field jobClassField = statusClass.getDeclaredField("jobClass");
    jobClassField.setAccessible(true);
    JobStatus status = new JobStatus();
    jobClassField.set(status, "unknown.class");
    status.setState(JobStatus.JobState.CREATED);
    assertEquals("unknown.class", status.getJobClass());
    List<JobStatus> jl = new ArrayList<>();
    jl.add(status);
    CandlepinQuery query = mock(CandlepinQuery.class);
    when(query.list()).thenReturn(jl);
    when(jobCurator.findWaitingJobs()).thenReturn(query);
    unpauseJob.execute(ctx);
    assertEquals(JobStatus.JobState.CANCELED, status.getState());
    assertEquals("Job canceled because job class no longer exists.", status.getResult());
    verify(jobCurator).merge(eq(status));
}
Also used : JobStatus(org.candlepin.pinsetter.core.model.JobStatus) Field(java.lang.reflect.Field) JobDetail(org.quartz.JobDetail) JobKey(org.quartz.JobKey) ArrayList(java.util.ArrayList) CandlepinQuery(org.candlepin.model.CandlepinQuery) EmptyCandlepinQuery(org.candlepin.model.EmptyCandlepinQuery) Test(org.junit.Test)

Example 23 with JobStatus

use of org.candlepin.pinsetter.core.model.JobStatus in project candlepin by candlepin.

the class UnpauseJobTest method unPauseTest.

@Test
public void unPauseTest() throws JobExecutionException, PinsetterException {
    JobDetail jd = newJob(KingpinJob.class).withIdentity("Kayfabe", "Deluxe").build();
    JobStatus js = new JobStatus(jd, true);
    List<JobStatus> jl = new ArrayList<>();
    jl.add(js);
    CandlepinQuery query = mock(CandlepinQuery.class);
    when(query.list()).thenReturn(jl);
    when(jobCurator.findWaitingJobs()).thenReturn(query);
    unpauseJob.execute(ctx);
    try {
        verify(pk, atLeastOnce()).addTrigger(js);
    } catch (SchedulerException e) {
        fail("Should not throw an exception");
    }
}
Also used : JobStatus(org.candlepin.pinsetter.core.model.JobStatus) JobDetail(org.quartz.JobDetail) SchedulerException(org.quartz.SchedulerException) ArrayList(java.util.ArrayList) CandlepinQuery(org.candlepin.model.CandlepinQuery) EmptyCandlepinQuery(org.candlepin.model.EmptyCandlepinQuery) Test(org.junit.Test)

Example 24 with JobStatus

use of org.candlepin.pinsetter.core.model.JobStatus in project candlepin by candlepin.

the class JobResourceTest method getStatusesByUuid.

@Test
public void getStatusesByUuid() {
    List<JobStatus> statuses = new ArrayList<>();
    JobStatus status = new JobStatus();
    statuses.add(status);
    CandlepinQuery query = mock(CandlepinQuery.class);
    when(query.list()).thenReturn(statuses);
    when(query.iterate()).thenReturn(new MockResultIterator(statuses.iterator()));
    when(query.iterate(anyInt(), anyBoolean())).thenReturn(new MockResultIterator(statuses.iterator()));
    when(jobCurator.findByConsumerUuid(eq("abcd"))).thenReturn(query);
    this.mockCPQueryTransform(query);
    Collection<JobStatusDTO> real = jobResource.getStatuses(null, "abcd", null).list();
    assertNotNull(real);
    assertEquals(1, real.size());
}
Also used : JobStatus(org.candlepin.pinsetter.core.model.JobStatus) ArrayList(java.util.ArrayList) TransformedCandlepinQuery(org.candlepin.model.TransformedCandlepinQuery) CandlepinQuery(org.candlepin.model.CandlepinQuery) JobStatusDTO(org.candlepin.dto.api.v1.JobStatusDTO) MockResultIterator(org.candlepin.test.MockResultIterator) Test(org.junit.Test)

Example 25 with JobStatus

use of org.candlepin.pinsetter.core.model.JobStatus in project candlepin by candlepin.

the class JobResourceTest method getStatusesByOwner.

@Test
public void getStatusesByOwner() {
    List<JobStatus> statuses = new ArrayList<>();
    JobStatus status = new JobStatus();
    statuses.add(status);
    CandlepinQuery query = mock(CandlepinQuery.class);
    when(query.list()).thenReturn(statuses);
    when(query.iterate()).thenReturn(new MockResultIterator(statuses.iterator()));
    when(query.iterate(anyInt(), anyBoolean())).thenReturn(new MockResultIterator(statuses.iterator()));
    when(jobCurator.findByOwnerKey(eq("admin"))).thenReturn(query);
    this.mockCPQueryTransform(query);
    Collection<JobStatusDTO> real = jobResource.getStatuses("admin", null, null).list();
    assertNotNull(real);
    assertEquals(1, real.size());
}
Also used : JobStatus(org.candlepin.pinsetter.core.model.JobStatus) ArrayList(java.util.ArrayList) TransformedCandlepinQuery(org.candlepin.model.TransformedCandlepinQuery) CandlepinQuery(org.candlepin.model.CandlepinQuery) JobStatusDTO(org.candlepin.dto.api.v1.JobStatusDTO) MockResultIterator(org.candlepin.test.MockResultIterator) Test(org.junit.Test)

Aggregations

JobStatus (org.candlepin.pinsetter.core.model.JobStatus)56 Test (org.junit.Test)43 JobDetail (org.quartz.JobDetail)22 JobKey (org.quartz.JobKey)10 ArrayList (java.util.ArrayList)7 CandlepinQuery (org.candlepin.model.CandlepinQuery)7 JobDataMap (org.quartz.JobDataMap)7 JobExecutionException (org.quartz.JobExecutionException)7 Trigger (org.quartz.Trigger)6 JobStatusDTO (org.candlepin.dto.api.v1.JobStatusDTO)5 TransformedCandlepinQuery (org.candlepin.model.TransformedCandlepinQuery)5 SchedulerException (org.quartz.SchedulerException)5 Principal (org.candlepin.auth.Principal)4 Date (java.util.Date)3 HashSet (java.util.HashSet)3 ConsumerPrincipal (org.candlepin.auth.ConsumerPrincipal)3 JobCurator (org.candlepin.model.JobCurator)3 MockResultIterator (org.candlepin.test.MockResultIterator)3 CronTrigger (org.quartz.CronTrigger)3 Transactional (com.google.inject.persist.Transactional)2