Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Migrate the Asynchronous Rest service to the new task framework.
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos A. Munoz committed Aug 20, 2013
1 parent 7554ca8 commit f323076
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 130 deletions.
Expand Up @@ -20,7 +20,9 @@
*/
package org.zanata.rest.service;

import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
Expand All @@ -33,18 +35,16 @@
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Transactional;
import org.zanata.async.AsyncHandle;
import org.zanata.async.SimpleAsyncTask;
import org.zanata.common.EntityStatus;
import org.zanata.common.LocaleId;
import org.zanata.common.MergeType;
import org.zanata.dao.DocumentDAO;
import org.zanata.dao.ProjectIterationDAO;
import org.zanata.lock.LockNotAcquiredException;
import org.zanata.model.HDocument;
import org.zanata.model.HProject;
import org.zanata.model.HProjectIteration;
import org.zanata.process.MessagesProcessHandle;
import org.zanata.process.ProcessHandle;
import org.zanata.process.RunnableProcess;
import org.zanata.rest.NoSuchEntityException;
import org.zanata.rest.ReadOnlyEntityException;
import org.zanata.rest.dto.ProcessStatus;
Expand All @@ -53,10 +53,11 @@
import org.zanata.security.ZanataIdentity;
import org.zanata.service.DocumentService;
import org.zanata.service.LocaleService;
import org.zanata.service.ProcessManagerService;
import org.zanata.service.TranslationService;
import org.zanata.service.impl.AsyncTaskManagerServiceImpl;
import org.zanata.service.impl.DocumentServiceImpl;
import org.zanata.service.impl.TranslationServiceImpl;
import com.google.common.collect.Lists;

import lombok.extern.slf4j.Slf4j;

Expand All @@ -75,7 +76,7 @@
public class AsynchronousProcessResourceService implements AsynchronousProcessResource
{
@In
private ProcessManagerService processManagerServiceImpl;
private AsyncTaskManagerServiceImpl asyncTaskManagerServiceImpl;

@In
private LocaleService localeServiceImpl;
Expand All @@ -101,7 +102,6 @@ public ProcessStatus startSourceDocCreation(final @PathParam("id") String idNoSl
final @QueryParam("ext") Set<String> extensions,
final @QueryParam("copyTrans") @DefaultValue("true") boolean copytrans)
{
ProcessHandle handle = new ProcessHandle();
String errorMessage = null;

HProjectIteration hProjectIteration = retrieveAndCheckIteration(projectSlug, iterationSlug, true);
Expand All @@ -122,24 +122,23 @@ public ProcessStatus startSourceDocCreation(final @PathParam("id") String idNoSl

if( errorMessage == null )
{
RunnableProcess<ProcessHandle> process =
new RunnableProcess<ProcessHandle>()
{
@Override
protected void run(ProcessHandle handle) throws Throwable
SimpleAsyncTask<Void> task =
new SimpleAsyncTask<Void>()
{
DocumentService documentServiceImpl =
(DocumentService)Component.getInstance(DocumentServiceImpl.class);
documentServiceImpl.saveDocument(
projectSlug, iterationSlug, resource, extensions, copytrans, true);
handle.setCurrentProgress( handle.getMaxProgress() ); // TODO This should update with real progress
}
}.withIdentity(identity);

processManagerServiceImpl.startProcess(process,handle);
@Override
public Void call() throws Exception
{
DocumentService documentServiceImpl =
(DocumentService)Component.getInstance(DocumentServiceImpl.class);
documentServiceImpl.saveDocument(
projectSlug, iterationSlug, resource, extensions, copytrans, true);
getHandle().setCurrentProgress(getHandle().getMaxProgress()); // TODO This should update with real progress
return null;
}
};

//response.setStatus(Response.Status.ACCEPTED.getStatusCode());
return this.getProcessStatus(handle.getId());
String taskId = asyncTaskManagerServiceImpl.startTask(task);
return getProcessStatus(taskId); // TODO Change to return 202 Accepted, with a url to get the progress
}
else
{
Expand All @@ -158,58 +157,28 @@ public ProcessStatus startSourceDocCreationOrUpdate(final @PathParam("id") Strin
final @QueryParam("ext") Set<String> extensions,
final @QueryParam("copyTrans") @DefaultValue("true") boolean copytrans)
{
ProcessHandle handle = new ProcessHandle();
String errorMessage = null;

HProjectIteration hProjectIteration = retrieveAndCheckIteration(projectSlug, iterationSlug, true);

resourceUtils.validateExtensions(extensions); //gettext, comment

if( errorMessage == null )
{
RunnableProcess<ProcessHandle> process =
new RunnableProcess<ProcessHandle>()
SimpleAsyncTask<Void> task =
new SimpleAsyncTask<Void>()
{
@Override
public Void call() throws Exception
{
@Override
protected void prepare(ProcessHandle handle)
{
handle.setMaxProgress(resource.getTextFlows().size());
}

@Override
protected void run(ProcessHandle handle) throws Throwable
{
DocumentService documentServiceImpl =
(DocumentService)Component.getInstance(DocumentServiceImpl.class);
documentServiceImpl.saveDocument(
projectSlug, iterationSlug, resource, extensions, copytrans, true);
handle.setCurrentProgress( handle.getMaxProgress() ); // TODO This should update with real progress
}

@Override
protected void handleThrowable(ProcessHandle handle, Throwable t)
{
// Ignore Lock exceptions
if( !(t instanceof LockNotAcquiredException) )
{
AsynchronousProcessResourceService.log.error("Error pushing source document", t);
}
super.handleThrowable(handle, t);
}
}.withIdentity(identity);

processManagerServiceImpl.startProcess(process,handle);
DocumentService documentServiceImpl =
(DocumentService)Component.getInstance(DocumentServiceImpl.class);
documentServiceImpl.saveDocument(
projectSlug, iterationSlug, resource, extensions, copytrans, true);
getHandle().setCurrentProgress( getHandle().getMaxProgress() ); // TODO This should update with real progress
return null;
}
};

//response.setStatus(Response.Status.ACCEPTED.getStatusCode());
return this.getProcessStatus(handle.getId());
}
else
{
ProcessStatus status = new ProcessStatus();
status.setStatusCode(ProcessStatusCode.Failed);
status.getMessages().add(errorMessage);
return status;
}
String taskId = asyncTaskManagerServiceImpl.startTask(task);
return this.getProcessStatus(taskId); // TODO Change to return 202 Accepted, with a url to get the progress
}

@Override
Expand All @@ -226,8 +195,6 @@ public ProcessStatus startTranslatedDocCreationOrUpdate(final @PathParam("id") S
this.getSecuredIteration(projectSlug, iterationSlug).getProject());

String errorMessage = null;
MessagesProcessHandle handle = new MessagesProcessHandle();

MergeType mergeType = null;
try
{
Expand All @@ -240,46 +207,28 @@ public ProcessStatus startTranslatedDocCreationOrUpdate(final @PathParam("id") S

final String id = URIHelper.convertFromDocumentURIId(idNoSlash);
final MergeType finalMergeType = mergeType;
final String userName = identity.getCredentials().getUsername();
HProjectIteration hProjectIteration = projectIterationDAO.getBySlug(projectSlug, iterationSlug);

if( errorMessage == null )
{
RunnableProcess<MessagesProcessHandle> process =
new RunnableProcess<MessagesProcessHandle>()
{
@Override
protected void prepare(MessagesProcessHandle handle)
{
handle.setMaxProgress( translatedDoc.getTextFlowTargets().size() );
}

@Override
protected void run(MessagesProcessHandle handle) throws Throwable
{
TranslationService translationServiceImpl =
(TranslationService)Component.getInstance(TranslationServiceImpl.class);

// Translate
translationServiceImpl.translateAllInDoc(projectSlug, iterationSlug, id, locale, translatedDoc,
extensions, finalMergeType, true);
}
SimpleAsyncTask<List<String>> task = new SimpleAsyncTask<List<String>>()
{
@Override
public List<String> call() throws Exception
{
TranslationService translationServiceImpl =
(TranslationService)Component.getInstance(TranslationServiceImpl.class);

@Override
protected void handleThrowable(MessagesProcessHandle handle, Throwable t)
{
// Ignore Lock exceptions
if( !(t instanceof LockNotAcquiredException) )
{
AsynchronousProcessResourceService.log.error("Error pushing translations", t);
}
super.handleThrowable(handle, t);
}
}.withIdentity(identity);
// Translate
List<String> messages =
translationServiceImpl.translateAllInDoc(projectSlug, iterationSlug, id, locale, translatedDoc,
extensions, finalMergeType, true);

processManagerServiceImpl.startProcess(process,handle);
return messages;
}
};

return this.getProcessStatus(handle.getId());
String taskId = asyncTaskManagerServiceImpl.startTask(task);
return this.getProcessStatus(taskId);
}
else
{
Expand All @@ -293,41 +242,47 @@ protected void handleThrowable(MessagesProcessHandle handle, Throwable t)
@Override
public ProcessStatus getProcessStatus(@PathParam("processId") String processId)
{
ProcessHandle handle = processManagerServiceImpl.getProcessHandle(processId);
AsyncHandle handle = asyncTaskManagerServiceImpl.getHandle(processId);

if( handle == null )
{
throw new NotFoundException("A process was not found for id " + processId);
}

ProcessStatus status = new ProcessStatus();
status.setStatusCode(handle.isInProgress() ? ProcessStatusCode.Running : ProcessStatusCode.Finished);
status.setStatusCode(handle.isDone() ? ProcessStatusCode.Finished : ProcessStatusCode.Running);
int perComplete = 100;
if(handle.getMaxProgress() > 0)
{
perComplete = (handle.getCurrentProgress() * 100 / handle.getMaxProgress());
}
status.setPercentageComplete(perComplete);
status.setUrl("" + processId);
if( handle.getError() != null )

if( handle.isDone() )
{
// Lock Exception, tell the client to keep waiting
if( handle.getError() instanceof LockNotAcquiredException )
Object result = null;
try
{
status.getMessages().add("Waiting to acquire lock.");
status.setStatusCode(ProcessStatusCode.NotAccepted);
result = handle.get();
}
else
catch (InterruptedException e)
{
status.setStatusCode(ProcessStatusCode.Failed);
status.getMessages().add(handle.getError().getMessage());
// The process was forcefully cancelled
status.setMessages(Lists.newArrayList(e.getMessage()));
}
catch (ExecutionException e)
{
// Exception thrown while running the task
status.setMessages(Lists.newArrayList(e.getCause().getMessage()));
}
}

if( handle instanceof MessagesProcessHandle )
{
MessagesProcessHandle messagesProcessHandle = (MessagesProcessHandle)handle;
status.getMessages().addAll( messagesProcessHandle.getMessages() );
// TODO Need to find a generic way of returning all object types. Since the only current
// scenario involves lists of strings, hardcoding to that
if( result != null && result instanceof List )
{
status.getMessages().addAll( (List)result );
}
}

return status;
Expand Down
Expand Up @@ -84,7 +84,7 @@ public interface TranslationService
* and locale.
* @see TranslationService#translateAllInDoc(String, String, String, org.zanata.common.LocaleId, org.zanata.rest.dto.resource.TranslationsResource, java.util.Set, org.zanata.common.MergeType)
*/
public void translateAllInDoc(String projectSlug, String iterationSlug, String docId, LocaleId locale,
public List<String> translateAllInDoc(String projectSlug, String iterationSlug, String docId, LocaleId locale,
TranslationsResource translations, Set<String> extensions, MergeType mergeType,
boolean lock);

Expand Down

0 comments on commit f323076

Please sign in to comment.