Search in sources :

Example 6 with ScheduledPersistedAction

use of org.alfresco.service.cmr.action.scheduled.ScheduledPersistedAction in project alfresco-repository by Alfresco.

the class ScheduledPersistedActionServiceTest method testLoadList.

/**
 * Tests that the listings work, both of all scheduled, and from an action
 */
@Test
public void testLoadList() throws Exception {
    assertEquals(0, service.listSchedules().size());
    // Create
    ScheduledPersistedAction schedule1 = service.createSchedule(testAction);
    assertNotNull(schedule1);
    ScheduledPersistedAction schedule2 = service.createSchedule(testAction2);
    assertNotNull(schedule2);
    assertEquals(0, service.listSchedules().size());
    service.saveSchedule(schedule1);
    assertEquals(1, service.listSchedules().size());
    assertEquals(testAction.getNodeRef(), service.listSchedules().get(0).getActionNodeRef());
    service.saveSchedule(schedule2);
    assertEquals(2, service.listSchedules().size());
}
Also used : ScheduledPersistedAction(org.alfresco.service.cmr.action.scheduled.ScheduledPersistedAction) BaseSpringTest(org.alfresco.util.BaseSpringTest) Test(org.junit.Test)

Example 7 with ScheduledPersistedAction

use of org.alfresco.service.cmr.action.scheduled.ScheduledPersistedAction in project alfresco-repository by Alfresco.

the class ScheduledPersistedActionServiceTest method tearDown.

@After
public void tearDown() throws Exception {
    UserTransaction txn = transactionService.getUserTransaction();
    txn.begin();
    // Zap all test schedules
    List<ScheduledPersistedAction> schedules = service.listSchedules();
    for (ScheduledPersistedAction schedule : schedules) {
        service.deleteSchedule(schedule);
    }
    txn.commit();
    // Re-enable the scheduler again
    scheduler.start();
}
Also used : UserTransaction(javax.transaction.UserTransaction) ScheduledPersistedAction(org.alfresco.service.cmr.action.scheduled.ScheduledPersistedAction) After(org.junit.After)

Example 8 with ScheduledPersistedAction

use of org.alfresco.service.cmr.action.scheduled.ScheduledPersistedAction in project alfresco-repository by Alfresco.

the class ReplicationServiceImpl method enableScheduling.

public void enableScheduling(ReplicationDefinition replicationDefinition) {
    if (!replicationDefinition.isSchedulingEnabled()) {
        ScheduledPersistedAction schedule = scheduledPersistedActionService.createSchedule(replicationDefinition);
        ((ReplicationDefinitionImpl) replicationDefinition).setSchedule(schedule);
    }
}
Also used : ScheduledPersistedAction(org.alfresco.service.cmr.action.scheduled.ScheduledPersistedAction)

Example 9 with ScheduledPersistedAction

use of org.alfresco.service.cmr.action.scheduled.ScheduledPersistedAction in project alfresco-repository by Alfresco.

the class ScheduledPersistedActionServiceTest method testExecution.

/**
 * Tests that things actually get run correctly. Each sub-test runs in its own transaction
 */
@Test
public void testExecution() throws Exception {
    final SleepActionExecuter sleepActionExec = (SleepActionExecuter) applicationContext.getBean(SleepActionExecuter.NAME);
    sleepActionExec.resetTimesExecuted();
    sleepActionExec.setSleepMs(1);
    // This test needs the scheduler running properly
    scheduler.start();
    // Until the schedule is persisted, nothing will happen
    @SuppressWarnings("unused") ScheduledPersistedAction schedule = service.createSchedule(testAction);
    assertEquals(0, scheduler.getJobKeys(GroupMatcher.groupEquals(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP)).size());
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            // A job due to start in 1 second, and run once
            ScheduledPersistedAction schedule = service.createSchedule(testAction);
            schedule.setScheduleStart(new Date(System.currentTimeMillis() + 1000));
            assertNull(schedule.getScheduleInterval());
            assertNull(schedule.getScheduleIntervalCount());
            assertNull(schedule.getScheduleIntervalPeriod());
            assertNull(schedule.getScheduleLastExecutedAt());
            System.out.println("Job starts in 1 second, no repeat...");
            service.saveSchedule(schedule);
            return null;
        }
    }, false, true);
    // Check it went in
    assertEquals(1, scheduler.getJobKeys(GroupMatcher.groupEquals(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP)).size());
    // Let it run
    Thread.sleep(2000);
    // Ensure it did properly run the once
    assertEquals(1, sleepActionExec.getTimesExecuted());
    // Should have removed itself now the schedule is over
    assertEquals(0, scheduler.getJobKeys(GroupMatcher.groupEquals(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP)).size());
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            // Ensure it was tagged with when it ran
            ScheduledPersistedAction schedule = service.getSchedule(testAction);
            assertEquals((double) System.currentTimeMillis(), (double) schedule.getScheduleLastExecutedAt().getTime(), // Within 2.5 secs
            2500);
            // Zap it
            service.deleteSchedule(schedule);
            assertEquals(0, scheduler.getJobKeys(GroupMatcher.groupEquals(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP)).size());
            return null;
        }
    }, false, true);
    // ==========================
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            // A job that runs every 2 seconds, for the next 3.5 seconds
            // (Should get to run twice, now and @2 secs)
            ScheduledPersistedAction schedule = service.createSchedule(testAction);
            schedule.setScheduleStart(new Date(System.currentTimeMillis() - 50));
            ((ScheduledPersistedActionImpl) schedule).setScheduleEnd(new Date(System.currentTimeMillis() + 3500));
            schedule.setScheduleIntervalCount(2);
            schedule.setScheduleIntervalPeriod(IntervalPeriod.Second);
            assertEquals("2Second", schedule.getScheduleInterval());
            // Reset count
            sleepActionExec.resetTimesExecuted();
            assertEquals(0, sleepActionExec.getTimesExecuted());
            service.saveSchedule(schedule);
            System.out.println("Job " + ((ScheduledPersistedActionImpl) schedule).getPersistedAtNodeRef() + " starts now, repeats twice @ 2s");
            return null;
        }
    }, false, true);
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            Thread.sleep(4250);
            ScheduledPersistedAction schedule = service.getSchedule(testAction);
            // runs)
            if (sleepActionExec.getTimesExecuted() == 3) {
                assertEquals(3, sleepActionExec.getTimesExecuted());
            } else {
                assertEquals(2, sleepActionExec.getTimesExecuted());
            }
            // Zap it
            service.deleteSchedule(schedule);
            assertEquals(0, scheduler.getJobKeys(GroupMatcher.groupEquals(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP)).size());
            return null;
        }
    }, false, true);
    // ==========================
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            // A job that starts in 2 seconds time, and runs
            // every second until we kill it
            ScheduledPersistedAction schedule = service.createSchedule(testAction);
            schedule.setScheduleStart(new Date(System.currentTimeMillis() + 2000));
            schedule.setScheduleIntervalCount(1);
            schedule.setScheduleIntervalPeriod(IntervalPeriod.Second);
            assertEquals("1Second", schedule.getScheduleInterval());
            // Reset count
            sleepActionExec.resetTimesExecuted();
            assertEquals(0, sleepActionExec.getTimesExecuted());
            System.out.println("Job starts in 2s, repeats @ 1s");
            service.saveSchedule(schedule);
            return null;
        }
    }, false, true);
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            // Let it run a few times
            Thread.sleep(5000);
            // Zap it - should still be live
            ScheduledPersistedAction schedule = service.getSchedule(testAction);
            assertEquals(1, scheduler.getJobKeys(GroupMatcher.groupEquals(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP)).size());
            service.deleteSchedule(schedule);
            assertEquals(0, scheduler.getJobKeys(GroupMatcher.groupEquals(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP)).size());
            // Check it ran an appropriate number of times
            assertEquals("Didn't run enough - " + sleepActionExec.getTimesExecuted(), true, sleepActionExec.getTimesExecuted() >= 3);
            assertEquals("Ran too much - " + sleepActionExec.getTimesExecuted(), true, sleepActionExec.getTimesExecuted() < 5);
            // Ensure it finished shutting down
            Thread.sleep(500);
            return null;
        }
    }, false, true);
}
Also used : ScheduledPersistedAction(org.alfresco.service.cmr.action.scheduled.ScheduledPersistedAction) SleepActionExecuter(org.alfresco.repo.action.ActionServiceImplTest.SleepActionExecuter) Date(java.util.Date) BaseSpringTest(org.alfresco.util.BaseSpringTest) Test(org.junit.Test)

Example 10 with ScheduledPersistedAction

use of org.alfresco.service.cmr.action.scheduled.ScheduledPersistedAction in project alfresco-repository by Alfresco.

the class ScheduledPersistedActionServiceTest method testDeletion.

/**
 * Ensures that deletion works correctly
 */
@Test
public void testDeletion() throws Exception {
    // Delete does nothing if not persisted
    assertEquals(0, service.listSchedules().size());
    ScheduledPersistedAction schedule1 = service.createSchedule(testAction);
    assertEquals(0, service.listSchedules().size());
    service.deleteSchedule(schedule1);
    assertEquals(0, service.listSchedules().size());
    // Create and save two
    ScheduledPersistedAction schedule2 = service.createSchedule(testAction2);
    service.saveSchedule(schedule1);
    service.saveSchedule(schedule2);
    assertEquals(2, service.listSchedules().size());
    NodeRef schedule1NodeRef = ((ScheduledPersistedActionImpl) schedule1).getPersistedAtNodeRef();
    NodeRef schedule2NodeRef = ((ScheduledPersistedActionImpl) schedule2).getPersistedAtNodeRef();
    // Both should have the relationship
    assertEquals(1, nodeService.getTargetAssocs(schedule1NodeRef, RegexQNamePattern.MATCH_ALL).size());
    assertEquals(1, nodeService.getTargetAssocs(schedule2NodeRef, RegexQNamePattern.MATCH_ALL).size());
    assertEquals(1, nodeService.getSourceAssocs(testAction.getNodeRef(), RegexQNamePattern.MATCH_ALL).size());
    assertEquals(1, nodeService.getSourceAssocs(testAction2.getNodeRef(), RegexQNamePattern.MATCH_ALL).size());
    // Delete one - the correct one goes!
    service.deleteSchedule(schedule2);
    assertEquals(1, service.listSchedules().size());
    assertEquals(testAction.getNodeRef(), service.listSchedules().get(0).getActionNodeRef());
    assertNotNull(serviceImpl.loadPersistentSchedule(schedule1NodeRef));
    assertNull(serviceImpl.loadPersistentSchedule(schedule2NodeRef));
    assertNotNull(service.getSchedule(testAction));
    assertNull(service.getSchedule(testAction2));
    // Ensure that the relationship went
    assertEquals(1, nodeService.getTargetAssocs(schedule1NodeRef, RegexQNamePattern.MATCH_ALL).size());
    assertEquals(1, nodeService.getSourceAssocs(testAction.getNodeRef(), RegexQNamePattern.MATCH_ALL).size());
    assertEquals(0, nodeService.getSourceAssocs(testAction2.getNodeRef(), RegexQNamePattern.MATCH_ALL).size());
    // Re-delete already deleted, no change
    service.deleteSchedule(schedule2);
    assertEquals(1, service.listSchedules().size());
    assertEquals(testAction.getNodeRef(), service.listSchedules().get(0).getActionNodeRef());
    assertNotNull(serviceImpl.loadPersistentSchedule(schedule1NodeRef));
    assertNull(serviceImpl.loadPersistentSchedule(schedule2NodeRef));
    assertNotNull(service.getSchedule(testAction));
    assertNull(service.getSchedule(testAction2));
    // Delete the 2nd
    service.deleteSchedule(schedule1);
    assertEquals(0, service.listSchedules().size());
    assertNull(serviceImpl.loadPersistentSchedule(schedule1NodeRef));
    assertNull(serviceImpl.loadPersistentSchedule(schedule2NodeRef));
    assertNull(service.getSchedule(testAction));
    assertNull(service.getSchedule(testAction2));
    // Ensure that the relationship went
    assertEquals(0, nodeService.getSourceAssocs(testAction.getNodeRef(), RegexQNamePattern.MATCH_ALL).size());
    assertEquals(0, nodeService.getSourceAssocs(testAction2.getNodeRef(), RegexQNamePattern.MATCH_ALL).size());
    // Can add back in again after being deleted
    service.saveSchedule(schedule1);
    assertEquals(1, service.listSchedules().size());
    assertEquals(testAction.getNodeRef(), service.listSchedules().get(0).getActionNodeRef());
    assertNotNull(service.getSchedule(testAction));
    assertNull(service.getSchedule(testAction2));
    // If we delete the action, then the schedule must go as well
    UserTransaction txn = transactionService.getUserTransaction();
    txn.begin();
    nodeService.deleteNode(testAction.getNodeRef());
    txn.commit();
    assertEquals(0, service.listSchedules().size());
}
Also used : ScheduledPersistedAction(org.alfresco.service.cmr.action.scheduled.ScheduledPersistedAction) UserTransaction(javax.transaction.UserTransaction) NodeRef(org.alfresco.service.cmr.repository.NodeRef) BaseSpringTest(org.alfresco.util.BaseSpringTest) Test(org.junit.Test)

Aggregations

ScheduledPersistedAction (org.alfresco.service.cmr.action.scheduled.ScheduledPersistedAction)16 BaseSpringTest (org.alfresco.util.BaseSpringTest)9 Test (org.junit.Test)9 Date (java.util.Date)6 UserTransaction (javax.transaction.UserTransaction)4 SleepActionExecuter (org.alfresco.repo.action.ActionServiceImplTest.SleepActionExecuter)2 QuickShareLinkExpiryAction (org.alfresco.service.cmr.quickshare.QuickShareLinkExpiryAction)2 NodeRef (org.alfresco.service.cmr.repository.NodeRef)2 ArrayList (java.util.ArrayList)1 ClientAppNotFoundException (org.alfresco.repo.client.config.ClientAppNotFoundException)1 AccessDeniedException (org.alfresco.repo.security.permissions.AccessDeniedException)1 InvalidSharedIdException (org.alfresco.service.cmr.quickshare.InvalidSharedIdException)1 QuickShareDisabledException (org.alfresco.service.cmr.quickshare.QuickShareDisabledException)1 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)1 InvalidNodeRefException (org.alfresco.service.cmr.repository.InvalidNodeRefException)1 NoSuchPersonException (org.alfresco.service.cmr.security.NoSuchPersonException)1 After (org.junit.After)1 Before (org.junit.Before)1