Search in sources :

Example 1 with BusinessException

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;
}
Also used : TxType(javax.transaction.Transactional.TxType) PathParam(javax.ws.rs.PathParam) AbstractLongTask(org.ligoj.app.model.AbstractLongTask) Transactional(javax.transaction.Transactional) SpringUtils(org.ligoj.bootstrap.core.SpringUtils) Date(java.util.Date) RestRepository(org.ligoj.bootstrap.core.dao.RestRepository) BusinessException(org.ligoj.bootstrap.core.resource.BusinessException) Supplier(java.util.function.Supplier) DateUtils(org.ligoj.bootstrap.core.DateUtils) Serializable(java.io.Serializable) Consumer(java.util.function.Consumer) SubscriptionRepository(org.ligoj.app.dao.SubscriptionRepository) SecurityHelper(org.ligoj.bootstrap.core.security.SecurityHelper) LongTaskRepository(org.ligoj.app.dao.task.LongTaskRepository) Persistable(org.springframework.data.domain.Persistable) EntityNotFoundException(javax.persistence.EntityNotFoundException) Optional(java.util.Optional) BusinessException(org.ligoj.bootstrap.core.resource.BusinessException)

Example 2 with BusinessException

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();
    }
}
Also used : BusinessException(org.ligoj.bootstrap.core.resource.BusinessException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 3 with BusinessException

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);
}
Also used : BusinessException(org.ligoj.bootstrap.core.resource.BusinessException) ProvLocation(org.ligoj.app.plugin.prov.model.ProvLocation) ProvQuote(org.ligoj.app.plugin.prov.model.ProvQuote) Node(org.ligoj.app.model.Node)

Aggregations

BusinessException (org.ligoj.bootstrap.core.resource.BusinessException)3 Serializable (java.io.Serializable)1 Date (java.util.Date)1 Optional (java.util.Optional)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Consumer (java.util.function.Consumer)1 Supplier (java.util.function.Supplier)1 EntityNotFoundException (javax.persistence.EntityNotFoundException)1 Transactional (javax.transaction.Transactional)1 TxType (javax.transaction.Transactional.TxType)1 PathParam (javax.ws.rs.PathParam)1 SubscriptionRepository (org.ligoj.app.dao.SubscriptionRepository)1 LongTaskRepository (org.ligoj.app.dao.task.LongTaskRepository)1 AbstractLongTask (org.ligoj.app.model.AbstractLongTask)1 Node (org.ligoj.app.model.Node)1 ProvLocation (org.ligoj.app.plugin.prov.model.ProvLocation)1 ProvQuote (org.ligoj.app.plugin.prov.model.ProvQuote)1 DateUtils (org.ligoj.bootstrap.core.DateUtils)1 SpringUtils (org.ligoj.bootstrap.core.SpringUtils)1 RestRepository (org.ligoj.bootstrap.core.dao.RestRepository)1