Search in sources :

Example 1 with DisabledReplicationJobException

use of org.alfresco.service.cmr.replication.DisabledReplicationJobException in project alfresco-repository by Alfresco.

the class ReplicationServiceIntegrationTest method testBasicExecution.

/**
 * Test that the action service can find the executor
 *  for us, and that it has everything it needs
 */
public void testBasicExecution() throws Exception {
    // We need the test transfer target for this test
    makeTransferTarget();
    // Ensure the destination is empty
    // (don't want to get confused with older runs)
    assertEquals(0, nodeService.getChildAssocs(destinationFolder).size());
    // First one with no target, which isn't allowed
    ReplicationDefinition rd = replicationService.createReplicationDefinition(ACTION_NAME, "Test");
    UserTransaction txn = transactionService.getUserTransaction();
    txn.begin();
    try {
        actionService.executeAction(rd, replicationRoot);
        fail("Shouldn't be permitted with no Target defined");
    } catch (ReplicationServiceException e) {
    }
    txn.rollback();
    // Now no payload, also not allowed
    rd.setTargetName(TRANSFER_TARGET);
    txn = transactionService.getUserTransaction();
    txn.begin();
    try {
        actionService.executeAction(rd, replicationRoot);
        fail("Shouldn't be permitted with no payload defined");
    } catch (ReplicationServiceException e) {
    }
    txn.rollback();
    // Invalid Transfer Target, not allowed
    rd = replicationService.createReplicationDefinition(ACTION_NAME, "Test");
    rd.setTargetName("I am an invalid target that isn't there");
    rd.getPayload().add(folder1);
    txn = transactionService.getUserTransaction();
    txn.begin();
    try {
        actionService.executeAction(rd, replicationRoot);
        fail("Shouldn't be permitted with an invalid transfer target");
    } catch (ReplicationServiceException e) {
    }
    txn.rollback();
    // Can't send Folder2a if Folder2 isn't there, as it
    // won't have anywhere to put it
    rd = replicationService.createReplicationDefinition(ACTION_NAME, "Test");
    rd.setTargetName(TRANSFER_TARGET);
    rd.getPayload().add(folder2a);
    txn = transactionService.getUserTransaction();
    txn.begin();
    try {
        actionService.executeAction(rd, replicationRoot);
        fail("Shouldn't be able to send Folder2a when Folder2 is missing!");
    } catch (ReplicationServiceException e) {
    }
    txn.rollback();
    // Next a proper one with a transient definition,
    // and a sensible set of folders
    rd = replicationService.createReplicationDefinition(ACTION_NAME, "Test");
    rd.setTargetName(TRANSFER_TARGET);
    rd.getPayload().add(folder1);
    // A deleted folder is fine, will be skipped
    rd.getPayload().add(deletedFolder);
    // Will execute without error
    txn = transactionService.getUserTransaction();
    txn.begin();
    try {
        actionService.executeAction(rd, replicationRoot);
    } catch (ReplicationServiceException e) {
        // This shouldn't happen normally! Something is wrong!
        // Tidy up before we throw the exception
        txn.rollback();
        throw e;
    }
    txn.commit();
    // Now with one that's in the repo
    ReplicationDefinition rd2 = replicationService.createReplicationDefinition(ACTION_NAME2, "Test");
    rd2.setTargetName(TRANSFER_TARGET);
    rd2.getPayload().add(folder2);
    replicationService.saveReplicationDefinition(rd2);
    rd2 = replicationService.loadReplicationDefinition(ACTION_NAME2);
    // Again no errors
    txn = transactionService.getUserTransaction();
    txn.begin();
    actionService.executeAction(rd2, replicationRoot);
    txn.commit();
    // Now disabled, not allowed
    assertEquals(true, rd.isEnabled());
    rd.setEnabled(false);
    assertEquals(false, rd.isEnabled());
    txn = transactionService.getUserTransaction();
    txn.begin();
    try {
        actionService.executeAction(rd, replicationRoot);
        fail("Shouldn't be permitted when disabled");
    } catch (ReplicationServiceException e) {
        // check if throwed exception is of expected type
        assertTrue(e instanceof DisabledReplicationJobException);
        assertTrue(actionService instanceof RuntimeActionService);
        if (actionService instanceof RuntimeActionService) {
            RuntimeActionService runtimeActionService = (RuntimeActionService) actionService;
            // check if throwed exception is considered handled
            assertTrue(runtimeActionService.onLogException(rd, log, e, e.getMessage()));
        }
    }
    txn.rollback();
    rd.setEnabled(true);
    // Schedule it for 0.5 seconds into the future
    // Ensure that it is run to completion
    txn = transactionService.getUserTransaction();
    txn.begin();
    ((ActionImpl) rd2).setExecutionStatus(ActionStatus.New);
    replicationService.enableScheduling(rd2);
    rd2.setScheduleStart(new Date(System.currentTimeMillis() + 500));
    replicationService.saveReplicationDefinition(rd2);
    txn.commit();
    // Wait for it to run
    Thread.sleep(2000);
    for (int i = 0; i < 100; i++) {
        txn = transactionService.getUserTransaction();
        txn.begin();
        rd2 = replicationService.loadReplicationDefinition(ACTION_NAME2);
        txn.commit();
        if (rd2.getExecutionStatus().equals(ActionStatus.New) || rd2.getExecutionStatus().equals(ActionStatus.Pending) || rd2.getExecutionStatus().equals(ActionStatus.Running)) {
            Thread.sleep(50);
        }
    }
    // Check it worked
    assertEquals(ActionStatus.Completed, rd2.getExecutionStatus());
}
Also used : UserTransaction(javax.transaction.UserTransaction) DisabledReplicationJobException(org.alfresco.service.cmr.replication.DisabledReplicationJobException) ReplicationServiceException(org.alfresco.service.cmr.replication.ReplicationServiceException) ScriptReplicationDefinition(org.alfresco.repo.replication.script.ScriptReplicationDefinition) ReplicationDefinition(org.alfresco.service.cmr.replication.ReplicationDefinition) RuntimeActionService(org.alfresco.repo.action.RuntimeActionService) ActionImpl(org.alfresco.repo.action.ActionImpl) Date(java.util.Date)

Example 2 with DisabledReplicationJobException

use of org.alfresco.service.cmr.replication.DisabledReplicationJobException in project alfresco-repository by Alfresco.

the class ReplicationActionExecutor method executeImpl.

@Override
protected void executeImpl(Action action, NodeRef actionedUponNodeRef) {
    if (action instanceof ReplicationDefinition) {
    // Already of the correct type
    } else if (action.getActionDefinitionName().equals(ReplicationDefinitionImpl.EXECUTOR_NAME)) {
        // Specialise the action if needed, eg when loaded directly from
        // the NodeRef without going via the replication service
        action = new ReplicationDefinitionImpl(action);
    }
    // Off we go
    final ReplicationDefinition replicationDef = (ReplicationDefinition) action;
    if (replicationDef.getTargetName() == null || replicationDef.getTargetName().equals("")) {
        throw new ReplicationServiceException(I18NUtil.getMessage(MSG_ERR_TARGET_NOT_GIVEN));
    }
    if (replicationDef.getPayload().size() == 0) {
        throw new ReplicationServiceException(I18NUtil.getMessage(MSG_ERR_NO_PAYLOADS_SPECIFIED));
    }
    if (!replicationDef.isEnabled()) {
        throw new DisabledReplicationJobException(I18NUtil.getMessage(MSG_ERR_REPLICATION_DEF_DISABLED));
    }
    if (!replicationParams.isEnabled()) {
        throw new ReplicationServiceException(I18NUtil.getMessage(MSG_ERR_UNABLE_TO_REPLICATE));
    }
    // Lock the service - only one instance of the replication
    // should occur at a time
    ReplicationDefinitionLockExtender lock = new ReplicationDefinitionLockExtender(replicationDef);
    // Turn our payload list of root nodes into something that
    // the transfer service can work with
    Set<NodeRef> toTransfer;
    try {
        toTransfer = expandPayload(replicationDef);
    } catch (Exception e) {
        lock.close();
        throw new ReplicationServiceException(I18NUtil.getMessage(MSG_ERR_PROCESSING_PAYLOAD, e.getMessage()), e);
    }
    // Ask the transfer service to do the replication
    // work for us
    TransferEndEvent endEvent = null;
    try {
        // Build the definition
        TransferDefinition transferDefinition = buildTransferDefinition(replicationDef, toTransfer);
        // Off we go
        endEvent = transferService.transfer(replicationDef.getTargetName(), transferDefinition, lock);
        if (endEvent instanceof TransferEventCancelled) {
            if (logger.isDebugEnabled())
                logger.debug("Cancelling replication job");
            // that this is correctly recorded
            throw new ActionCancelledException(replicationDef);
        }
        // Record details of the transfer reports (in success case)
        replicationDef.setLocalTransferReport(endEvent.getSourceReport());
        replicationDef.setRemoteTransferReport(endEvent.getDestinationReport());
        replicationDefinitionPersister.saveReplicationDefinition(replicationDef);
    } catch (Exception e) {
        if (e instanceof ActionCancelledException) {
            writeDefinitionReports(replicationDef, endEvent.getSourceReport(), endEvent.getDestinationReport());
            throw (ActionCancelledException) e;
        }
        if (e instanceof TransferFailureException) {
            TransferEventError failureEndEvent = ((TransferFailureException) e).getErrorEvent();
            writeDefinitionReports(replicationDef, failureEndEvent.getSourceReport(), failureEndEvent.getDestinationReport());
            Throwable cause = (e.getCause() == null) ? e : e.getCause();
            throw new ReplicationServiceException(I18NUtil.getMessage(MSG_ERR_EXECUTING_TRANSFER, cause.getMessage()), cause);
        }
        writeDefinitionReports(replicationDef, null, null);
        throw new ReplicationServiceException(I18NUtil.getMessage(MSG_ERR_EXECUTING_TRANSFER, e.getMessage()), e);
    } finally {
        lock.close();
    }
}
Also used : ReplicationServiceException(org.alfresco.service.cmr.replication.ReplicationServiceException) TransferEndEvent(org.alfresco.service.cmr.transfer.TransferEndEvent) TransferEventCancelled(org.alfresco.service.cmr.transfer.TransferEventCancelled) DisabledReplicationJobException(org.alfresco.service.cmr.replication.DisabledReplicationJobException) ActionCancelledException(org.alfresco.repo.action.ActionCancelledException) TransferFailureException(org.alfresco.service.cmr.transfer.TransferFailureException) LockAcquisitionException(org.alfresco.repo.lock.LockAcquisitionException) ReplicationServiceException(org.alfresco.service.cmr.replication.ReplicationServiceException) DisabledReplicationJobException(org.alfresco.service.cmr.replication.DisabledReplicationJobException) TransferDefinition(org.alfresco.service.cmr.transfer.TransferDefinition) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ReplicationDefinition(org.alfresco.service.cmr.replication.ReplicationDefinition) ActionCancelledException(org.alfresco.repo.action.ActionCancelledException) TransferEventError(org.alfresco.service.cmr.transfer.TransferEventError) TransferFailureException(org.alfresco.service.cmr.transfer.TransferFailureException)

Aggregations

DisabledReplicationJobException (org.alfresco.service.cmr.replication.DisabledReplicationJobException)2 ReplicationDefinition (org.alfresco.service.cmr.replication.ReplicationDefinition)2 ReplicationServiceException (org.alfresco.service.cmr.replication.ReplicationServiceException)2 Date (java.util.Date)1 UserTransaction (javax.transaction.UserTransaction)1 ActionCancelledException (org.alfresco.repo.action.ActionCancelledException)1 ActionImpl (org.alfresco.repo.action.ActionImpl)1 RuntimeActionService (org.alfresco.repo.action.RuntimeActionService)1 LockAcquisitionException (org.alfresco.repo.lock.LockAcquisitionException)1 ScriptReplicationDefinition (org.alfresco.repo.replication.script.ScriptReplicationDefinition)1 NodeRef (org.alfresco.service.cmr.repository.NodeRef)1 TransferDefinition (org.alfresco.service.cmr.transfer.TransferDefinition)1 TransferEndEvent (org.alfresco.service.cmr.transfer.TransferEndEvent)1 TransferEventCancelled (org.alfresco.service.cmr.transfer.TransferEventCancelled)1 TransferEventError (org.alfresco.service.cmr.transfer.TransferEventError)1 TransferFailureException (org.alfresco.service.cmr.transfer.TransferFailureException)1