-
Notifications
You must be signed in to change notification settings - Fork 61
Work towards automated cleaning of JcrJobRepository #107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
grabbit/src/main/groovy/com/twcable/grabbit/resources/CleanJobRepositoryResource.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| * Copyright 2015 Time Warner Cable, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.twcable.grabbit.resources | ||
|
|
||
| import groovy.transform.CompileStatic | ||
| import org.apache.sling.api.resource.ResourceResolver | ||
| import org.apache.sling.api.resource.SyntheticResource | ||
|
|
||
| import javax.annotation.Nonnull | ||
|
|
||
| /** | ||
| * A resource representing what needs to be deleted from Grabbit's JobRepository. | ||
| * provided by {@link GrabbitResourceProvider}. | ||
| * Queried from {@link com.twcable.grabbit.spring.batch.repository.servlets.GrabbitCleanJobRepositoryServlet}. | ||
| */ | ||
| @CompileStatic | ||
| class CleanJobRepositoryResource extends SyntheticResource { | ||
|
|
||
| public static final String CLEAN_JOBREPOSITORY_RESOURCE_TYPE = "twcable:grabbit/jobrepository/clean" | ||
|
|
||
| CleanJobRepositoryResource(@Nonnull final ResourceResolver resourceResolver, @Nonnull final String resolutionPath) { | ||
| super(resourceResolver, resolutionPath, CLEAN_JOBREPOSITORY_RESOURCE_TYPE) | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| String getResourceType() { | ||
| return CLEAN_JOBREPOSITORY_RESOURCE_TYPE | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...main/groovy/com/twcable/grabbit/spring/batch/repository/GrabbitExecutionContextDao.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /* | ||
| * Copyright 2015 Time Warner Cable, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.twcable.grabbit.spring.batch.repository | ||
|
|
||
| import org.springframework.batch.core.repository.dao.ExecutionContextDao | ||
| import org.springframework.batch.item.ExecutionContext; | ||
|
|
||
|
|
||
| /** | ||
| * Modified DAO Interface for persisting and retrieving {@link ExecutionContext} | ||
| * @see ExecutionContextDao for more details | ||
| */ | ||
| interface GrabbitExecutionContextDao extends ExecutionContextDao { | ||
|
|
||
| /** | ||
| * Returns job execution context paths by comparing "executionId" property on "executionContext/job/<id>" with | ||
| * "executionId" property on JobExecutions for the @param jobExecutionResourcePaths | ||
| */ | ||
| public Collection<String> getJobExecutionContextPaths(Collection<String> jobExecutionResourcePaths) | ||
|
|
||
| /** | ||
| * Returns step execution context paths by comparing "executionId" property on "executionContext/job/<id>" with | ||
| * "id" property on StepExecutions for the @param stepExecutionResourcePaths | ||
| */ | ||
| public Collection<String> getStepExecutionContextPaths(Collection<String> stepExecutionResourcePaths) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...src/main/groovy/com/twcable/grabbit/spring/batch/repository/GrabbitJobExecutionDao.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| * Copyright 2015 Time Warner Cable, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.twcable.grabbit.spring.batch.repository | ||
|
|
||
| import org.springframework.batch.core.BatchStatus | ||
| import org.springframework.batch.core.JobExecution | ||
| import org.springframework.batch.core.repository.dao.JobExecutionDao | ||
|
|
||
| /** | ||
| * Modified DAO Interface for persisting and retrieving {@link JobExecution} | ||
| * @see JobExecutionDao for more details | ||
| */ | ||
| interface GrabbitJobExecutionDao extends JobExecutionDao{ | ||
|
|
||
| /** | ||
| * Returns job execution paths for given BatchStatuses | ||
| */ | ||
| public Collection<String> getJobExecutions(Collection<BatchStatus> batchStatuses) | ||
|
|
||
| /** | ||
| * Returns job execution paths which ended @param hours ago from "Now" | ||
| */ | ||
| public Collection<String> getJobExecutions(int hours, Collection<String> jobExecutionPaths) | ||
|
|
||
| } |
34 changes: 34 additions & 0 deletions
34
.../src/main/groovy/com/twcable/grabbit/spring/batch/repository/GrabbitJobInstanceDao.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /* | ||
| * Copyright 2015 Time Warner Cable, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.twcable.grabbit.spring.batch.repository | ||
|
|
||
| import org.springframework.batch.core.JobInstance | ||
| import org.springframework.batch.core.repository.dao.JobInstanceDao | ||
|
|
||
| /** | ||
| * Modified DAO Interface for persisting and retrieving {@link JobInstance} | ||
| * @see JobInstanceDao for more details | ||
| */ | ||
| interface GrabbitJobInstanceDao extends JobInstanceDao { | ||
|
|
||
| /** | ||
| * Returns job instance paths by comparing "id" property on "jobInstances" with | ||
| * "instanceId" property on JobExecutions for the @param jobExecutionResourcePaths | ||
| */ | ||
| public Collection<String> getJobInstancePaths(Collection<String> jobExecutionResourcePaths) | ||
|
|
||
| } |
33 changes: 33 additions & 0 deletions
33
...rc/main/groovy/com/twcable/grabbit/spring/batch/repository/GrabbitStepExecutionDao.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* | ||
| * Copyright 2015 Time Warner Cable, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.twcable.grabbit.spring.batch.repository | ||
|
|
||
| import org.springframework.batch.core.StepExecution | ||
| import org.springframework.batch.core.repository.dao.StepExecutionDao | ||
|
|
||
| /** | ||
| * Modified DAO Interface for persisting and retrieving {@link StepExecution} | ||
| * @see StepExecutionDao for more details | ||
| */ | ||
| interface GrabbitStepExecutionDao extends StepExecutionDao { | ||
|
|
||
| /** | ||
| * Returns step execution paths by comparing "jobExecutionId" property on "stepExecutions with | ||
| * "executionId" property on JobExecutions for the @param jobExecutionResourcePaths | ||
| */ | ||
| public Collection<String> getStepExecutionPaths(Collection<String> jobExecutionResourcePaths) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,11 +20,8 @@ import com.twcable.grabbit.jcr.JcrUtil | |
| import com.twcable.grabbit.util.CryptoUtil | ||
| import groovy.transform.CompileStatic | ||
| import groovy.util.logging.Slf4j | ||
| import org.apache.sling.api.resource.ModifiableValueMap | ||
| import org.apache.sling.api.resource.Resource | ||
| import org.apache.sling.api.resource.ResourceResolver | ||
| import org.apache.sling.api.resource.ResourceResolverFactory | ||
| import org.apache.sling.api.resource.ValueMap | ||
| import org.apache.sling.api.SlingException | ||
| import org.apache.sling.api.resource.* | ||
| import org.springframework.batch.core.JobExecution | ||
| import org.springframework.batch.core.StepExecution | ||
| import org.springframework.batch.core.repository.ExecutionContextSerializer | ||
|
|
@@ -33,6 +30,7 @@ import org.springframework.batch.item.ExecutionContext | |
|
|
||
| import javax.annotation.Nonnull | ||
|
|
||
| import static JcrGrabbitStepExecutionDao.ID | ||
| import static org.apache.jackrabbit.JcrConstants.NT_UNSTRUCTURED | ||
| import static org.apache.sling.api.resource.ResourceUtil.getOrCreateResource | ||
|
|
||
|
|
@@ -42,7 +40,7 @@ import static org.apache.sling.api.resource.ResourceUtil.getOrCreateResource | |
| */ | ||
| @CompileStatic | ||
| @Slf4j | ||
| class JcrExecutionContextDao extends AbstractJcrDao implements ExecutionContextDao { | ||
| class JcrGrabbitExecutionContextDao extends AbstractJcrDao implements GrabbitExecutionContextDao { | ||
|
|
||
| public static final String EXECUTION_CONTEXT_ROOT = "${ROOT_RESOURCE_NAME}/executionContexts" | ||
| public static final String JOB_EXECUTION_CONTEXT_ROOT = "${EXECUTION_CONTEXT_ROOT}/job" | ||
|
|
@@ -55,7 +53,7 @@ class JcrExecutionContextDao extends AbstractJcrDao implements ExecutionContextD | |
| private ExecutionContextSerializer contextSerializer | ||
|
|
||
|
|
||
| JcrExecutionContextDao( | ||
| JcrGrabbitExecutionContextDao( | ||
| @Nonnull final ResourceResolverFactory rrf, @Nonnull final ExecutionContextSerializer serializer) { | ||
| this.resourceResolverFactory = rrf | ||
| this.contextSerializer = serializer | ||
|
|
@@ -156,7 +154,7 @@ class JcrExecutionContextDao extends AbstractJcrDao implements ExecutionContextD | |
| } | ||
|
|
||
| /** | ||
| * Must be called when a new instance of JcrExecutionContextDao is created. | ||
| * Must be called when a new instance of JcrGrabbitExecutionContextDao is created. | ||
| * Ensures that {@link #STEP_EXECUTION_CONTEXT_ROOT} and {@link #JOB_EXECUTION_CONTEXT_ROOT} exist on initialization | ||
| */ | ||
| @Override | ||
|
|
@@ -306,4 +304,55 @@ class JcrExecutionContextDao extends AbstractJcrDao implements ExecutionContextD | |
| contextAsMap.each { key, value -> context.put(key, value) } | ||
| context | ||
| } | ||
|
|
||
| @Override | ||
| Collection<String> getJobExecutionContextPaths(Collection<String> jobExecutionResourcePaths) { | ||
| JcrUtil.manageResourceResolver(resourceResolverFactory) { ResourceResolver resolver -> | ||
| Collection<String> jobExecutionContextPathsToRemove = [] | ||
| jobExecutionResourcePaths.each { String jobExecutionResourcePath -> | ||
| Resource jobExecutionResource = resolver.getResource(jobExecutionResourcePath) | ||
| ValueMap props = jobExecutionResource.adaptTo(ValueMap) | ||
| Long jobExecutionId = props[JcrGrabbitJobExecutionDao.EXECUTION_ID] as Long | ||
| String query = "select * from [nt:unstructured] as s " + | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/s/resource/ ? |
||
| "where ISDESCENDANTNODE(s,'${JOB_EXECUTION_CONTEXT_ROOT}') AND ( s.${EXECUTION_ID} = ${jobExecutionId})" | ||
| try { | ||
| List<String> jobExecutionContextPaths = resolver.findResources(query, "JCR-SQL2").toList().collect { it.path } | ||
| jobExecutionContextPathsToRemove.addAll(jobExecutionContextPaths) | ||
| } | ||
| catch(SlingException | IllegalStateException e) { | ||
| log.error "Exception when executing Query: ${query}. \nException - ", e | ||
| } | ||
| } | ||
| //There are 2 versions of Resources returned back by findResources | ||
| //One for JcrNodeResource and one for SocialResourceWrapper | ||
| //Hence, duplicates need to be removed | ||
| return jobExecutionContextPathsToRemove.unique() as Collection<String> | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| Collection<String> getStepExecutionContextPaths(Collection<String> stepExecutionResourcePaths) { | ||
| JcrUtil.manageResourceResolver(resourceResolverFactory) { ResourceResolver resolver -> | ||
| Collection<String> stepExecutionContextPathsToRemove = [] | ||
| stepExecutionResourcePaths.each { String stepExecutionResourcePath -> | ||
| Resource stepExecutionResource = resolver.getResource(stepExecutionResourcePath) | ||
| ValueMap props = stepExecutionResource.adaptTo(ValueMap) | ||
| Long stepExecutionId = props[ID] as Long | ||
| String query = "select * from [nt:unstructured] as s " + | ||
| "where ISDESCENDANTNODE(s,'${STEP_EXECUTION_CONTEXT_ROOT}') AND ( s.${EXECUTION_ID} = ${stepExecutionId})" | ||
| try { | ||
| List<String> stepExecutionContextPaths = resolver.findResources(query, "JCR-SQL2").toList().collect { it.path } | ||
| stepExecutionContextPathsToRemove.addAll(stepExecutionContextPaths) | ||
| } catch (SlingException | IllegalStateException e) { | ||
| log.error "Exception when executing Query: ${query}. \nException - ", e | ||
| } | ||
| } | ||
| //There are 2 versions of Resources returned back by findResources | ||
| //One for JcrNodeResource and one for SocialResourceWrapper | ||
| //Hence, duplicates need to be removed | ||
| return stepExecutionContextPathsToRemove.unique() as Collection<String> | ||
|
|
||
| } | ||
|
|
||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed to something like this ... I liked the idea of having
Jcrsomewhere in the name indicating what is the implementation made for.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
!fixed i