Project Scan Hook

Is there a hook present to fire actions on a project scan that identifies changes?

I have a module that is compiling some side-content in a gateway that depends on certain resources, and I dont really want to setup a custom thing to watch those resources, but ideally to just leverage a pre-existing hook that sees new resources.

Ideal state would be this:

  1. Project Scan Starts
  2. Project Scan identifies 3 new resources
  3. Project Scan does whatever it needs to do to the resources
  4. Project Scan finishes that, and fires said mystery OnProjectScanCompleteHook
  5. Said hook gives me a list of resources that were just identified, and I can go on about my business with them.

I have dug around in the javadocs and haven't found anything yet that could mirror this setup.

Not exactly like you're asking for, but you'll probably want to look at 3 things:

  1. ProjectManagerBase::addProjectListener and ::removeProjectListener (ProjectManagerBase)
  2. Project::addProjectResourceListener and ::removeProjectResourceListener (Project)
  3. ProjectLifecycle and ProjectLifecycleFactory, which are things you can use to not deal with the "raw" listeners and instead just get notified when resources you're interested in get created/updated/deleted. (ProjectLifecycle and ProjectLifecycleFactory)
1 Like

Here's a fairly simple example of what implementing a ProjectLifecycle and ProjectLifecycleFactory look like:

    private static final ResourceFilter PIPELINE_RESOURCE_FILTER = new ResourceFilter(
        ApplicationScope.GATEWAY,
        Collections.singletonList(PipelineDescriptor.RESOURCE_TYPE));

    private class PipelineLifecycleFactory extends ProjectLifecycleFactory<PipelineLifecycle> {

        PipelineLifecycleFactory(ProjectManager projectManager) {
            super(projectManager);
        }

        @Override
        public PipelineLifecycle createProjectLifecycle(RuntimeProject project) {
            return new PipelineLifecycle(project);
        }

        @Override
        protected ResourceFilter getResourceFilter() {
            return PIPELINE_RESOURCE_FILTER;
        }

        @Override
        public Predicate<RuntimeProject> getProjectFilter() {
            return project -> project.hasResourcesOfType(PipelineDescriptor.RESOURCE_TYPE);
        }

    }

    private class PipelineLifecycle extends ProjectLifecycle {

        PipelineLifecycle(RuntimeProject project) {
            super(project);
        }

        @Override
        protected void onStartup(List<ProjectResource> resources) {
            startPipelinesFromResources(resources);
        }

        @Override
        protected void onShutdown(List<ProjectResourceId> resourceIds) {
            retirePipelinesFromResources(resourceIds);
        }

        @Override
        protected void onResourcesCreated(List<ProjectResource> resources) {
            startPipelinesFromResources(resources);
        }

        @Override
        protected void onResourcesModified(List<ProjectResource> resources) {
            // Modifications to a pipeline are basically stop the old one and start a new one.
            // startPipelinesFromResources() will retire an existing pipeline before starting
            // an updated pipeline.
            startPipelinesFromResources(resources);
        }

        @Override
        protected void onResourcesDeleted(List<ProjectResourceId> resourceIds) {
            retirePipelinesFromResources(resourceIds);
        }

    }

Elsewhere the factory gets instantiated and startup() is called on it, then eventually shutdown().

You may just want the listeners if you don't care about the lifecycle of the project as well as the resources.

2 Likes

This is perfect!

Thanks, I will try it out.