Search in sources :

Example 1 with CancellationException

use of com.microsoft.appcenter.CancellationException in project mobile-center-sdk-android by Microsoft.

the class DefaultChannel method enqueue.

/**
 * Actual implementation of enqueue logic. Will increase counters, triggers of batching logic.
 *
 * @param log       the Log to be enqueued
 * @param groupName the queue to use
 */
@Override
public synchronized void enqueue(@NonNull Log log, @NonNull final String groupName) {
    /* Check group name is registered. */
    final GroupState groupState = mGroupStates.get(groupName);
    if (groupState == null) {
        AppCenterLog.error(LOG_TAG, "Invalid group name:" + groupName);
        return;
    }
    /* Check if disabled with discarding logs. */
    if (mDiscardLogs) {
        AppCenterLog.warn(LOG_TAG, "Channel is disabled, log are discarded.");
        if (groupState.mListener != null) {
            groupState.mListener.onBeforeSending(log);
            groupState.mListener.onFailure(log, new CancellationException());
        }
        return;
    }
    /* Call listeners so that they can decorate the log. */
    for (Listener listener : mListeners) {
        listener.onEnqueuingLog(log, groupName);
    }
    /* Attach device properties to every log if its not already attached by a service. */
    if (log.getDevice() == null) {
        /* Generate device properties only once per process life time. */
        if (mDevice == null) {
            try {
                mDevice = DeviceInfoHelper.getDeviceInfo(mContext);
            } catch (DeviceInfoHelper.DeviceInfoException e) {
                AppCenterLog.error(LOG_TAG, "Device log cannot be generated", e);
                return;
            }
        }
        /* Attach device properties. */
        log.setDevice(mDevice);
    }
    /* Set date to current if not explicitly set in the past by a module (such as a crash). */
    if (log.getTimestamp() == null) {
        log.setTimestamp(new Date());
    }
    /* Call listeners so that they can filter the log. */
    boolean filteredOut = false;
    for (Listener listener : mListeners) {
        filteredOut = filteredOut || listener.shouldFilter(log);
    }
    /* Persist log if not filtered out. */
    if (filteredOut) {
        AppCenterLog.debug(LOG_TAG, "Log of type '" + log.getType() + "' was filtered out by listener(s)");
    } else {
        try {
            /* Increment counters and schedule ingestion if we are enabled. */
            mPersistence.putLog(groupName, log);
            groupState.mPendingLogCount++;
            AppCenterLog.debug(LOG_TAG, "enqueue(" + groupState.mName + ") pendingLogCount=" + groupState.mPendingLogCount);
            if (mEnabled) {
                checkPendingLogs(groupState.mName);
            } else {
                AppCenterLog.warn(LOG_TAG, "Channel is temporarily disabled, log was saved to disk.");
            }
        } catch (Persistence.PersistenceException e) {
            AppCenterLog.error(LOG_TAG, "Error persisting log with exception: " + e.toString());
        }
    }
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) DatabasePersistence(com.microsoft.appcenter.persistence.DatabasePersistence) CancellationException(com.microsoft.appcenter.CancellationException) DeviceInfoHelper(com.microsoft.appcenter.utils.DeviceInfoHelper) Date(java.util.Date)

Example 2 with CancellationException

use of com.microsoft.appcenter.CancellationException in project AppCenter-SDK-Android by Microsoft.

the class DefaultChannelRaceConditionTest method disabledWhileHandlingIngestionSuccess.

@Test
public void disabledWhileHandlingIngestionSuccess() throws Exception {
    /* Set up mocking. */
    final Semaphore beforeCallSemaphore = new Semaphore(0);
    final Semaphore afterCallSemaphore = new Semaphore(0);
    Persistence mockPersistence = mock(Persistence.class);
    when(mockPersistence.countLogs(anyString())).thenReturn(1);
    when(mockPersistence.getLogs(anyString(), eq(1), anyListOf(Log.class))).then(getGetLogsAnswer(1));
    when(mockPersistence.getLogs(anyString(), eq(CLEAR_BATCH_SIZE), anyListOf(Log.class))).then(getGetLogsAnswer(0));
    IngestionHttp mockIngestion = mock(IngestionHttp.class);
    when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(new Answer<Object>() {

        @Override
        public Object answer(final InvocationOnMock invocation) throws Throwable {
            new Thread() {

                @Override
                public void run() {
                    beforeCallSemaphore.acquireUninterruptibly();
                    ((ServiceCallback) invocation.getArguments()[3]).onCallSucceeded("");
                    afterCallSemaphore.release();
                }
            }.start();
            return mock(ServiceCall.class);
        }
    });
    /* Simulate enable module then disable. */
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion, mCoreHandler);
    Channel.GroupListener listener = mock(Channel.GroupListener.class);
    channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, listener);
    channel.setEnabled(false);
    channel.setEnabled(true);
    /* Release call to mock ingestion. */
    beforeCallSemaphore.release();
    /* Wait for callback ingestion. */
    afterCallSemaphore.acquireUninterruptibly();
    /* Verify handling success was ignored. */
    verify(listener, never()).onSuccess(any(Log.class));
    verify(listener).onFailure(any(Log.class), argThat(new ArgumentMatcher<Exception>() {

        @Override
        public boolean matches(Object argument) {
            return argument instanceof CancellationException;
        }
    }));
}
Also used : Context(android.content.Context) ServiceCall(com.microsoft.appcenter.http.ServiceCall) Log(com.microsoft.appcenter.ingestion.models.Log) Semaphore(java.util.concurrent.Semaphore) Persistence(com.microsoft.appcenter.persistence.Persistence) IngestionHttp(com.microsoft.appcenter.ingestion.IngestionHttp) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) CancellationException(com.microsoft.appcenter.CancellationException) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ArgumentMatcher(org.mockito.ArgumentMatcher) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Example 3 with CancellationException

use of com.microsoft.appcenter.CancellationException in project AppCenter-SDK-Android by Microsoft.

the class DefaultChannel method enqueue.

/**
 * Actual implementation of enqueue logic. Will increase counters, triggers of batching logic.
 *
 * @param log       the Log to be enqueued
 * @param groupName the queue to use
 */
@Override
public synchronized void enqueue(@NonNull Log log, @NonNull final String groupName) {
    /* Check group name is registered. */
    final GroupState groupState = mGroupStates.get(groupName);
    if (groupState == null) {
        AppCenterLog.error(LOG_TAG, "Invalid group name:" + groupName);
        return;
    }
    /* Check if disabled with discarding logs. */
    if (mDiscardLogs) {
        AppCenterLog.warn(LOG_TAG, "Channel is disabled, log are discarded.");
        if (groupState.mListener != null) {
            groupState.mListener.onBeforeSending(log);
            groupState.mListener.onFailure(log, new CancellationException());
        }
        return;
    }
    /* Call listeners so that they can decorate the log. */
    for (Listener listener : mListeners) {
        listener.onEnqueuingLog(log, groupName);
    }
    /* Attach device properties to every log if its not already attached by a service. */
    if (log.getDevice() == null) {
        /* Generate device properties only once per process life time. */
        if (mDevice == null) {
            try {
                mDevice = DeviceInfoHelper.getDeviceInfo(mContext);
            } catch (DeviceInfoHelper.DeviceInfoException e) {
                AppCenterLog.error(LOG_TAG, "Device log cannot be generated", e);
                return;
            }
        }
        /* Attach device properties. */
        log.setDevice(mDevice);
    }
    /* Set date to current if not explicitly set in the past by a module (such as a crash). */
    if (log.getTimestamp() == null) {
        log.setTimestamp(new Date());
    }
    /* Call listeners so that they can filter the log. */
    boolean filteredOut = false;
    for (Listener listener : mListeners) {
        filteredOut = filteredOut || listener.shouldFilter(log);
    }
    /* Persist log if not filtered out. */
    if (filteredOut) {
        AppCenterLog.debug(LOG_TAG, "Log of type '" + log.getType() + "' was filtered out by listener(s)");
    } else {
        try {
            /* Increment counters and schedule ingestion if we are enabled. */
            mPersistence.putLog(groupName, log);
            groupState.mPendingLogCount++;
            AppCenterLog.debug(LOG_TAG, "enqueue(" + groupState.mName + ") pendingLogCount=" + groupState.mPendingLogCount);
            if (mEnabled) {
                checkPendingLogs(groupState.mName);
            } else {
                AppCenterLog.warn(LOG_TAG, "Channel is temporarily disabled, log was saved to disk.");
            }
        } catch (Persistence.PersistenceException e) {
            AppCenterLog.error(LOG_TAG, "Error persisting log with exception: " + e.toString());
        }
    }
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) DatabasePersistence(com.microsoft.appcenter.persistence.DatabasePersistence) CancellationException(com.microsoft.appcenter.CancellationException) DeviceInfoHelper(com.microsoft.appcenter.utils.DeviceInfoHelper) Date(java.util.Date)

Example 4 with CancellationException

use of com.microsoft.appcenter.CancellationException in project AppCenter-SDK-Android by Microsoft.

the class DefaultChannel method deleteLogsOnSuspended.

private void deleteLogsOnSuspended(final GroupState groupState) {
    final List<Log> logs = new ArrayList<>();
    mPersistence.getLogs(groupState.mName, CLEAR_BATCH_SIZE, logs);
    if (logs.size() > 0 && groupState.mListener != null) {
        for (Log log : logs) {
            groupState.mListener.onBeforeSending(log);
            groupState.mListener.onFailure(log, new CancellationException());
        }
    }
    if (logs.size() >= CLEAR_BATCH_SIZE && groupState.mListener != null) {
        deleteLogsOnSuspended(groupState);
    } else {
        mPersistence.deleteLogs(groupState.mName);
    }
}
Also used : AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) Log(com.microsoft.appcenter.ingestion.models.Log) CancellationException(com.microsoft.appcenter.CancellationException) ArrayList(java.util.ArrayList)

Example 5 with CancellationException

use of com.microsoft.appcenter.CancellationException in project mobile-center-sdk-android by Microsoft.

the class DefaultChannel method enqueue.

@Override
public void enqueue(@NonNull Log log, @NonNull final String groupName, int flags) {
    /* Check group name is registered. */
    GroupState groupState = mGroupStates.get(groupName);
    if (groupState == null) {
        AppCenterLog.error(LOG_TAG, "Invalid group name:" + groupName);
        return;
    }
    /* Check if disabled with discarding logs. */
    if (mDiscardLogs) {
        AppCenterLog.warn(LOG_TAG, "Channel is disabled, the log is discarded.");
        if (groupState.mListener != null) {
            groupState.mListener.onBeforeSending(log);
            groupState.mListener.onFailure(log, new CancellationException());
        }
        return;
    }
    /* Call listeners so that they can decorate the log. */
    for (Listener listener : mListeners) {
        listener.onPreparingLog(log, groupName);
    }
    /* Attach device properties to every log if its not already attached by a service. */
    if (log.getDevice() == null) {
        /* Generate device properties only once per process life time. */
        if (mDevice == null) {
            try {
                mDevice = DeviceInfoHelper.getDeviceInfo(mContext);
            } catch (DeviceInfoHelper.DeviceInfoException e) {
                AppCenterLog.error(LOG_TAG, "Device log cannot be generated", e);
                return;
            }
        }
        /* Attach device properties. */
        log.setDevice(mDevice);
    }
    /* Set date to current if not explicitly set in the past by a module (such as a crash). */
    if (log.getTimestamp() == null) {
        log.setTimestamp(new Date());
    }
    /* Notify listeners that log is prepared and is in a final state. */
    for (Listener listener : mListeners) {
        listener.onPreparedLog(log, groupName, flags);
    }
    /* Call listeners so that they can filter the log. */
    boolean filteredOut = false;
    for (Listener listener : mListeners) {
        filteredOut = filteredOut || listener.shouldFilter(log);
    }
    /* If filtered out, nothing more to do. */
    if (filteredOut) {
        AppCenterLog.debug(LOG_TAG, "Log of type '" + log.getType() + "' was filtered out by listener(s)");
    } else {
        if (mAppSecret == null && groupState.mIngestion == mIngestion) {
            /* Log was not filtered out but no app secret has been provided. Do nothing in this case. */
            AppCenterLog.debug(LOG_TAG, "Log of type '" + log.getType() + "' was not filtered out by listener(s) but no app secret was provided. Not persisting/sending the log.");
            return;
        }
        try {
            /* Persist log. */
            mPersistence.putLog(log, groupName, flags);
        } catch (Persistence.PersistenceException e) {
            AppCenterLog.error(LOG_TAG, "Error persisting log", e);
            if (groupState.mListener != null) {
                groupState.mListener.onBeforeSending(log);
                groupState.mListener.onFailure(log, e);
            }
            return;
        }
        /* Nothing more to do if the log is from a paused transmission target. */
        Iterator<String> targetKeys = log.getTransmissionTargetTokens().iterator();
        String targetKey = targetKeys.hasNext() ? PartAUtils.getTargetKey(targetKeys.next()) : null;
        if (groupState.mPausedTargetKeys.contains(targetKey)) {
            AppCenterLog.debug(LOG_TAG, "Transmission target ikey=" + targetKey + " is paused.");
            return;
        }
        /* Increment counters and schedule ingestion if we are enabled. */
        groupState.mPendingLogCount++;
        AppCenterLog.debug(LOG_TAG, "enqueue(" + groupState.mName + ") pendingLogCount=" + groupState.mPendingLogCount);
        if (mEnabled) {
            checkPendingLogs(groupState);
        } else {
            AppCenterLog.debug(LOG_TAG, "Channel is temporarily disabled, log was saved to disk.");
        }
    }
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) DatabasePersistence(com.microsoft.appcenter.persistence.DatabasePersistence) CancellationException(com.microsoft.appcenter.CancellationException) DeviceInfoHelper(com.microsoft.appcenter.utils.DeviceInfoHelper) Date(java.util.Date)

Aggregations

CancellationException (com.microsoft.appcenter.CancellationException)13 Persistence (com.microsoft.appcenter.persistence.Persistence)7 Log (com.microsoft.appcenter.ingestion.models.Log)6 Context (android.content.Context)4 ServiceCall (com.microsoft.appcenter.http.ServiceCall)4 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)4 LogContainer (com.microsoft.appcenter.ingestion.models.LogContainer)4 UUID (java.util.UUID)4 Semaphore (java.util.concurrent.Semaphore)4 Test (org.junit.Test)4 ArgumentMatcher (org.mockito.ArgumentMatcher)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 AppCenterIngestion (com.microsoft.appcenter.ingestion.AppCenterIngestion)3 DatabasePersistence (com.microsoft.appcenter.persistence.DatabasePersistence)3 DeviceInfoHelper (com.microsoft.appcenter.utils.DeviceInfoHelper)3 Date (java.util.Date)3 IngestionHttp (com.microsoft.appcenter.ingestion.IngestionHttp)2 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2