BatchJob - BatchJob still running when is server stopped
Asked Answered
A

2

1

When the BJ is currently RUNNING and if server is STOPPED, the BATCH JOB status in Spring Batch Admin still shows its RUNNING even after the server is stopped, it needs to be FAILED

Please help on this? Do we need to handle it manually or we can achieve it out of the box. Need help on this?

Anishaaniso answered 27/6, 2013 at 7:19 Comment(0)
G
1

the job repository (database implementation) reflects the last 'known' state. so, in the case where the job was running and the JVM crashed, then it will never be updated in the database.

in the event of the database being 'out-of-sync' with the JVM, then the process needs to be a manual one, there doesn't appear to be an out of the box solution for it. the simplest solution would be to execute a script on startup that checked the batch tables for any RUNNING jobs and then 'failed' them.

update batch_job_execution set STATUS = 'FAILED', EXIT_CODE = 'FAILED', EXIT_MESSAGE = 'FORCED UPDATE' where job_execution_id in (select job_execution_id from batch_job_execution where status = 'RUNNING');

one thing you will want to consider in this situation is if the JobRepository tables, and the jobs associated with them, are shared with another JVM. in this case, you may wish to do a pass that also evaluates if the job is still running beyond the maximum runtime of any history it has. (a subselect with max() end_time - create_time for the same job_name)

Gamez answered 27/6, 2013 at 18:34 Comment(0)
H
0

Update batch tables with FAILED status to move ahead

-- batch_step_execution

UPDATE batch_step_execution
SET
    end_time = sysdate,
    last_updated = sysdate,
    status = 'COMPLETED',
    exit_code = 'FAILED'
WHERE
    step_execution_id IN (
        SELECT
            step_execution_id
        FROM
            batch_step_execution bse
        WHERE
                1 = 1
            AND status NOT IN ( 'COMPLETED', 'FAILED' )
            OR exit_code NOT IN ( 'COMPLETED', 'FAILED' )
    );

-- batch_job_execution

UPDATE batch_job_execution
SET
    end_time = sysdate,
    last_updated = sysdate,
    status = 'COMPLETED',
    exit_code = 'FAILED'
WHERE
    job_execution_id IN (
        SELECT
            bje.job_execution_id
        FROM
            batch_job_execution bje
        WHERE
                1 = 1
            AND ( status NOT IN ( 'COMPLETED', 'FAILED' ) )
            OR ( exit_code NOT IN ( 'COMPLETED', 'FAILED' ) )
    );
Hostler answered 27/3, 2023 at 13:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.