From df75c2d6c3504be11ee7c72e9afb4413b93e79ce Mon Sep 17 00:00:00 2001 From: Jonathan Coustick Date: Wed, 6 Jan 2021 17:10:06 +0000 Subject: [PATCH 1/6] FISH-108 Add command to clean Jbatch repository --- .../payara/batch/CleanJbatchRepository.java | 150 ++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 appserver/batch/glassfish-batch-commands/src/main/java/fish/payara/batch/CleanJbatchRepository.java diff --git a/appserver/batch/glassfish-batch-commands/src/main/java/fish/payara/batch/CleanJbatchRepository.java b/appserver/batch/glassfish-batch-commands/src/main/java/fish/payara/batch/CleanJbatchRepository.java new file mode 100644 index 00000000000..af17c394fef --- /dev/null +++ b/appserver/batch/glassfish-batch-commands/src/main/java/fish/payara/batch/CleanJbatchRepository.java @@ -0,0 +1,150 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) [2021] Payara Foundation and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can + * obtain a copy of the License at + * https://github.com/payara/Payara/blob/master/LICENSE.txt + * See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at glassfish/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * The Payara Foundation designates this particular file as subject to the "Classpath" + * exception as provided by the Payara Foundation in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ +package fish.payara.batch; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.inject.Inject; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.sql.DataSource; +import org.glassfish.api.ActionReport; +import org.glassfish.api.Param; +import org.glassfish.api.admin.AdminCommand; +import org.glassfish.api.admin.AdminCommandContext; +import org.glassfish.api.admin.CommandLock; +import org.glassfish.api.admin.ExecuteOn; +import org.glassfish.api.admin.RuntimeType; +import org.glassfish.batch.spi.impl.BatchRuntimeHelper; +import org.glassfish.hk2.api.PerLookup; +import org.jvnet.hk2.annotations.Service; + +/** + * Cleans the records of jobs from the repository. + * + * @author jonathan coustick + * @since 5.25.0 + */ +@Service(name = "clean-jbatch-repository") +@PerLookup +@CommandLock(CommandLock.LockType.NONE) +@ExecuteOn(value = {RuntimeType.INSTANCE}) +public class CleanJbatchRepository implements AdminCommand { + + @Param(acceptableValues = "ALL,COMPLETED", defaultValue = "completed", optional=true) + private String type; + + @Param(optional = true) + int days; + + @Param(name = "jobname", primary = true, optional = false) + String jobname; + + @Inject + BatchRuntimeHelper batchRuntimeHelper; + + @Override + public void execute(AdminCommandContext context) { + ActionReport report = context.getActionReport(); + + try { + String dataSourceName = batchRuntimeHelper.getDataSourceLookupName(); + InitialContext ctx = new InitialContext(); + Object object = ctx.lookup(dataSourceName); + if (object instanceof DataSource) { + DataSource datasource = (DataSource) object; + try ( Connection conn = datasource.getConnection()) { + PreparedStatement deleteStatement = conn.prepareStatement("DELETE FROM stepstatus WHERE id IN (SELECT seid.stepexecid " + + "FROM jobinstancedata jid, executioninstancedata eid, stepexecutioninstancedata seid " + + "WHERE jid.jobinstanceid = eid.jobinstanceid AND eid.jobexecid = seid.jobexecid " + + "AND jid.name = ? AND eid.endtime < DATEADD('DAY',?, NOW()) AND eid.batchstatus = ?)"); + deleteStatement.setString(1, jobname); + deleteStatement.setInt(2, -days); + deleteStatement.setString(3, type); + deleteStatement.execute(); + + deleteStatement = conn.prepareStatement("DELETE FROM stepexecutioninstancedata WHERE jobexecid IN (SELECT eid.jobexecid " + + "FROM jobinstancedata jid, executioninstancedata eid WHERE jid.jobinstanceid = eid.jobinstanceid " + + "AND jid.name = ? AND eid.endtime < DATEADD('DAY',?, NOW()) AND eid.batchstatus = ?)"); + deleteStatement.setString(1, jobname); + deleteStatement.setInt(2, -days); + deleteStatement.setString(3, type); + deleteStatement.execute(); + + deleteStatement = conn.prepareStatement("DELETE FROM executioninstancedata WHERE jobinstanceid IN (SELECT jid.jobinstanceid " + + "FROM jobinstancedata jid, executioninstancedata eid WHERE jid.jobinstanceid = eid.jobinstanceid " + + "AND jid.name = ? AND eid.endtime < DATEADD('DAY',?, NOW()) AND eid.batchstatus = ?)"); + deleteStatement.setString(1, jobname); + deleteStatement.setInt(2, -days); + deleteStatement.setString(3, type); + deleteStatement.execute(); + + deleteStatement = conn.prepareStatement("DELETE FROM jobstatus " + + "WHERE id NOT IN (SELECT DISTINCT jobinstanceid FROM executioninstancedata)"); + deleteStatement.execute(); + + deleteStatement = conn.prepareStatement("DELETE FROM jobinstancedata " + + "WHERE jobinstanceid NOT IN (SELECT DISTINCT jobinstanceid FROM executioninstancedata)"); + deleteStatement.execute(); + + report.setActionExitCode(ActionReport.ExitCode.SUCCESS); + } catch (SQLException ex) { + Logger.getLogger("fish.payara.batch").log(Level.SEVERE, "Error cleaning repository", ex); + report.setActionExitCode(ActionReport.ExitCode.FAILURE); + report.setMessage("Error cleaning repository"); + report.setFailureCause(ex); + } + + } else { + report.setActionExitCode(ActionReport.ExitCode.FAILURE); + report.setMessage("Invalid data source type for JBatch"); + } + + } catch (NamingException ex) { + report.setActionExitCode(ActionReport.ExitCode.FAILURE); + report.setMessage("Unable to get data source for JBatch"); + report.setFailureCause(ex); + } + } + +} From 1ba631f526b0996532c7719d0813e4df78d4420a Mon Sep 17 00:00:00 2001 From: Jonathan Coustick Date: Wed, 6 Jan 2021 17:32:28 +0000 Subject: [PATCH 2/6] FISH-108 Added man page for clean-jbatch-repository and preexisting purge command --- .../payara/batch/CleanJbatchRepository.java | 8 +-- .../payara/batch/clean-jbatch-repository.1 | 64 +++++++++++++++++++ .../payara/batch/purge-jbatch-repository.1 | 38 +++++++++++ 3 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 appserver/batch/glassfish-batch-commands/src/main/manpages/fish/payara/batch/clean-jbatch-repository.1 create mode 100644 appserver/batch/glassfish-batch-commands/src/main/manpages/fish/payara/batch/purge-jbatch-repository.1 diff --git a/appserver/batch/glassfish-batch-commands/src/main/java/fish/payara/batch/CleanJbatchRepository.java b/appserver/batch/glassfish-batch-commands/src/main/java/fish/payara/batch/CleanJbatchRepository.java index af17c394fef..908b2827cbb 100644 --- a/appserver/batch/glassfish-batch-commands/src/main/java/fish/payara/batch/CleanJbatchRepository.java +++ b/appserver/batch/glassfish-batch-commands/src/main/java/fish/payara/batch/CleanJbatchRepository.java @@ -72,7 +72,7 @@ public class CleanJbatchRepository implements AdminCommand { @Param(acceptableValues = "ALL,COMPLETED", defaultValue = "completed", optional=true) - private String type; + String status; @Param(optional = true) int days; @@ -100,7 +100,7 @@ public void execute(AdminCommandContext context) { + "AND jid.name = ? AND eid.endtime < DATEADD('DAY',?, NOW()) AND eid.batchstatus = ?)"); deleteStatement.setString(1, jobname); deleteStatement.setInt(2, -days); - deleteStatement.setString(3, type); + deleteStatement.setString(3, status); deleteStatement.execute(); deleteStatement = conn.prepareStatement("DELETE FROM stepexecutioninstancedata WHERE jobexecid IN (SELECT eid.jobexecid " @@ -108,7 +108,7 @@ public void execute(AdminCommandContext context) { + "AND jid.name = ? AND eid.endtime < DATEADD('DAY',?, NOW()) AND eid.batchstatus = ?)"); deleteStatement.setString(1, jobname); deleteStatement.setInt(2, -days); - deleteStatement.setString(3, type); + deleteStatement.setString(3, status); deleteStatement.execute(); deleteStatement = conn.prepareStatement("DELETE FROM executioninstancedata WHERE jobinstanceid IN (SELECT jid.jobinstanceid " @@ -116,7 +116,7 @@ public void execute(AdminCommandContext context) { + "AND jid.name = ? AND eid.endtime < DATEADD('DAY',?, NOW()) AND eid.batchstatus = ?)"); deleteStatement.setString(1, jobname); deleteStatement.setInt(2, -days); - deleteStatement.setString(3, type); + deleteStatement.setString(3, status); deleteStatement.execute(); deleteStatement = conn.prepareStatement("DELETE FROM jobstatus " diff --git a/appserver/batch/glassfish-batch-commands/src/main/manpages/fish/payara/batch/clean-jbatch-repository.1 b/appserver/batch/glassfish-batch-commands/src/main/manpages/fish/payara/batch/clean-jbatch-repository.1 new file mode 100644 index 00000000000..a6f6e0888cf --- /dev/null +++ b/appserver/batch/glassfish-batch-commands/src/main/manpages/fish/payara/batch/clean-jbatch-repository.1 @@ -0,0 +1,64 @@ +clean-jbatch-repository(1) asadmin Utility Subcommands clean-jbatch-repository(1) + +NAME + clean-jbatch-repository - cleans up batch job execution records + +SYNOPSIS + clean-jbatch-repository [--help] + [--target target] + [--status={COMPLETED|ALL}] + [--days days] + [job_name] + +DESCRIPTION + The clean-jbatch-repository subcommand deletes records of job executions + +OPTIONS + --help, -? + Displays the help text for the subcommand. + + --long, -l + Displays detailed information about batch jobs. The default value + is false. + + --status + The status of batch jobs executions to which delete the records of. + This value is COMPLETED by default + + Possible values are as follows: + + ALL + All job executions will be deleted, regardless of status. + + COMPLETED + Only job executions that successfully completed will be deleted. + + --days + The number of days previous that the end time of the job execution must be to + be deleted. The default value is 0. + +OPERANDS + job_name + The name of the job for which to delete records of. + +EXAMPLES + Example 1, Deleting job execution records + This example deletes all job execution records that are over a week old for a job name batchprocess. + + asadmin> clean-jbatch-repository --status=ALL --days=7 batchprocess + Command clean-jbatch-repository executed successfully. + +EXIT STATUS + 0 + subcommand executed successfully + + 1 + error in executing the subcommand + +SEE ALSO + list-batch-job-executions(1), list-batch-job-steps(1), + purge-jbatch-repository + + asadmin(1M) + +Jakarta EE 8 6 Jan 2021 clean-jbatch-repository(1) diff --git a/appserver/batch/glassfish-batch-commands/src/main/manpages/fish/payara/batch/purge-jbatch-repository.1 b/appserver/batch/glassfish-batch-commands/src/main/manpages/fish/payara/batch/purge-jbatch-repository.1 new file mode 100644 index 00000000000..e5791471d8f --- /dev/null +++ b/appserver/batch/glassfish-batch-commands/src/main/manpages/fish/payara/batch/purge-jbatch-repository.1 @@ -0,0 +1,38 @@ +purge-jbatch-repository(1) asadmin Utility Subcommands purge-jbatch-repository(1) + +NAME + purge-jbatch-repository - lists batch jobs + +SYNOPSIS + purge-jbatch-repository [--help] + [apptag] + +DESCRIPTION + The purge-jbatch-repository subcommand deletes all job executions of a given apptag + +OPTIONS +OPERANDS + apptag + The apptag used to purge all records of from the database. + +EXAMPLES + Example 1, Deleting all job executions + This example deletes all job executions for an application named jbatchapp on the default server config. + + asadmin> purge-jbatch-repository server-config:jbatchapp + Command purge-jbatch-repository executed successfully. + +EXIT STATUS + 0 + subcommand executed successfully + + 1 + error in executing the subcommand + +SEE ALSO + list-batch-job-executions(1), list-batch-job-steps(1), + clean-jbatch-repository + + asadmin(1M) + +Jakarta EE 8 6 Jan 2021 purge-jbatch-repository(1) From c3beb029fcfbb3c415bfee6b88cd689426da8f53 Mon Sep 17 00:00:00 2001 From: Jonathan Coustick Date: Thu, 7 Jan 2021 12:40:57 +0000 Subject: [PATCH 3/6] FISH-108 Requested changes and bugfix --- .../fish/payara/batch/CleanJbatchRepository.java | 16 +++++++++------- .../fish/payara/batch/clean-jbatch-repository.1 | 6 +----- .../fish/payara/batch/purge-jbatch-repository.1 | 4 ++-- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/appserver/batch/glassfish-batch-commands/src/main/java/fish/payara/batch/CleanJbatchRepository.java b/appserver/batch/glassfish-batch-commands/src/main/java/fish/payara/batch/CleanJbatchRepository.java index 908b2827cbb..6ed225f98c4 100644 --- a/appserver/batch/glassfish-batch-commands/src/main/java/fish/payara/batch/CleanJbatchRepository.java +++ b/appserver/batch/glassfish-batch-commands/src/main/java/fish/payara/batch/CleanJbatchRepository.java @@ -60,7 +60,7 @@ import org.jvnet.hk2.annotations.Service; /** - * Cleans the records of jobs from the repository. + * Cleans the records of job executions from the repository. * * @author jonathan coustick * @since 5.25.0 @@ -71,7 +71,7 @@ @ExecuteOn(value = {RuntimeType.INSTANCE}) public class CleanJbatchRepository implements AdminCommand { - @Param(acceptableValues = "ALL,COMPLETED", defaultValue = "completed", optional=true) + @Param(acceptableValues = "ALL,COMPLETED", defaultValue = "COMPLETED", optional=true) String status; @Param(optional = true) @@ -97,26 +97,30 @@ public void execute(AdminCommandContext context) { PreparedStatement deleteStatement = conn.prepareStatement("DELETE FROM stepstatus WHERE id IN (SELECT seid.stepexecid " + "FROM jobinstancedata jid, executioninstancedata eid, stepexecutioninstancedata seid " + "WHERE jid.jobinstanceid = eid.jobinstanceid AND eid.jobexecid = seid.jobexecid " - + "AND jid.name = ? AND eid.endtime < DATEADD('DAY',?, NOW()) AND eid.batchstatus = ?)"); + + "AND jid.name = ? AND eid.endtime < DATEADD('DAY',?, NOW()) AND (eid.batchstatus = ? OR ? = 'ALL'))"); + deleteStatement.setString(1, jobname); deleteStatement.setInt(2, -days); deleteStatement.setString(3, status); + deleteStatement.setString(4, status); deleteStatement.execute(); deleteStatement = conn.prepareStatement("DELETE FROM stepexecutioninstancedata WHERE jobexecid IN (SELECT eid.jobexecid " + "FROM jobinstancedata jid, executioninstancedata eid WHERE jid.jobinstanceid = eid.jobinstanceid " - + "AND jid.name = ? AND eid.endtime < DATEADD('DAY',?, NOW()) AND eid.batchstatus = ?)"); + + "AND jid.name = ? AND eid.endtime < DATEADD('DAY',?, NOW()) AND (eid.batchstatus = ? OR ? = 'ALL'))"); deleteStatement.setString(1, jobname); deleteStatement.setInt(2, -days); deleteStatement.setString(3, status); + deleteStatement.setString(4, status); deleteStatement.execute(); deleteStatement = conn.prepareStatement("DELETE FROM executioninstancedata WHERE jobinstanceid IN (SELECT jid.jobinstanceid " + "FROM jobinstancedata jid, executioninstancedata eid WHERE jid.jobinstanceid = eid.jobinstanceid " - + "AND jid.name = ? AND eid.endtime < DATEADD('DAY',?, NOW()) AND eid.batchstatus = ?)"); + + "AND jid.name = ? AND eid.endtime < DATEADD('DAY',?, NOW()) AND (eid.batchstatus = ? OR ? = 'ALL'))"); deleteStatement.setString(1, jobname); deleteStatement.setInt(2, -days); deleteStatement.setString(3, status); + deleteStatement.setString(4, status); deleteStatement.execute(); deleteStatement = conn.prepareStatement("DELETE FROM jobstatus " @@ -126,8 +130,6 @@ public void execute(AdminCommandContext context) { deleteStatement = conn.prepareStatement("DELETE FROM jobinstancedata " + "WHERE jobinstanceid NOT IN (SELECT DISTINCT jobinstanceid FROM executioninstancedata)"); deleteStatement.execute(); - - report.setActionExitCode(ActionReport.ExitCode.SUCCESS); } catch (SQLException ex) { Logger.getLogger("fish.payara.batch").log(Level.SEVERE, "Error cleaning repository", ex); report.setActionExitCode(ActionReport.ExitCode.FAILURE); diff --git a/appserver/batch/glassfish-batch-commands/src/main/manpages/fish/payara/batch/clean-jbatch-repository.1 b/appserver/batch/glassfish-batch-commands/src/main/manpages/fish/payara/batch/clean-jbatch-repository.1 index a6f6e0888cf..3dd8bbdc7b2 100644 --- a/appserver/batch/glassfish-batch-commands/src/main/manpages/fish/payara/batch/clean-jbatch-repository.1 +++ b/appserver/batch/glassfish-batch-commands/src/main/manpages/fish/payara/batch/clean-jbatch-repository.1 @@ -17,10 +17,6 @@ OPTIONS --help, -? Displays the help text for the subcommand. - --long, -l - Displays detailed information about batch jobs. The default value - is false. - --status The status of batch jobs executions to which delete the records of. This value is COMPLETED by default @@ -61,4 +57,4 @@ SEE ALSO asadmin(1M) -Jakarta EE 8 6 Jan 2021 clean-jbatch-repository(1) +Jakarta EE 8 7 Jan 2021 clean-jbatch-repository(1) diff --git a/appserver/batch/glassfish-batch-commands/src/main/manpages/fish/payara/batch/purge-jbatch-repository.1 b/appserver/batch/glassfish-batch-commands/src/main/manpages/fish/payara/batch/purge-jbatch-repository.1 index e5791471d8f..6f1b374ddce 100644 --- a/appserver/batch/glassfish-batch-commands/src/main/manpages/fish/payara/batch/purge-jbatch-repository.1 +++ b/appserver/batch/glassfish-batch-commands/src/main/manpages/fish/payara/batch/purge-jbatch-repository.1 @@ -1,7 +1,7 @@ purge-jbatch-repository(1) asadmin Utility Subcommands purge-jbatch-repository(1) NAME - purge-jbatch-repository - lists batch jobs + purge-jbatch-repository - purges batch jobs SYNOPSIS purge-jbatch-repository [--help] @@ -35,4 +35,4 @@ SEE ALSO asadmin(1M) -Jakarta EE 8 6 Jan 2021 purge-jbatch-repository(1) +Jakarta EE 8 7 Jan 2021 purge-jbatch-repository(1) From bbe3a9dac6bdce7d383cf06686378823575a1d78 Mon Sep 17 00:00:00 2001 From: Jonathan Coustick Date: Tue, 12 Jan 2021 14:36:24 +0000 Subject: [PATCH 4/6] FISH-108 Use prefix and suffix for table commands --- .../payara/batch/CleanJbatchRepository.java | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/appserver/batch/glassfish-batch-commands/src/main/java/fish/payara/batch/CleanJbatchRepository.java b/appserver/batch/glassfish-batch-commands/src/main/java/fish/payara/batch/CleanJbatchRepository.java index 6ed225f98c4..65d2e26890e 100644 --- a/appserver/batch/glassfish-batch-commands/src/main/java/fish/payara/batch/CleanJbatchRepository.java +++ b/appserver/batch/glassfish-batch-commands/src/main/java/fish/payara/batch/CleanJbatchRepository.java @@ -55,6 +55,7 @@ import org.glassfish.api.admin.CommandLock; import org.glassfish.api.admin.ExecuteOn; import org.glassfish.api.admin.RuntimeType; +import org.glassfish.batch.spi.impl.BatchRuntimeConfiguration; import org.glassfish.batch.spi.impl.BatchRuntimeHelper; import org.glassfish.hk2.api.PerLookup; import org.jvnet.hk2.annotations.Service; @@ -82,11 +83,14 @@ public class CleanJbatchRepository implements AdminCommand { @Inject BatchRuntimeHelper batchRuntimeHelper; + + @Inject + BatchRuntimeConfiguration config; @Override public void execute(AdminCommandContext context) { ActionReport report = context.getActionReport(); - + try { String dataSourceName = batchRuntimeHelper.getDataSourceLookupName(); InitialContext ctx = new InitialContext(); @@ -94,7 +98,11 @@ public void execute(AdminCommandContext context) { if (object instanceof DataSource) { DataSource datasource = (DataSource) object; try ( Connection conn = datasource.getConnection()) { - PreparedStatement deleteStatement = conn.prepareStatement("DELETE FROM stepstatus WHERE id IN (SELECT seid.stepexecid " + String prefix = config.getTablePrefix(); + String suffix = config.getTableSuffix(); + + PreparedStatement deleteStatement = conn.prepareStatement("DELETE FROM " + prefix + "stepstatus" + suffix + + " WHERE id IN (SELECT seid.stepexecid " + "FROM jobinstancedata jid, executioninstancedata eid, stepexecutioninstancedata seid " + "WHERE jid.jobinstanceid = eid.jobinstanceid AND eid.jobexecid = seid.jobexecid " + "AND jid.name = ? AND eid.endtime < DATEADD('DAY',?, NOW()) AND (eid.batchstatus = ? OR ? = 'ALL'))"); @@ -105,7 +113,8 @@ public void execute(AdminCommandContext context) { deleteStatement.setString(4, status); deleteStatement.execute(); - deleteStatement = conn.prepareStatement("DELETE FROM stepexecutioninstancedata WHERE jobexecid IN (SELECT eid.jobexecid " + deleteStatement = conn.prepareStatement("DELETE FROM " + prefix + "stepexecutioninstancedata" + suffix + + " WHERE jobexecid IN (SELECT eid.jobexecid " + "FROM jobinstancedata jid, executioninstancedata eid WHERE jid.jobinstanceid = eid.jobinstanceid " + "AND jid.name = ? AND eid.endtime < DATEADD('DAY',?, NOW()) AND (eid.batchstatus = ? OR ? = 'ALL'))"); deleteStatement.setString(1, jobname); @@ -114,7 +123,8 @@ public void execute(AdminCommandContext context) { deleteStatement.setString(4, status); deleteStatement.execute(); - deleteStatement = conn.prepareStatement("DELETE FROM executioninstancedata WHERE jobinstanceid IN (SELECT jid.jobinstanceid " + deleteStatement = conn.prepareStatement("DELETE FROM " + prefix + "executioninstancedata" + suffix + + " WHERE jobinstanceid IN (SELECT jid.jobinstanceid " + "FROM jobinstancedata jid, executioninstancedata eid WHERE jid.jobinstanceid = eid.jobinstanceid " + "AND jid.name = ? AND eid.endtime < DATEADD('DAY',?, NOW()) AND (eid.batchstatus = ? OR ? = 'ALL'))"); deleteStatement.setString(1, jobname); @@ -123,12 +133,12 @@ public void execute(AdminCommandContext context) { deleteStatement.setString(4, status); deleteStatement.execute(); - deleteStatement = conn.prepareStatement("DELETE FROM jobstatus " - + "WHERE id NOT IN (SELECT DISTINCT jobinstanceid FROM executioninstancedata)"); + deleteStatement = conn.prepareStatement("DELETE FROM " + prefix + "jobstatus" + suffix + + " WHERE id NOT IN (SELECT DISTINCT jobinstanceid FROM executioninstancedata)"); deleteStatement.execute(); - deleteStatement = conn.prepareStatement("DELETE FROM jobinstancedata " - + "WHERE jobinstanceid NOT IN (SELECT DISTINCT jobinstanceid FROM executioninstancedata)"); + deleteStatement = conn.prepareStatement("DELETE FROM " + prefix + "jobinstancedata " + suffix + + " WHERE jobinstanceid NOT IN (SELECT DISTINCT jobinstanceid FROM executioninstancedata)"); deleteStatement.execute(); } catch (SQLException ex) { Logger.getLogger("fish.payara.batch").log(Level.SEVERE, "Error cleaning repository", ex); From 11de439867e39b07fe9176c58424f39817ee9480 Mon Sep 17 00:00:00 2001 From: Jonathan Coustick Date: Wed, 13 Jan 2021 16:35:10 +0000 Subject: [PATCH 5/6] FISH-108 Command now initialises tables if they don't already exist --- .../java/fish/payara/batch/CleanJbatchRepository.java | 10 +++++++--- .../fish/payara/batch/clean-jbatch-repository.1 | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/appserver/batch/glassfish-batch-commands/src/main/java/fish/payara/batch/CleanJbatchRepository.java b/appserver/batch/glassfish-batch-commands/src/main/java/fish/payara/batch/CleanJbatchRepository.java index 65d2e26890e..62a59db8826 100644 --- a/appserver/batch/glassfish-batch-commands/src/main/java/fish/payara/batch/CleanJbatchRepository.java +++ b/appserver/batch/glassfish-batch-commands/src/main/java/fish/payara/batch/CleanJbatchRepository.java @@ -44,6 +44,7 @@ import java.sql.SQLException; import java.util.logging.Level; import java.util.logging.Logger; +import javax.batch.runtime.BatchRuntime; import javax.inject.Inject; import javax.naming.InitialContext; import javax.naming.NamingException; @@ -72,7 +73,7 @@ @ExecuteOn(value = {RuntimeType.INSTANCE}) public class CleanJbatchRepository implements AdminCommand { - @Param(acceptableValues = "ALL,COMPLETED", defaultValue = "COMPLETED", optional=true) + @Param(acceptableValues = "ALL,COMPLETED", defaultValue = "COMPLETED", optional = true) String status; @Param(optional = true) @@ -86,18 +87,21 @@ public class CleanJbatchRepository implements AdminCommand { @Inject BatchRuntimeConfiguration config; - + @Override public void execute(AdminCommandContext context) { ActionReport report = context.getActionReport(); + //Initialises databases if they don't already exist, ignore result + BatchRuntime.getJobOperator(); + try { String dataSourceName = batchRuntimeHelper.getDataSourceLookupName(); InitialContext ctx = new InitialContext(); Object object = ctx.lookup(dataSourceName); if (object instanceof DataSource) { DataSource datasource = (DataSource) object; - try ( Connection conn = datasource.getConnection()) { + try (Connection conn = datasource.getConnection()) { String prefix = config.getTablePrefix(); String suffix = config.getTableSuffix(); diff --git a/appserver/batch/glassfish-batch-commands/src/main/manpages/fish/payara/batch/clean-jbatch-repository.1 b/appserver/batch/glassfish-batch-commands/src/main/manpages/fish/payara/batch/clean-jbatch-repository.1 index 3dd8bbdc7b2..cea335bb34f 100644 --- a/appserver/batch/glassfish-batch-commands/src/main/manpages/fish/payara/batch/clean-jbatch-repository.1 +++ b/appserver/batch/glassfish-batch-commands/src/main/manpages/fish/payara/batch/clean-jbatch-repository.1 @@ -39,7 +39,7 @@ OPERANDS EXAMPLES Example 1, Deleting job execution records - This example deletes all job execution records that are over a week old for a job name batchprocess. + This example deletes all job execution records that are over a week old for a job named batchprocess. asadmin> clean-jbatch-repository --status=ALL --days=7 batchprocess Command clean-jbatch-repository executed successfully. From 9c3680803cf3d15435f21fb764cde87003588821 Mon Sep 17 00:00:00 2001 From: Jonathan Coustick Date: Wed, 13 Jan 2021 17:08:29 +0000 Subject: [PATCH 6/6] FISH-108 Wrap getJobOperator in a try-catch block --- .../java/fish/payara/batch/CleanJbatchRepository.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/appserver/batch/glassfish-batch-commands/src/main/java/fish/payara/batch/CleanJbatchRepository.java b/appserver/batch/glassfish-batch-commands/src/main/java/fish/payara/batch/CleanJbatchRepository.java index 62a59db8826..f26516ab300 100644 --- a/appserver/batch/glassfish-batch-commands/src/main/java/fish/payara/batch/CleanJbatchRepository.java +++ b/appserver/batch/glassfish-batch-commands/src/main/java/fish/payara/batch/CleanJbatchRepository.java @@ -42,6 +42,7 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; +import java.util.ServiceConfigurationError; import java.util.logging.Level; import java.util.logging.Logger; import javax.batch.runtime.BatchRuntime; @@ -93,7 +94,14 @@ public void execute(AdminCommandContext context) { ActionReport report = context.getActionReport(); //Initialises databases if they don't already exist, ignore result - BatchRuntime.getJobOperator(); + try { + BatchRuntime.getJobOperator(); + } catch (ServiceConfigurationError error) { + report.setMessage("Could not get JobOperator. Check if the Batch DataSource is configured properly and Check if the Database is up and running"); + report.setFailureCause(error); + report.setActionExitCode(ActionReport.ExitCode.FAILURE); + return; + } try { String dataSourceName = batchRuntimeHelper.getDataSourceLookupName();