use of org.batfish.common.Task in project batfish by batfish.
the class Service method getTaskStatus.
@GET
@Path(BfConsts.SVC_GET_TASKSTATUS_RSC)
@Produces(MediaType.APPLICATION_JSON)
public JSONArray getTaskStatus(@QueryParam(BfConsts.SVC_TASKID_KEY) String taskId) {
_logger.infof("BFS:getTaskStatus %s\n", taskId);
try {
if (taskId == null || taskId.equals("")) {
return new JSONArray(Arrays.asList(BfConsts.SVC_FAILURE_KEY, "taskid not supplied"));
}
Task task = Driver.getTaskFromLog(taskId);
if (task == null) {
task = new Task(TaskStatus.Unknown);
}
String taskStr = BatfishObjectMapper.writePrettyString(task);
return new JSONArray(Arrays.asList(BfConsts.SVC_SUCCESS_KEY, taskStr));
} catch (Exception e) {
return new JSONArray(Arrays.asList(BfConsts.SVC_FAILURE_KEY, e.getMessage()));
}
}
use of org.batfish.common.Task in project batfish by batfish.
the class Service method killTask.
@GET
@Path(BfConsts.SVC_KILL_TASK_RSC)
@Produces(MediaType.APPLICATION_JSON)
public JSONArray killTask(@QueryParam(BfConsts.SVC_TASKID_KEY) String taskId) {
_logger.infof("BFS:killTask %s\n", taskId);
try {
if (Strings.isNullOrEmpty(taskId)) {
return new JSONArray(Arrays.asList(BfConsts.SVC_FAILURE_KEY, "taskid not supplied"));
}
Task task = Driver.killTask(taskId);
String taskStr = BatfishObjectMapper.writePrettyString(task);
return new JSONArray(Arrays.asList(BfConsts.SVC_SUCCESS_KEY, taskStr));
} catch (Exception e) {
return new JSONArray(Arrays.asList(BfConsts.SVC_FAILURE_KEY, e.getMessage()));
}
}
use of org.batfish.common.Task in project batfish by batfish.
the class WorkMgr method checkTask.
private void checkTask(QueuedWork work, String worker) {
_logger.infof("WM:CheckWork: Trying to check %s on %s\n", work, worker);
Task task = new Task(TaskStatus.UnreachableOrBadResponse);
Client client = null;
SpanContext queueWorkSpan = work.getWorkItem().getSourceSpan();
try (ActiveSpan checkTaskSpan = GlobalTracer.get().buildSpan("Checking Task Status").addReference(References.FOLLOWS_FROM, queueWorkSpan).startActive()) {
// avoid unused warning
assert checkTaskSpan != null;
client = CommonUtil.createHttpClientBuilder(_settings.getSslPoolDisable(), _settings.getSslPoolTrustAllCerts(), _settings.getSslPoolKeystoreFile(), _settings.getSslPoolKeystorePassword(), _settings.getSslPoolTruststoreFile(), _settings.getSslPoolTruststorePassword()).build();
String protocol = _settings.getSslPoolDisable() ? "http" : "https";
WebTarget webTarget = client.target(String.format("%s://%s%s/%s", protocol, worker, BfConsts.SVC_BASE_RSC, BfConsts.SVC_GET_TASKSTATUS_RSC)).queryParam(BfConsts.SVC_TASKID_KEY, UriComponent.encode(work.getId().toString(), UriComponent.Type.QUERY_PARAM_SPACE_ENCODED));
Response response = webTarget.request(MediaType.APPLICATION_JSON).get();
if (response.getStatus() != Response.Status.OK.getStatusCode()) {
_logger.errorf("WM:CheckTask: Got non-OK response %s\n", response.getStatus());
} else {
String sobj = response.readEntity(String.class);
JSONArray array = new JSONArray(sobj);
_logger.info(String.format("response: %s [%s] [%s]\n", array, array.get(0), array.get(1)));
if (!array.get(0).equals(BfConsts.SVC_SUCCESS_KEY)) {
_logger.error(String.format("got error while refreshing status: %s %s\n", array.get(0), array.get(1)));
} else {
String taskStr = array.get(1).toString();
task = BatfishObjectMapper.mapper().readValue(taskStr, Task.class);
if (task.getStatus() == null) {
_logger.error("did not see status key in json response\n");
}
}
}
} catch (ProcessingException e) {
String stackTrace = ExceptionUtils.getStackTrace(e);
_logger.error(String.format("unable to connect to %s: %s\n", worker, stackTrace));
} catch (Exception e) {
String stackTrace = ExceptionUtils.getStackTrace(e);
_logger.error(String.format("exception: %s\n", stackTrace));
} finally {
if (client != null) {
client.close();
}
}
if (work.getStatus() == WorkStatusCode.TERMINATEDBYUSER) {
return;
}
try {
_workQueueMgr.processTaskCheckResult(work, task);
} catch (Exception e) {
_logger.errorf("exception: %s\n", ExceptionUtils.getStackTrace(e));
}
// if the task ended, send a hint to the pool manager to look up worker status
if (task.getStatus().isTerminated()) {
Main.getPoolMgr().refreshWorkerStatus(worker);
}
}
use of org.batfish.common.Task in project batfish by batfish.
the class WorkMgr method killWork.
private boolean killWork(QueuedWork work, String worker) {
Client client = null;
boolean killed = false;
SpanContext queueWorkSpan = work.getWorkItem().getSourceSpan();
try (ActiveSpan killTaskSpan = GlobalTracer.get().buildSpan("Checking Task Status").addReference(References.FOLLOWS_FROM, queueWorkSpan).startActive()) {
// avoid unused warning
assert killTaskSpan != null;
client = CommonUtil.createHttpClientBuilder(_settings.getSslPoolDisable(), _settings.getSslPoolTrustAllCerts(), _settings.getSslPoolKeystoreFile(), _settings.getSslPoolKeystorePassword(), _settings.getSslPoolTruststoreFile(), _settings.getSslPoolTruststorePassword()).build();
String protocol = _settings.getSslPoolDisable() ? "http" : "https";
WebTarget webTarget = client.target(String.format("%s://%s%s/%s", protocol, worker, BfConsts.SVC_BASE_RSC, BfConsts.SVC_KILL_TASK_RSC)).queryParam(BfConsts.SVC_TASKID_KEY, UriComponent.encode(work.getId().toString(), UriComponent.Type.QUERY_PARAM_SPACE_ENCODED));
Response response = webTarget.request(MediaType.APPLICATION_JSON).get();
if (response.getStatus() != Response.Status.OK.getStatusCode()) {
_logger.errorf("WM:KillTask: Got non-OK response %s\n", response.getStatus());
} else {
try {
String sobj = response.readEntity(String.class);
JSONArray array = new JSONArray(sobj);
_logger.infof("response: %s [%s] [%s]\n", array, array.get(0), array.get(1));
if (!array.get(0).equals(BfConsts.SVC_SUCCESS_KEY)) {
_logger.errorf("Got error while killing task: %s %s\n", array.get(0), array.get(1));
} else {
Task task = BatfishObjectMapper.mapper().readValue(array.getString(1), Task.class);
_workQueueMgr.processTaskCheckResult(work, task);
killed = true;
}
} catch (IllegalStateException e) {
// can happen if the worker dies before we could finish reading; let's assume success
_logger.infof("worker appears dead before response completion\n");
Task fakeTask = new Task(TaskStatus.TerminatedByUser, "worker appears dead before responding");
_workQueueMgr.processTaskCheckResult(work, fakeTask);
killed = true;
}
}
} catch (ProcessingException e) {
_logger.errorf("unable to connect to %s: %s\n", worker, ExceptionUtils.getStackTrace(e));
} catch (Exception e) {
_logger.errorf("exception: %s\n", ExceptionUtils.getStackTrace(e));
} finally {
if (client != null) {
client.close();
}
}
return killed;
}
use of org.batfish.common.Task in project batfish by batfish.
the class WorkQueueMgrTest method processTaskCheckTerminatedByUser.
@Test
public void processTaskCheckTerminatedByUser() throws Exception {
initTestrigMetadata("testrig", "env_default", ProcessingStatus.UNINITIALIZED);
QueuedWork work1 = new QueuedWork(new WorkItem(CONTAINER, "testrig"), new WorkDetails("testrig", "env_default", WorkType.PARSING));
doAction(new Action(ActionType.QUEUE, work1));
_workQueueMgr.processTaskCheckResult(work1, new Task(TaskStatus.TerminatedByUser, "Fake"));
/*
* after processing the termination task,
* 1) the status of work should be terminatedbyuser
* 2) incomplete queue should be empty
*/
assertThat(work1.getStatus(), equalTo(WorkStatusCode.TERMINATEDBYUSER));
assertThat(_workQueueMgr.getLength(QueueType.INCOMPLETE), equalTo(0L));
}
Aggregations