use of cz.metacentrum.perun.core.api.Service in project perun by CESNET.
the class TasksManagerBlImpl method deleteTask.
@Override
public void deleteTask(PerunSession sess, Task task) {
Facility facility = task.getFacility();
Service service = task.getService();
// clear all task results
getTasksManagerImpl().deleteTaskResults(task.getId());
// remove task itself
getTasksManagerImpl().removeTask(service, facility);
}
use of cz.metacentrum.perun.core.api.Service in project perun by CESNET.
the class TasksManagerBlImpl method getFacilityState.
@Override
public FacilityState getFacilityState(PerunSession session, Facility facility) throws FacilityNotExistsException {
perun.getFacilitiesManagerBl().checkFacilityExists(session, facility);
// get all tasks
List<Task> tasks = getTasksManagerImpl().listAllTasksForFacility(facility.getId());
// define state
FacilityState state = new FacilityState();
state.setFacility(facility);
// if no tasks we can't determine facility state
if (tasks == null || tasks.isEmpty()) {
state.setState(FacilityState.FacilityPropagationState.NOT_DETERMINED);
return state;
} else {
// OK if no change
state.setState(FacilityState.FacilityPropagationState.OK);
}
// fill all available destinations
List<RichDestination> destinations = perun.getServicesManagerBl().getAllRichDestinations(session, facility);
for (RichDestination rd : destinations) {
state.getResults().put(rd.getDestination(), FacilityState.FacilityPropagationState.NOT_DETERMINED);
}
// magic with tasks :-)
for (Task task : tasks) {
// save previous facility state
FacilityState.FacilityPropagationState facState = state.getState();
// PROCESSING and not ERROR before
if (Arrays.asList(Task.TaskStatus.GENERATED, Task.TaskStatus.GENERATING, Task.TaskStatus.PLANNED, Task.TaskStatus.SENDING).contains(task.getStatus()) && (facState != FacilityState.FacilityPropagationState.ERROR)) {
state.setState(FacilityState.FacilityPropagationState.PROCESSING);
} else // ERROR - set ERROR
if (Arrays.asList(Task.TaskStatus.ERROR, Task.TaskStatus.GENERROR, Task.TaskStatus.SENDERROR).contains(task.getStatus())) {
state.setState(FacilityState.FacilityPropagationState.ERROR);
}
// get destination status
if (task.getService() != null) {
List<TaskResult> results = getTasksManagerImpl().getTaskResultsByTask(task.getId());
Map<Service, Map<Destination, TaskResult>> latestResults = new HashMap<>();
for (TaskResult res : results) {
if (latestResults.get(res.getService()) == null) {
// put in map since result for service exists
Map<Destination, TaskResult> value = new HashMap<>();
value.put(res.getDestination(), res);
latestResults.put(res.getService(), value);
} else if (latestResults.get(res.getService()) != null && latestResults.get(res.getService()).get(res.getDestination()) == null) {
// put in inner map, since destination for service not yet exists
latestResults.get(res.getService()).put(res.getDestination(), res);
} else {
// update in inner map since this is later task result
if (latestResults.get(res.getService()).get(res.getDestination()).getId() < res.getId()) {
// put in map
latestResults.get(res.getService()).put(res.getDestination(), res);
}
}
}
for (Map<Destination, TaskResult> res : latestResults.values()) {
for (TaskResult result : res.values()) {
// iterate over all latest tasks results
String destination = result.getDestination().getDestination();
FacilityState.FacilityPropagationState propState = state.getResults().get(destination);
// if any error => state is error
if (TaskResult.TaskResultStatus.ERROR.equals(result.getStatus())) {
state.getResults().put(destination, FacilityState.FacilityPropagationState.ERROR);
continue;
}
// if result ok and previous was not bad
if (TaskResult.TaskResultStatus.DONE.equals(result.getStatus())) {
if (FacilityState.FacilityPropagationState.NOT_DETERMINED.equals(propState)) {
state.getResults().put(destination, FacilityState.FacilityPropagationState.OK);
}
}
}
}
}
}
return state;
}
use of cz.metacentrum.perun.core.api.Service in project perun by CESNET.
the class SendWorkerImpl method call.
@Override
public SendTask call() throws TaskExecutionException {
Task task = sendTask.getTask();
Service service = task.getService();
// we never actually run DUMMY destinations !!
if (sendTask.getDestination().getPropagationType().equals(Destination.PROPAGATIONTYPE_DUMMY)) {
log.info("[{}] Executing SEND worker skipped for dummy Destination: {}. Marked as SENT.", sendTask.getTask().getId(), sendTask.getDestination().getDestination());
// set results
sendTask.setStatus(SENT);
sendTask.setStdout("");
sendTask.setStderr("");
sendTask.setReturnCode(0);
sendTask.setEndTime(new Date(System.currentTimeMillis()));
return sendTask;
}
log.info("[{}] Executing SEND worker for Task with Service ID: {} and Facility ID: {} and Destination: {}", sendTask.getTask().getId(), sendTask.getTask().getServiceId(), sendTask.getTask().getFacilityId(), sendTask.getDestination().getDestination());
ProcessBuilder pb = new ProcessBuilder(service.getScript(), task.getFacility().getName(), sendTask.getDestination().getDestination(), sendTask.getDestination().getType());
try {
// start the script and wait for results
super.execute(pb);
// set results
sendTask.setStdout(super.getStdout());
sendTask.setStderr(super.getStderr());
sendTask.setReturnCode(super.getReturnCode());
sendTask.setEndTime(new Date(System.currentTimeMillis()));
if (getReturnCode() != 0) {
log.error("[{}] SEND worker failed for Task. Ret code {}, STDOUT: {}, STDERR: {}", task.getId(), getReturnCode(), getStdout(), getStderr());
sendTask.setStatus(ERROR);
// XXX: why exception? There is nothing exceptional about the situation.
throw new TaskExecutionException(task, sendTask.getDestination(), getReturnCode(), getStdout(), getStderr());
} else {
if (getStderr().isEmpty()) {
sendTask.setStatus(SENT);
} else {
sendTask.setStatus(WARNING);
}
log.info("[{}] SEND worker finished for Task with status {}. Ret code {}, STDOUT: {}, STDERR: {}", sendTask.getTask().getId(), sendTask.getStatus(), getReturnCode(), getStdout(), getStderr());
return sendTask;
}
} catch (IOException e) {
log.error("[{}] SEND worker failed for Task. IOException: {}.", task.getId(), e);
sendTask.setStatus(ERROR);
sendTask.setEndTime(new Date(System.currentTimeMillis()));
throw new TaskExecutionException(task, sendTask.getDestination(), 2, "", e.getMessage());
} catch (InterruptedException e) {
log.warn("[{}] SEND worker failed for Task. Execution was interrupted {}.", task.getId(), e);
sendTask.setStatus(ERROR);
sendTask.setEndTime(new Date(System.currentTimeMillis()));
throw new TaskExecutionException(task, sendTask.getDestination(), 1, "", e.getMessage());
}
}
use of cz.metacentrum.perun.core.api.Service in project perun by CESNET.
the class SchedulingPoolImpl method scheduleTask.
@Override
public void scheduleTask(Task task, int delayCount) {
// check if service/facility exists
boolean removeTask = false;
try {
Service service = perun.getServicesManager().getServiceById(sess, task.getServiceId());
Facility facility = perun.getFacilitiesManager().getFacilityById(sess, task.getFacilityId());
task.setService(service);
task.setFacility(facility);
} catch (ServiceNotExistsException e) {
log.error("[{}] Task NOT added to waiting queue, service not exists: {}.", task.getId(), task);
removeTask = true;
} catch (FacilityNotExistsException e) {
log.error("[{}] Task NOT added to waiting queue, facility not exists: {}.", task.getId(), task);
removeTask = true;
} catch (InternalErrorException | PrivilegeException e) {
log.error("[{}] {}", task.getId(), e);
}
if (!task.getService().isEnabled() || ((PerunBl) perun).getServicesManagerBl().isServiceBlockedOnFacility(task.getService(), task.getFacility())) {
log.error("[{}] Task NOT added to waiting queue, service is blocked: {}.", task.getId(), task);
// do not change Task status or any other data !
if (!removeTask)
return;
}
try {
List<Destination> destinations = perun.getServicesManager().getDestinations(sess, task.getService(), task.getFacility());
if (destinations != null && !destinations.isEmpty()) {
Iterator<Destination> iter = destinations.iterator();
while (iter.hasNext()) {
Destination dest = iter.next();
if (((PerunBl) perun).getServicesManagerBl().isServiceBlockedOnDestination(task.getService(), dest.getId())) {
iter.remove();
}
}
if (destinations.isEmpty()) {
// All service destinations were blocked -> Task is denied to be sent to engine just like
// when service is blocked globally in Perun or on facility as a whole.
log.debug("[{}] Task NOT added to waiting queue, all its destinations are blocked.", task.getId());
if (!removeTask)
return;
}
}
} catch (ServiceNotExistsException e) {
log.error("[{}] Task NOT added to waiting queue, service not exists: {}.", task.getId(), task);
removeTask = true;
} catch (FacilityNotExistsException e) {
log.error("[{}] Task NOT added to waiting queue, facility not exists: {}.", task.getId(), task);
removeTask = true;
} catch (InternalErrorException | PrivilegeException e) {
log.error("[{}] {}", task.getId(), e);
}
try {
List<Service> assignedServices = perun.getServicesManager().getAssignedServices(sess, task.getFacility());
if (!assignedServices.contains(task.getService())) {
log.debug("[{}] Task NOT added to waiting queue, service is not assigned to facility any more: {}.", task.getId(), task);
if (!removeTask)
return;
}
} catch (FacilityNotExistsException e) {
removeTask = true;
log.error("[{}] Task removed from database, facility no longer exists: {}.", task.getId(), task);
} catch (InternalErrorException | PrivilegeException e) {
log.error("[{}] Unable to check Service assignment to Facility: {}", task.getId(), e.getMessage());
}
if (removeTask) {
// in memory task belongs to non existent facility/service - remove it and return
try {
removeTask(task);
return;
} catch (TaskStoreException e) {
log.error("[{}] Unable to remove Task from pool: {}.", task.getId(), e);
return;
}
}
// Task is eligible for running - create new schedule
task.setSourceUpdated(false);
long newTaskDelay = 0;
if (!task.isPropagationForced()) {
// normal tasks are delayed
try {
newTaskDelay = Long.parseLong(dispatcherProperties.getProperty("dispatcher.task.delay.time"));
} catch (NumberFormatException e) {
log.warn("Could not parse value of dispatcher.task.delay.time property. Using default.");
newTaskDelay = 30000;
}
}
if (task.isPropagationForced()) {
delayCount = 0;
}
if (delayCount < 0) {
try {
delayCount = Integer.parseInt(dispatcherProperties.getProperty("dispatcher.task.delay.count"));
} catch (NumberFormatException e) {
log.warn("Could not parse value of dispatcher.task.delay.count property. Using default.");
delayCount = 4;
}
}
TaskSchedule schedule = new TaskSchedule(newTaskDelay, task);
schedule.setBase(System.currentTimeMillis());
schedule.setDelayCount(delayCount);
// Task was newly planned for propagation, switch state.
if (!task.getStatus().equals(TaskStatus.WAITING)) {
task.setStatus(TaskStatus.WAITING);
task.setSchedule(LocalDateTime.now());
// clear previous timestamps
task.setSentToEngine((LocalDateTime) null);
task.setStartTime((LocalDateTime) null);
task.setGenStartTime((LocalDateTime) null);
task.setSendStartTime((LocalDateTime) null);
task.setEndTime((LocalDateTime) null);
task.setGenEndTime((LocalDateTime) null);
task.setSendEndTime((LocalDateTime) null);
tasksManagerBl.updateTask(sess, task);
}
boolean added = false;
if (schedule.getTask().isPropagationForced()) {
added = waitingForcedTasksQueue.add(schedule);
} else {
added = waitingTasksQueue.add(schedule);
}
if (!added) {
log.error("[{}] Task could not be added to waiting queue. Shouldn't ever happen. Look to javadoc of DelayQueue. {}", task.getId(), schedule);
} else {
log.debug("[{}] Task was added to waiting queue: {}", task.getId(), schedule);
}
}
use of cz.metacentrum.perun.core.api.Service in project perun by CESNET.
the class EventServiceResolverTest method parseEventTest.
@Test
public void parseEventTest() throws ServiceNotExistsException, InvalidEventMessageException, PrivilegeException {
System.out.println("EventServiceResolver.parseEventTest()");
AuditEvent auditEvent = new DirectMemberAddedToGroup(member1, group1);
Event event = new Event();
event.setTimeStamp(System.currentTimeMillis());
event.setHeader("portishead");
event.setData(auditEvent);
Map<Facility, Set<Service>> resolvedServices = eventServiceResolver.resolveEvent(event.getData());
Assert.assertTrue("We should resolved only one facility-service", resolvedServices.size() == 1);
Set<Service> resolved = resolvedServices.get(facility1);
Assert.assertTrue("We should have 2 service", resolved.size() == 2);
Assert.assertTrue("Our Service 1 is missing", resolved.contains(service1));
Assert.assertTrue("Our Service 2 is missing", resolved.contains(service2));
}
Aggregations