Search in sources :

Example 11 with GlobalLock

use of org.killbill.commons.locker.GlobalLock in project killbill by killbill.

the class InvoiceDispatcher method processSubscriptionStartRequestedDate.

public void processSubscriptionStartRequestedDate(final RequestedSubscriptionInternalEvent transition, final InternalCallContext context) {
    final long dryRunNotificationTime = invoiceConfig.getDryRunNotificationSchedule(context).getMillis();
    final boolean isInvoiceNotificationEnabled = dryRunNotificationTime > 0;
    if (!isInvoiceNotificationEnabled) {
        return;
    }
    final UUID accountId;
    try {
        accountId = subscriptionApi.getAccountIdFromSubscriptionId(transition.getSubscriptionId(), context);
    } catch (final SubscriptionBaseApiException e) {
        log.warn("Failed handling SubscriptionBase change.", new InvoiceApiException(ErrorCode.INVOICE_NO_ACCOUNT_ID_FOR_SUBSCRIPTION_ID, transition.getSubscriptionId().toString()));
        return;
    }
    GlobalLock lock = null;
    try {
        lock = locker.lockWithNumberOfTries(LockerType.ACCNT_INV_PAY.toString(), accountId.toString(), invoiceConfig.getMaxGlobalLockRetries());
        processSubscriptionStartRequestedDateWithLock(accountId, transition, context);
    } catch (final LockFailedException e) {
        log.warn("Failed to process RequestedSubscriptionInternalEvent for accountId='{}'", accountId.toString(), e);
    } finally {
        if (lock != null) {
            lock.release();
        }
    }
}
Also used : GlobalLock(org.killbill.commons.locker.GlobalLock) InvoiceApiException(org.killbill.billing.invoice.api.InvoiceApiException) LockFailedException(org.killbill.commons.locker.LockFailedException) UUID(java.util.UUID) SubscriptionBaseApiException(org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)

Example 12 with GlobalLock

use of org.killbill.commons.locker.GlobalLock in project killbill by killbill.

the class IncompletePaymentTransactionTask method tryToDoJanitorOperationWithAccountLock.

private TransactionStatus tryToDoJanitorOperationWithAccountLock(final JanitorIterationCallback callback, final InternalTenantContext internalTenantContext) throws LockFailedException {
    GlobalLock lock = null;
    try {
        final ImmutableAccountData account = accountInternalApi.getImmutableAccountDataByRecordId(internalTenantContext.getAccountRecordId(), internalTenantContext);
        lock = locker.lockWithNumberOfTries(LockerType.ACCNT_INV_PAY.toString(), account.getId().toString(), paymentConfig.getMaxGlobalLockRetries());
        return callback.doIteration();
    } catch (final AccountApiException e) {
        log.warn("Error retrieving accountRecordId='{}'", internalTenantContext.getAccountRecordId(), e);
    } finally {
        if (lock != null) {
            lock.release();
        }
    }
    return null;
}
Also used : GlobalLock(org.killbill.commons.locker.GlobalLock) ImmutableAccountData(org.killbill.billing.account.api.ImmutableAccountData) AccountApiException(org.killbill.billing.account.api.AccountApiException)

Example 13 with GlobalLock

use of org.killbill.commons.locker.GlobalLock in project killbill-analytics-plugin by killbill.

the class AnalyticsListener method handleAnalyticsJob.

private void handleAnalyticsJob(final AnalyticsJob job) throws AnalyticsRefreshException {
    final AnalyticsConfiguration analyticsConfiguration = analyticsConfigurationHandler.getConfigurable(job.getTenantId());
    if (!locker.isFree(ANALYTICS_REFRESH_LOCK_NAME, job.getAccountId().toString())) {
        final Integer delaySec = analyticsConfiguration.rescheduleIntervalOnLockSeconds;
        if (delaySec > 0) {
            final DateTime nextRescheduleDt = clock.getUTCNow().plusSeconds(delaySec);
            if (scheduleAnalyticsJob(job, analyticsConfiguration)) {
                logger.info("Lock is busy for account {}, rescheduling job at time {}", job.getAccountId(), nextRescheduleDt);
                return;
            }
        }
    }
    GlobalLock lock = null;
    try {
        lock = locker.lockWithNumberOfTries(ANALYTICS_REFRESH_LOCK_NAME, job.getAccountId().toString(), analyticsConfiguration.lockAttemptRetries);
        handleAnalyticsJobWithLock(job);
    } catch (final LockFailedException e) {
        throw new RuntimeException(e);
    } finally {
        if (lock != null) {
            lock.release();
        }
    }
}
Also used : AnalyticsConfiguration(org.killbill.billing.plugin.analytics.api.core.AnalyticsConfiguration) GlobalLock(org.killbill.commons.locker.GlobalLock) LockFailedException(org.killbill.commons.locker.LockFailedException) DateTime(org.joda.time.DateTime)

Aggregations

GlobalLock (org.killbill.commons.locker.GlobalLock)13 LockFailedException (org.killbill.commons.locker.LockFailedException)10 UUID (java.util.UUID)3 Test (org.testng.annotations.Test)3 LinkedList (java.util.LinkedList)2 DateTime (org.joda.time.DateTime)2 AccountApiException (org.killbill.billing.account.api.AccountApiException)2 ImmutableAccountData (org.killbill.billing.account.api.ImmutableAccountData)2 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)2 InvoiceApiException (org.killbill.billing.invoice.api.InvoiceApiException)2 InvoiceItemModelDao (org.killbill.billing.invoice.dao.InvoiceItemModelDao)2 InvoiceModelDao (org.killbill.billing.invoice.dao.InvoiceModelDao)2 Predicate (com.google.common.base.Predicate)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 IOException (java.io.IOException)1 LocalDate (org.joda.time.LocalDate)1 State (org.killbill.automaton.State)1 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)1 ItemAdjInvoiceItem (org.killbill.billing.invoice.model.ItemAdjInvoiceItem)1 Payment (org.killbill.billing.payment.api.Payment)1