use of org.ligoj.bootstrap.core.resource.BusinessException in project ligoj-api by ligoj.
the class LongTaskRunner method createAsNeeded.
/**
* Get or create a new task associated to given subscription.
*
* @param lockedId
* The locked entity's identifier. The related entity will be locked.
* @return The task, never <code>null</code>.
*/
default T createAsNeeded(final I lockedId) {
final T task = Optional.ofNullable(getTask(lockedId)).map(t -> {
// Additional remote check (optional)
if (!isFinished(t)) {
// On this service, there is already a running remote task
throw new BusinessException("concurrent-task-remote", t.getAuthor(), t.getStart(), lockedId);
}
return t;
}).orElseGet(() -> {
final T newTask = newTask().get();
newTask.setLocked(getLockedRepository().findOneExpected(lockedId));
return newTask;
});
// Reset internal fields
task.setAuthor(SpringUtils.getBean(SecurityHelper.class).getLogin());
task.setStart(DateUtils.newCalendar().getTime());
task.setEnd(null);
task.setFailed(false);
return task;
}
use of org.ligoj.bootstrap.core.resource.BusinessException in project plugin-prov by ligoj.
the class TerraformResource method execute.
/**
* Execute the given Terraform commands. Note there is no concurrency check for now.
*/
private void execute(final Subscription subscription, final Writer out, final String[][] commands, final String... additionalParameters) throws InterruptedException, IOException {
final AtomicInteger step = new AtomicInteger(0);
// Reset the current step
for (final String[] command : commands) {
// Next step, another transaction
runner.nextStep("service:prov:test:account", t -> t.setStep(TerraformStep.values()[step.get()]));
final int code = execute(subscription, out, ArrayUtils.addAll(command, additionalParameters));
if (code == 0) {
// Nothing wrong, no change, only useless to go further
log.info("Terraform paused for {} ({}) : {}", subscription.getId(), subscription, code);
out.write("Terraform exit code " + code + " -> no need to continue");
break;
}
if (code != 2) {
// Something goes wrong
log.error("Terraform failed for {} ({}) : {}", subscription.getId(), subscription, code);
out.write("Terraform exit code " + code + " -> aborted");
throw new BusinessException("aborted");
}
out.flush();
// Code is correct, proceed the next command
step.incrementAndGet();
}
}
use of org.ligoj.bootstrap.core.resource.BusinessException in project plugin-prov by ligoj.
the class ProvResource method create.
@Override
public void create(final int subscription) throws Exception {
// Add an empty quote
final ProvQuote quote = new ProvQuote();
quote.setSubscription(subscriptionRepository.findOne(subscription));
// Associate a default name and description
quote.setName(quote.getSubscription().getProject().getName());
final Node provider = quote.getSubscription().getNode().getRefined();
final List<ProvLocation> locations = locationRepository.findAllBy("node.id", provider.getId());
if (locations.isEmpty()) {
// No available location, need a catalog to continue
throw new BusinessException(SERVICE_KEY + "-no-catalog", provider.getId(), provider.getName());
}
quote.setLocation(locations.get(0));
quote.setDescription(quote.getSubscription().getProject().getPkey() + "-> " + provider.getName());
repository.saveAndFlush(quote);
}
Aggregations