use of cz.metacentrum.perun.core.api.exceptions.ServiceNotExistsException in project perun by CESNET.
the class EventProcessorImpl method receiveEvent.
@Override
public void receiveEvent(String event) {
log.info("Current pool size BEFORE event processing:" + schedulingPool.getSize());
log.debug("Event " + event + " is going to be resolved...");
Task task = null;
try {
task = eventParser.parseEvent(event);
} catch (InvalidEventMessageException e) {
log.error(e.toString());
} catch (ServiceNotExistsException e) {
log.error(e.toString());
} catch (InternalErrorException e) {
log.error(e.toString());
} catch (PrivilegeException e) {
log.error(e.toString());
}
if (task == null) {
return;
}
// FIXME: Disabled because it can cause race condition. See RT#33803
if (false) {
// if (event.contains("forceit")) { // TODO: Move string constant to
// a properties file
log.debug("\t Facility[" + task.getFacility() + "]");
log.debug("\t Resolved ExecService[" + task.getExecService() + "]");
if (task != null && task.getFacility() != null && task.getExecService() != null) {
// log.debug("SCHEDULING vie Force Service Propagation: ExecService["
// + execService.getId() + "] : Facility[" + results.getRight()
// + "]");
schedulingPool.addToPool(task);
final Task ntask = task;
taskExecutorEventProcessor.execute(new Runnable() {
@Override
public void run() {
try {
taskScheduler.propagateService(ntask, new Date(System.currentTimeMillis()));
} catch (InternalErrorException e) {
log.error(e.toString());
}
}
});
}
log.debug("POOL SIZE:" + schedulingPool.getSize());
}
log.debug("\t Facility[" + task.getFacility() + "]");
log.debug("\t Resolved ExecService[" + task.getExecService() + "]");
if (task.getFacility() != null && task.getExecService() != null) {
// log.debug("ADD to POOL: ExecService[" +
// results.getLeft().getId() + "] : Facility[" +
// results.getRight() + "]");
Task currentTask = schedulingPool.getTaskById(task.getId());
if (currentTask == null) {
// task.setSourceUpdated(false);
schedulingPool.addToPool(task);
currentTask = task;
} else {
// currentTask.setSourceUpdated(true);
log.debug("Resetting current task destination list to {}", task.getDestinations());
currentTask.setDestinations(task.getDestinations());
currentTask.setPropagationForced(task.isPropagationForced());
}
if (currentTask.isPropagationForced()) {
final Task ntask = currentTask;
try {
taskExecutorEventProcessor.execute(new Runnable() {
@Override
public void run() {
try {
taskScheduler.propagateService(ntask, new Date(System.currentTimeMillis()));
} catch (InternalErrorException e) {
log.error(e.toString());
}
}
});
} catch (Exception e) {
log.error("Error queuing task to executor: " + e.toString());
}
}
}
log.debug("POOL SIZE:" + schedulingPool.getSize());
log.info("Current pool size AFTER event processing:" + schedulingPool.getSize());
}
use of cz.metacentrum.perun.core.api.exceptions.ServiceNotExistsException in project perun by CESNET.
the class TaskSchedulerImpl method sendToEngine.
private void sendToEngine(Task task) {
DispatcherQueue dispatcherQueue;
try {
dispatcherQueue = schedulingPool.getQueueForTask(task);
} catch (InternalErrorException e1) {
log.error("No engine set for task " + task.toString() + ", could not send it!");
return;
}
if (dispatcherQueue == null) {
// where should we send the task?
if (dispatcherQueuePool.poolSize() > 0) {
dispatcherQueue = dispatcherQueuePool.getPool().iterator().next();
try {
schedulingPool.setQueueForTask(task, dispatcherQueue);
} catch (InternalErrorException e) {
log.error("Could not assign new queue for task {}: {}", task.getId(), e);
return;
}
log.debug("Assigned new queue " + dispatcherQueue.getQueueName() + " to task " + task.getId());
} else {
// bad luck...
log.error("Task " + task.toString() + " has no engine assigned and there are no engines registered...");
return;
}
}
// task|[engine_id]|[task_id][is_forced][exec_service_id][facility]|[destination_list]|[dependency_list]
// - the task|[engine_id] part is added by dispatcherQueue
List<Destination> destinations = task.getDestinations();
if (destinations == null || destinations.isEmpty()) {
log.debug("No destinations for task " + task.toString() + ", trying to query the database...");
try {
initPerunSession();
destinations = perun.getServicesManager().getDestinations(perunSession, task.getExecService().getService(), task.getFacility());
} catch (ServiceNotExistsException e) {
log.error("No destinations found for task " + task.getId());
task.setEndTime(new Date(System.currentTimeMillis()));
schedulingPool.setTaskStatus(task, TaskStatus.ERROR);
return;
} catch (FacilityNotExistsException e) {
log.error("Facility for task {} does not exist...", task.getId());
task.setEndTime(new Date(System.currentTimeMillis()));
schedulingPool.setTaskStatus(task, TaskStatus.ERROR);
return;
} catch (PrivilegeException e) {
log.error("Privilege error accessing the database: " + e.getMessage());
task.setEndTime(new Date(System.currentTimeMillis()));
schedulingPool.setTaskStatus(task, TaskStatus.ERROR);
return;
} catch (InternalErrorException e) {
log.error("Internal error: " + e.getMessage());
task.setEndTime(new Date(System.currentTimeMillis()));
schedulingPool.setTaskStatus(task, TaskStatus.ERROR);
return;
}
}
log.debug("Fetched destinations: " + ((destinations == null) ? "[]" : destinations.toString()));
task.setDestinations(destinations);
StringBuilder destinations_s = new StringBuilder("Destinations [");
if (destinations != null) {
for (Destination destination : destinations) {
destinations_s.append(destination.serializeToString() + ", ");
}
}
destinations_s.append("]");
String dependencies = "";
dispatcherQueue.sendMessage("[" + task.getId() + "][" + task.isPropagationForced() + "]|[" + fixStringSeparators(task.getExecService().serializeToString()) + "]|[" + fixStringSeparators(task.getFacility().serializeToString()) + "]|[" + fixStringSeparators(destinations_s.toString()) + "]|[" + dependencies + "]");
task.setStartTime(new Date(System.currentTimeMillis()));
task.setEndTime(null);
schedulingPool.setTaskStatus(task, TaskStatus.PROCESSING);
}
use of cz.metacentrum.perun.core.api.exceptions.ServiceNotExistsException in project perun by CESNET.
the class GeneralServiceManagerImpl method insertExecService.
@Override
public int insertExecService(PerunSession perunSession, ExecService execService) throws InternalErrorException, PrivilegeException, ServiceExistsException {
Service service = null;
try {
service = servicesManager.getServiceByName(perunSession, execService.getService().getName());
} catch (ServiceNotExistsException e) {
service = servicesManager.createService(perunSession, execService.getService());
}
execService.setService(service);
return execServiceDao.insertExecService(execService);
}
use of cz.metacentrum.perun.core.api.exceptions.ServiceNotExistsException in project perun by CESNET.
the class GeneralServiceManagerImpl method createCompleteService.
@Override
@Transactional(rollbackFor = Exception.class)
public Service createCompleteService(PerunSession perunSession, String serviceName, String scriptPath, int defaultDelay, boolean enabled) throws InternalErrorException, PrivilegeException, ServiceExistsException {
if (!AuthzResolver.isAuthorized(perunSession, Role.PERUNADMIN)) {
throw new PrivilegeException(perunSession, "createCompleteService");
}
Service service = null;
try {
service = servicesManager.getServiceByName(perunSession, serviceName);
if (service != null) {
throw new ServiceExistsException(service);
}
} catch (ServiceNotExistsException e) {
service = new Service();
service.setName(serviceName);
service = servicesManager.createService(perunSession, service);
}
ExecService genExecService = new ExecService();
genExecService.setService(service);
genExecService.setDefaultDelay(defaultDelay);
genExecService.setEnabled(enabled);
genExecService.setScript(scriptPath);
genExecService.setExecServiceType(ExecServiceType.GENERATE);
genExecService.setId(execServiceDao.insertExecService(genExecService));
ExecService sendExecService = new ExecService();
sendExecService.setService(service);
sendExecService.setDefaultDelay(defaultDelay);
sendExecService.setEnabled(enabled);
sendExecService.setScript(scriptPath);
sendExecService.setExecServiceType(ExecServiceType.SEND);
sendExecService.setId(execServiceDao.insertExecService(sendExecService));
this.createDependency(sendExecService, genExecService);
return service;
}
use of cz.metacentrum.perun.core.api.exceptions.ServiceNotExistsException in project perun by CESNET.
the class EventParserImpl method parseEvent.
@Override
public Task parseEvent(String event) throws InvalidEventMessageException, ServiceNotExistsException, InternalErrorException, PrivilegeException {
log.info("I am going to process event:" + event);
/**
* Expected string format as on:
* https://projekty.ics.muni.cz/perunv3/trac
* /wiki/PerunEngineDispatcherController event|x|[timestamp][Event
* header][Event data] New format:
* "task|[engine_id]|[task_id][is_forced]|[exec_service]|[facility]|[destination_list]|[dependency_list]"
*
*/
// String eventParsingPattern =
// "^event\\|([0-9]{1,6})\\|\\[([a-zA-Z0-9: ]+)\\]\\[([^\\]]+)\\]\\[(.*)\\]$";
String eventParsingPattern = "^task\\|([0-9]{1,6})\\|\\[([0-9]+)\\]\\[([^\\]]+)\\]\\|\\[([^\\|]+)\\]\\|\\[([^\\|]+)\\]\\|\\[([^\\|]+)\\]\\|\\[(.*)\\]$";
Pattern pattern = Pattern.compile(eventParsingPattern);
Matcher matcher = pattern.matcher(event);
boolean matchFound = matcher.find();
if (matchFound) {
log.debug("Message format matched ok...");
String thisEngineID = matcher.group(1);
// compare it...
try {
if (Integer.parseInt(thisEngineID) != Integer.parseInt((String) propertiesBean.get("engine.unique.id"))) {
throw new InvalidEventMessageException("Wrong Engine ID. Was:" + thisEngineID + ", Expected:" + propertiesBean.get("engine.unique.id"));
}
} catch (Exception e) {
throw new InvalidEventMessageException("Wrong Engine ID: parse exception", e);
}
// Data should provide information regarding the target ExecService
// (Processing rule).
String eventTaskId = matcher.group(2);
String eventIsForced = matcher.group(3);
String eventExecService = matcher.group(4);
String eventFacility = matcher.group(5);
String eventDestinationList = matcher.group(6);
String eventDependencyList = matcher.group(7);
// check possible enconding
if (!eventExecService.startsWith("ExecService")) {
eventExecService = new String(Base64.decodeBase64(eventExecService));
}
if (!eventExecService.startsWith("ExecService")) {
throw new InvalidEventMessageException("Wrong exec service: parse exception");
}
if (!eventFacility.startsWith("Facility")) {
eventFacility = new String(Base64.decodeBase64(eventFacility));
}
if (!eventFacility.startsWith("Facility")) {
throw new InvalidEventMessageException("Wrong facility: parse exception");
}
if (!eventDestinationList.startsWith("Destinations")) {
eventDestinationList = new String(Base64.decodeBase64(eventDestinationList));
}
if (!eventDestinationList.startsWith("Destinations")) {
throw new InvalidEventMessageException("Wrong destination list: parse exception");
}
log.debug("Event data to be parsed: task id " + eventTaskId + ", forced " + eventIsForced + ", facility " + eventFacility + ", exec service " + eventExecService + ", destination list " + eventDestinationList + ", dependency list " + eventDependencyList);
// Prepare variables
Facility facility;
ExecService execService;
List<Destination> destinationList = new ArrayList<Destination>();
// resolve facility
// deserialize event data
List<PerunBean> listOfBeans = AuditParser.parseLog(eventFacility);
try {
facility = (Facility) listOfBeans.get(0);
} catch (Exception e) {
throw new InvalidEventMessageException("Could not resolve facility from event [" + eventFacility + "]", e);
}
// resolve exec service
// deserialize event data
listOfBeans = AuditParser.parseLog(eventExecService);
try {
execService = (ExecService) listOfBeans.get(0);
} catch (Exception e) {
throw new InvalidEventMessageException("Could not resolve exec service from event [" + eventExecService + "]", e);
}
// resolve list of destinations
listOfBeans = AuditParser.parseLog(eventDestinationList);
log.debug("Found list of destination beans: " + listOfBeans);
// return new Pair<ExecService, Facility>(execService, facility);
try {
for (PerunBean bean : listOfBeans) {
destinationList.add((Destination) bean);
}
} catch (Exception e) {
throw new InvalidEventMessageException("Could not resolve list of destinations from event.", e);
}
Task task = new Task();
task.setId(Integer.parseInt(eventTaskId));
task.setFacility(facility);
task.setExecService(execService);
task.setDestinations(destinationList);
task.setDelay(execService.getDefaultDelay());
task.setRecurrence(execService.getDefaultRecurrence());
task.setPropagationForced(Boolean.parseBoolean(eventIsForced));
// resolve list of dependencies
if (eventDependencyList != null) {
for (String token : eventDependencyList.split("[\t ]*,[\t ]*")) {
if (token.length() > 0) {
try {
dependenciesResolver.addDependency(task, Integer.parseInt(token));
} catch (Exception e) {
throw new InvalidEventMessageException("Invalid dependency in event: " + token);
}
}
}
}
return task;
} else {
throw new InvalidEventMessageException("Invalid message format: Message[" + event + "]");
}
}
Aggregations