Skip to content
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

One more chunk will be executed even if a job is stopped #4631

Open
uyw4687 opened this issue Jul 13, 2024 · 1 comment · May be fixed by #4632
Open

One more chunk will be executed even if a job is stopped #4631

uyw4687 opened this issue Jul 13, 2024 · 1 comment · May be fixed by #4632
Labels
status: waiting-for-reporter Issues for which we are waiting for feedback from the reporter

Comments

@uyw4687
Copy link

uyw4687 commented Jul 13, 2024

Please do a quick search on Github issues first, there might be already a duplicate issue for the one you are about to create.
If the bug is trivial, just go ahead and create the issue. Otherwise, please take a few moments and fill in the following sections:

Bug description
A clear and concise description of what the bug is about.
If a job is stopped in ChunkListener#afterChunk, termainateOnly is not set.

Environment
Please provide as many details as possible: Spring Batch version, Java version, which database you use if any, etc
I assume this happens to all environments.

Steps to reproduce
Steps to reproduce the issue.
JobOperator#stop in ChunkListener#afterChunk then it will still run the next chunk.

Expected behavior
A clear and concise description of what you expected to happen.
The next chunk shouldn't run.

Minimal Complete Reproducible example
Please provide a failing test or a minimal complete verifiable example that reproduces the issue.
Bug reports that are reproducible will take priority in resolution over reports that are not reproducible.

@uyw4687 uyw4687 added status: waiting-for-triage Issues that we did not analyse yet type: bug labels Jul 13, 2024
@uyw4687 uyw4687 linked a pull request Jul 13, 2024 that will close this issue
@fmbenhassine
Copy link
Contributor

Thank you for opening this issue. However, I think there is misuse of the APIs according to the description you gave:

Bug description: If a job is stopped in ChunkListener#afterChunk, termainateOnly is not set.
Steps to reproduce: JobOperator#stop in ChunkListener#afterChunk then it will still run the next chunk.

JobOperator#stop is designed to stop a job from the outside (a separate thread or process), while StepExecution#setTerminateOnly() is designed to stop a job from within the inside.

So if you want to stop a step after a given chunk, you need to set the terminateOnly flag on the step execution and not call JobOperator#stop. Here is a hypothetical example of a chunk listener that should stop the execution if the number of chunks exceeds a given limit:

import org.springframework.batch.core.ChunkListener;
import org.springframework.batch.core.launch.JobOperator;
import org.springframework.batch.core.scope.context.ChunkContext;

public class MyChunkListener implements ChunkListener {

    // private JobOperator jobOperator;
    private int maxChunks;

    // constructor omitted

    @Override
    public void afterChunk(ChunkContext context) {
        if (++maxChunks > 10) {
            // jobOperator.stop(context.getStepContext().getStepExecution().getJobExecutionId()); // this is incorrect 
            context.getStepContext().getStepExecution().setTerminateOnly();
        }
    }
}

Do you see the point?

@fmbenhassine fmbenhassine added status: waiting-for-reporter Issues for which we are waiting for feedback from the reporter and removed status: waiting-for-triage Issues that we did not analyse yet type: bug labels Feb 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-reporter Issues for which we are waiting for feedback from the reporter
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants