Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>fr.insalyon.creatis.moteur2</groupId>
<artifactId>workflow-agent</artifactId>
<version>0.2</version>
<version>1.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static void main(String[] args) {
String workflowId = args[2];
JobDatabase db = new JobDatabase(workflowsPath, workflowId);
Command command = null;
if (cmd.toLowerCase().equals("kill")) {
if (cmd.equalsIgnoreCase("kill")) {
logger.info("Received kill signal to '" + workflowId + "'");
command = new KillCommand(db);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

public class KillCommand implements Command {
Expand All @@ -15,24 +17,33 @@ public KillCommand(JobDatabase jobDB) {
}

public void run() {
List<String> jobIds = this.jobDB.getJobIds();

for (String id : jobIds) {
try {
logger.info("Killing job id '" + id + "'");
String exec = "dirac-wms-job-kill " + id;
Process process = Runtime.getRuntime().exec(exec);
process.waitFor();
if (process.exitValue() != 0) {
logger.error("Unable to kill job id '" + id + "'");
} else {
this.jobDB.updateStatus(id);
}
} catch (InterruptedException | IOException e) {
logger.error("Error killing job " + id, e);
}
Map<String,String> jobIds = this.jobDB.getJobsToKill();

for (String id : jobIds.keySet()) {
// kill running jobs, delete waiting and succesfully submitted
boolean mustDelete = ! jobIds.get(id).equalsIgnoreCase("RUNNING");
killJob(id, mustDelete);
}

this.jobDB.close();
}

public void killJob(String id, boolean mustDelete) {
try {
String command = mustDelete ? "delete" : "kill";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We agree that we only make this distinction so that we keep a maximum of traces for running jobs, right? From a functional point of view, we could call delete on all of them.

logger.info("Doing " + command + " on job id '" + id + "'");
String exec = "dirac-wms-job-" + command + " " + id;
Process process = Runtime.getRuntime().exec(exec);
process.waitFor();
if (process.exitValue() != 0) {
logger.error("Unable to " + command + " job id '" + id + "' with dirac");
} else if (mustDelete) {
this.jobDB.updateStatusToDeleted(id);
} else {
this.jobDB.updateStatusToCancelled(id);
}
} catch (InterruptedException | IOException e) {
logger.error("Error killing job " + id, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

public class JobDatabase {
Expand All @@ -22,27 +25,36 @@ public JobDatabase(String workflowsPath, String workflowId) {
this.connect();
}

public List<String> getJobIds() {
public Map<String,String> getJobsToKill() {
try {
PreparedStatement ps = this.connection.prepareStatement("SELECT id FROM Jobs WHERE status='SUCCESSFULLY_SUBMITTED' OR status='QUEUED' OR status='RUNNING'");
PreparedStatement ps = this.connection.prepareStatement("SELECT id, status FROM Jobs WHERE status='SUCCESSFULLY_SUBMITTED' OR status='QUEUED' OR status='RUNNING'");
ResultSet rs = ps.executeQuery();
ArrayList jobIds = new ArrayList();
Map<String,String> jobs = new HashMap<>();

while(rs.next()) {
jobIds.add(rs.getString("id"));
jobs.put(rs.getString("id"),rs.getString("status"));
}

return jobIds;
return jobs;
} catch (SQLException e) {
logger.error("Error getting job ids", e);
return null;
}
}

public void updateStatus(String jobId) {

public void updateStatusToCancelled(String jobId) {
updateStatus(jobId, "CANCELLED");
}

public void updateStatusToDeleted(String jobId) {
updateStatus(jobId, "DELETED");
}

public void updateStatus(String jobId, String status) {
try {
PreparedStatement ps = this.connection.prepareStatement("UPDATE Jobs SET status = ? WHERE id = ?");
ps.setString(1, "CANCELLED");
ps.setString(1, status);
ps.setString(2, jobId);
ps.executeUpdate();
ps.close();
Expand Down