Search in sources :

Example 6 with SleepActionExecuter

use of org.alfresco.repo.action.ActionServiceImplTest.SleepActionExecuter in project alfresco-repository by Alfresco.

the class ActionTrackingServiceImplTest method testWorkingActions.

/**
 * Working actions go into the cache, then out
 */
public void testWorkingActions() throws Exception {
    final SleepActionExecuter sleepActionExec = (SleepActionExecuter) ctx.getBean(SleepActionExecuter.NAME);
    sleepActionExec.resetTimesExecuted();
    sleepActionExec.setSleepMs(10000);
    // Have it run asynchronously
    UserTransaction txn = transactionService.getUserTransaction();
    txn.begin();
    Action action = createWorkingSleepAction("54321");
    assertNull(action.getExecutionStartDate());
    assertNull(action.getExecutionEndDate());
    assertNull(action.getExecutionFailureMessage());
    assertEquals(ActionStatus.New, action.getExecutionStatus());
    String key = ActionTrackingServiceImpl.generateCacheKey(action);
    assertEquals(null, executingActionsCache.get(key));
    this.actionService.executeAction(action, this.nodeRef, false, true);
    // End the transaction. Should allow the async action
    // to start up, and begin sleeping
    txn.commit();
    Thread.sleep(150);
    // The action should now be running
    // It will have got an execution instance id, so a new key
    key = ActionTrackingServiceImpl.generateCacheKey(action);
    // Check it's in the cache
    System.out.println("Checking the cache for " + key);
    assertNotNull(executingActionsCache.get(key));
    ExecutionSummary s = ActionTrackingServiceImpl.buildExecutionSummary(action);
    ExecutionDetails d = actionTrackingService.getExecutionDetails(s);
    assertNotNull(d.getExecutionSummary());
    assertEquals("sleep-action", d.getActionType());
    assertEquals("54321", d.getActionId());
    assertEquals(1, d.getExecutionInstance());
    assertEquals(null, d.getPersistedActionRef());
    assertNotNull(null, d.getStartedAt());
    // Tell it to stop sleeping
    // Then wait for it to finish
    asyncOccurs.awaitExecution(null, sleepActionExec.getExecutingThread(), action.getActionDefinitionName());
    // Ensure it went away again
    assertEquals(ActionStatus.Completed, action.getExecutionStatus());
    assertEquals(null, executingActionsCache.get(key));
    d = actionTrackingService.getExecutionDetails(s);
    assertEquals(null, d);
}
Also used : UserTransaction(javax.transaction.UserTransaction) Action(org.alfresco.service.cmr.action.Action) CancellableSleepAction(org.alfresco.repo.action.ActionServiceImplTest.CancellableSleepAction) SleepActionExecuter(org.alfresco.repo.action.ActionServiceImplTest.SleepActionExecuter) ExecutionSummary(org.alfresco.service.cmr.action.ExecutionSummary) ExecutionDetails(org.alfresco.service.cmr.action.ExecutionDetails)

Example 7 with SleepActionExecuter

use of org.alfresco.repo.action.ActionServiceImplTest.SleepActionExecuter 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)

Aggregations

SleepActionExecuter (org.alfresco.repo.action.ActionServiceImplTest.SleepActionExecuter)7 UserTransaction (javax.transaction.UserTransaction)5 Date (java.util.Date)4 CancellableSleepAction (org.alfresco.repo.action.ActionServiceImplTest.CancellableSleepAction)4 Action (org.alfresco.service.cmr.action.Action)3 ExecutionSummary (org.alfresco.service.cmr.action.ExecutionSummary)3 ExecutionDetails (org.alfresco.service.cmr.action.ExecutionDetails)2 ScheduledPersistedAction (org.alfresco.service.cmr.action.scheduled.ScheduledPersistedAction)2 BaseSpringTest (org.alfresco.util.BaseSpringTest)2 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 ClasspathScriptLocation (org.alfresco.repo.jscript.ClasspathScriptLocation)1 ActionServiceTransientException (org.alfresco.service.cmr.action.ActionServiceTransientException)1 NodeRef (org.alfresco.service.cmr.repository.NodeRef)1 ScriptLocation (org.alfresco.service.cmr.repository.ScriptLocation)1