Search in sources :

Example 1 with ScheduledPersistedAction

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

the class ScheduledPersistedActionServiceImpl method listSchedules.

private List<ScheduledPersistedAction> listSchedules(NodeService nodeService) {
    List<ChildAssociationRef> childAssocs = nodeService.getChildAssocs(SCHEDULED_ACTION_ROOT_NODE_REF, ACTION_TYPES);
    List<ScheduledPersistedAction> scheduledActions = new ArrayList<ScheduledPersistedAction>(childAssocs.size());
    for (ChildAssociationRef actionAssoc : childAssocs) {
        ScheduledPersistedActionImpl scheduleImpl = loadPersistentSchedule(actionAssoc.getChildRef());
        scheduledActions.add(scheduleImpl);
    }
    return scheduledActions;
}
Also used : ScheduledPersistedAction(org.alfresco.service.cmr.action.scheduled.ScheduledPersistedAction) ArrayList(java.util.ArrayList) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef)

Example 2 with ScheduledPersistedAction

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

the class QuickShareServiceImpl method saveSharedLinkExpiryAction.

/**
 * Creates and persists the quick share link expiry action and its related schedule.
 */
protected void saveSharedLinkExpiryAction(String sharedId, Date expiryDate) {
    ParameterCheck.mandatory("expiryDate", expiryDate);
    // Validate the given expiry date
    checkExpiryDate(expiryDate);
    if (logger.isDebugEnabled()) {
        logger.debug("Creating shared link expiry action for the sharedId:" + sharedId);
    }
    final NodeRef expiryActionNodeRef = getQuickShareLinkExpiryActionNode(sharedId);
    // If an expiry action already exists for the specified shared Id, first remove it, before creating a new one.
    if (expiryActionNodeRef != null) {
        deleteQuickShareLinkExpiryAction(expiryActionNodeRef);
    }
    // Create the expiry action
    final QuickShareLinkExpiryAction expiryAction = new QuickShareLinkExpiryActionImpl(java.util.UUID.randomUUID().toString(), sharedId, "QuickShare link expiry action");
    // Create the persisted schedule
    final ScheduledPersistedAction schedule = scheduledPersistedActionService.createSchedule(expiryAction);
    // first set the scheduledAction so we can set the other information
    expiryAction.setSchedule(schedule);
    expiryAction.setScheduleStart(expiryDate);
    try {
        TenantUtil.runAsDefaultTenant((TenantRunAsWork<Void>) () -> {
            quickShareLinkExpiryActionPersister.saveQuickShareLinkExpiryAction(expiryAction);
            scheduledPersistedActionService.saveSchedule(schedule);
            return null;
        });
    } catch (Exception ex) {
        throw new QuickShareLinkExpiryActionException("Couldn't create quick share link expiry action.", ex);
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Quick share link expiry action is created for sharedId[" + sharedId + "] and it's scheduled to be executed on: " + expiryDate);
    }
}
Also used : ScheduledPersistedAction(org.alfresco.service.cmr.action.scheduled.ScheduledPersistedAction) NodeRef(org.alfresco.service.cmr.repository.NodeRef) QuickShareLinkExpiryAction(org.alfresco.service.cmr.quickshare.QuickShareLinkExpiryAction) ClientAppNotFoundException(org.alfresco.repo.client.config.ClientAppNotFoundException) AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) InvalidSharedIdException(org.alfresco.service.cmr.quickshare.InvalidSharedIdException) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException) QuickShareDisabledException(org.alfresco.service.cmr.quickshare.QuickShareDisabledException) NoSuchPersonException(org.alfresco.service.cmr.security.NoSuchPersonException)

Example 3 with ScheduledPersistedAction

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

the class ScheduledPersistedActionServiceTest method testStartup.

/**
 * Tests that the startup registering works properly
 */
@Test
public void testStartup() throws Exception {
    // Startup with none there, nothing happens
    assertEquals(0, scheduler.getJobKeys(GroupMatcher.groupEquals(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP)).size());
    assertEquals(0, service.listSchedules().size());
    bootstrap.onBootstrap(null);
    assertEquals(0, scheduler.getJobKeys(GroupMatcher.groupEquals(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP)).size());
    assertEquals(0, service.listSchedules().size());
    // Manually add a scheduled action
    // Does a bit of faffing to have it not in Quartz initially
    long future = System.currentTimeMillis() + 1234567;
    ScheduledPersistedAction schedule = service.createSchedule(testAction);
    schedule.setScheduleStart(new Date(future));
    service.saveSchedule(schedule);
    ((ScheduledPersistedActionServiceImpl) applicationContext.getBean("scheduledPersistedActionService")).removeFromScheduler((ScheduledPersistedActionImpl) schedule);
    assertEquals(0, scheduler.getJobKeys(GroupMatcher.groupEquals(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP)).size());
    assertEquals(1, service.listSchedules().size());
    // Now do the bootstrap, and see it get registered
    bootstrap.onBootstrap(null);
    assertEquals(1, scheduler.getJobKeys(GroupMatcher.groupEquals(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP)).size());
    assertEquals(1, service.listSchedules().size());
}
Also used : ScheduledPersistedAction(org.alfresco.service.cmr.action.scheduled.ScheduledPersistedAction) Date(java.util.Date) BaseSpringTest(org.alfresco.util.BaseSpringTest) Test(org.junit.Test)

Example 4 with ScheduledPersistedAction

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

the class ScheduledPersistedActionServiceTest method testCreation.

/**
 * Tests that we can create, save, edit, delete etc the scheduled persisted actions
 */
@Test
public void testCreation() {
    ScheduledPersistedAction schedule = service.createSchedule(testAction);
    assertNotNull(schedule);
    assertTrue(testAction == schedule.getAction());
    assertEquals(testAction.getNodeRef(), schedule.getAction().getNodeRef());
    assertNull(schedule.getScheduleStart());
    assertNull(schedule.getScheduleInterval());
    assertNull(schedule.getScheduleIntervalCount());
    assertNull(schedule.getScheduleIntervalPeriod());
    Date now = new Date();
    schedule.setScheduleStart(now);
    assertEquals(now, schedule.getScheduleStart());
    schedule.setScheduleIntervalCount(2);
    assertEquals(new Integer(2), schedule.getScheduleIntervalCount());
    schedule.setScheduleIntervalPeriod(ScheduledPersistedAction.IntervalPeriod.Day);
    assertEquals(ScheduledPersistedAction.IntervalPeriod.Day, schedule.getScheduleIntervalPeriod());
}
Also used : ScheduledPersistedAction(org.alfresco.service.cmr.action.scheduled.ScheduledPersistedAction) Date(java.util.Date) BaseSpringTest(org.alfresco.util.BaseSpringTest) Test(org.junit.Test)

Example 5 with ScheduledPersistedAction

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

the class ScheduledPersistedActionServiceTest method testMultipleExecutions.

/**
 * Tests that when we have more than one schedule defined and active, then the correct things run at the correct
 * times, and we never get confused
 */
@Test
public void testMultipleExecutions() throws Exception {
    final SleepActionExecuter sleepActionExec = (SleepActionExecuter) applicationContext.getBean(SleepActionExecuter.NAME);
    sleepActionExec.resetTimesExecuted();
    sleepActionExec.setSleepMs(1);
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            // Create one that starts running in 2 seconds, runs every 2
            // seconds
            // until 9 seconds are up (will run 4 times)
            ScheduledPersistedActionImpl scheduleA = (ScheduledPersistedActionImpl) service.createSchedule(testAction);
            scheduleA.setScheduleStart(new Date(System.currentTimeMillis() + 2000));
            scheduleA.setScheduleEnd(new Date(System.currentTimeMillis() + 9000));
            scheduleA.setScheduleIntervalCount(2);
            scheduleA.setScheduleIntervalPeriod(IntervalPeriod.Second);
            service.saveSchedule(scheduleA);
            // Create one that starts running now, every second until 9.5
            // seconds
            // are up (will run 9-10 times)
            ScheduledPersistedActionImpl scheduleB = (ScheduledPersistedActionImpl) service.createSchedule(testAction2);
            scheduleB.setScheduleStart(new Date(System.currentTimeMillis()));
            scheduleB.setScheduleEnd(new Date(System.currentTimeMillis() + 9500));
            scheduleB.setScheduleIntervalCount(1);
            scheduleB.setScheduleIntervalPeriod(IntervalPeriod.Second);
            service.saveSchedule(scheduleB);
            return null;
        }
    }, false, true);
    // Set them going
    scheduler.start();
    // Wait for 10 seconds for them to run and finish
    Thread.sleep(10 * 1000);
    // Check that they really did run properly
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            ScheduledPersistedAction scheduleA = service.getSchedule(testAction);
            ScheduledPersistedAction scheduleB = service.getSchedule(testAction2);
            // Both should have last run at some point in the last second
            // or two
            assertEquals((double) System.currentTimeMillis(), (double) scheduleA.getScheduleLastExecutedAt().getTime(), 2500);
            assertEquals((double) System.currentTimeMillis(), (double) scheduleB.getScheduleLastExecutedAt().getTime(), 2500);
            // A should have run ~4 times
            // B should have run ~9 times
            assertEquals("Didn't run enough - " + sleepActionExec.getTimesExecuted(), true, sleepActionExec.getTimesExecuted() >= 11);
            assertEquals("Ran too much - " + sleepActionExec.getTimesExecuted(), true, sleepActionExec.getTimesExecuted() < 16);
            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

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