Search in sources :

Example 1 with RunnableTask

use of io.cdap.cdap.api.service.worker.RunnableTask in project cdap by caskdata.

the class SystemAppTask method run.

@Override
public void run(RunnableTaskContext context) throws Exception {
    ArtifactId systemAppArtifactId = context.getArtifactId();
    if (systemAppArtifactId == null) {
        throw new IllegalArgumentException("Missing artifactId from the system app task request");
    }
    LOG.debug("Received system app task for artifact {}", systemAppArtifactId);
    Injector injector = createInjector(cConf);
    ArtifactRepository artifactRepository = injector.getInstance(ArtifactRepository.class);
    Impersonator impersonator = injector.getInstance(Impersonator.class);
    String systemAppNamespace = context.getNamespace();
    Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.from(systemAppNamespace), systemAppArtifactId.getName(), systemAppArtifactId.getVersion());
    ArtifactLocalizerClient localizerClient = injector.getInstance(ArtifactLocalizerClient.class);
    File artifactLocation = localizerClient.getUnpackedArtifactLocation(Artifacts.toProtoArtifactId(new NamespaceId(systemAppNamespace), systemAppArtifactId));
    EntityImpersonator classLoaderImpersonator = new EntityImpersonator(artifactId.toEntityId(), impersonator);
    try (CloseableClassLoader artifactClassLoader = artifactRepository.createArtifactClassLoader(new ArtifactDescriptor(artifactId.getNamespace().getId(), artifactId.toArtifactId(), Locations.toLocation(artifactLocation)), classLoaderImpersonator);
        SystemAppTaskContext systemAppTaskContext = buildTaskSystemAppContext(injector, systemAppNamespace, systemAppArtifactId, artifactClassLoader)) {
        RunnableTaskRequest taskRequest = context.getEmbeddedRequest();
        String taskClassName = taskRequest.getClassName();
        if (taskClassName == null) {
            LOG.debug("No system app task to execute");
            return;
        }
        LOG.debug("Requested to run system app task {}", taskClassName);
        Class<?> clazz = artifactClassLoader.loadClass(taskClassName);
        if (!(RunnableTask.class.isAssignableFrom(clazz))) {
            throw new ClassCastException(String.format("%s is not a RunnableTask", taskClassName));
        }
        LOG.debug("Launching system app task {}", taskClassName);
        RunnableTask runnableTask = (RunnableTask) injector.getInstance(clazz);
        RunnableTaskContext runnableTaskContext = new RunnableTaskContext(taskRequest.getParam().getSimpleParam(), null, null, null, systemAppTaskContext) {

            @Override
            public void writeResult(byte[] data) throws IOException {
                context.writeResult(data);
            }

            @Override
            public void setTerminateOnComplete(boolean terminate) {
                context.setTerminateOnComplete(terminate);
            }

            @Override
            public boolean isTerminateOnComplete() {
                return context.isTerminateOnComplete();
            }
        };
        runnableTask.run(runnableTaskContext);
        LOG.debug("System app task completed {}", taskClassName);
    }
}
Also used : ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) EntityImpersonator(io.cdap.cdap.security.impersonation.EntityImpersonator) SystemAppTaskContext(io.cdap.cdap.api.service.worker.SystemAppTaskContext) ArtifactRepository(io.cdap.cdap.internal.app.runtime.artifact.ArtifactRepository) CloseableClassLoader(io.cdap.cdap.api.artifact.CloseableClassLoader) Impersonator(io.cdap.cdap.security.impersonation.Impersonator) EntityImpersonator(io.cdap.cdap.security.impersonation.EntityImpersonator) ArtifactDescriptor(io.cdap.cdap.internal.app.runtime.artifact.ArtifactDescriptor) Injector(com.google.inject.Injector) RunnableTaskContext(io.cdap.cdap.api.service.worker.RunnableTaskContext) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Id(io.cdap.cdap.common.id.Id) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) RunnableTask(io.cdap.cdap.api.service.worker.RunnableTask) ArtifactLocalizerClient(io.cdap.cdap.internal.app.worker.sidecar.ArtifactLocalizerClient) File(java.io.File) RunnableTaskRequest(io.cdap.cdap.api.service.worker.RunnableTaskRequest)

Example 2 with RunnableTask

use of io.cdap.cdap.api.service.worker.RunnableTask in project cdap by caskdata.

the class RunnableTaskLauncher method launchRunnableTask.

public RunnableTaskContext launchRunnableTask(RunnableTaskRequest request) throws Exception {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    if (classLoader == null) {
        classLoader = getClass().getClassLoader();
    }
    Class<?> clazz = classLoader.loadClass(request.getClassName());
    Injector injector = Guice.createInjector(new RunnableTaskModule(cConf));
    Object obj = injector.getInstance(clazz);
    if (!(obj instanceof RunnableTask)) {
        throw new ClassCastException(String.format("%s is not a RunnableTask", request.getClassName()));
    }
    RunnableTask runnableTask = (RunnableTask) obj;
    RunnableTaskContext runnableTaskContext = RunnableTaskContext.getBuilder().withParam(request.getParam()).withArtifactId(request.getArtifactId()).withNamespace(request.getNamespace()).build();
    runnableTask.run(runnableTaskContext);
    return runnableTaskContext;
}
Also used : Injector(com.google.inject.Injector) RunnableTaskContext(io.cdap.cdap.api.service.worker.RunnableTaskContext) RunnableTask(io.cdap.cdap.api.service.worker.RunnableTask)

Aggregations

Injector (com.google.inject.Injector)2 RunnableTask (io.cdap.cdap.api.service.worker.RunnableTask)2 RunnableTaskContext (io.cdap.cdap.api.service.worker.RunnableTaskContext)2 ArtifactId (io.cdap.cdap.api.artifact.ArtifactId)1 CloseableClassLoader (io.cdap.cdap.api.artifact.CloseableClassLoader)1 RunnableTaskRequest (io.cdap.cdap.api.service.worker.RunnableTaskRequest)1 SystemAppTaskContext (io.cdap.cdap.api.service.worker.SystemAppTaskContext)1 Id (io.cdap.cdap.common.id.Id)1 ArtifactDescriptor (io.cdap.cdap.internal.app.runtime.artifact.ArtifactDescriptor)1 ArtifactRepository (io.cdap.cdap.internal.app.runtime.artifact.ArtifactRepository)1 ArtifactLocalizerClient (io.cdap.cdap.internal.app.worker.sidecar.ArtifactLocalizerClient)1 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)1 EntityImpersonator (io.cdap.cdap.security.impersonation.EntityImpersonator)1 Impersonator (io.cdap.cdap.security.impersonation.Impersonator)1 File (java.io.File)1