Skip to content

Commit

Permalink
fix: db insert error for no space left
Browse files Browse the repository at this point in the history
  • Loading branch information
zhu-xiaowei committed Aug 22, 2024
1 parent 0549134 commit e0ab694
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.amazonaws.logging.Log;
import com.amazonaws.logging.LogFactory;
import java.util.concurrent.Executors;
import software.aws.solution.clickstream.client.db.ClickstreamDBUtil;
import software.aws.solution.clickstream.client.db.EventTable;
import software.aws.solution.clickstream.client.network.NetRequest;
Expand Down Expand Up @@ -101,6 +102,7 @@ public Uri recordEvent(@NonNull final AnalyticsEvent event) {
}
} else {
LOG.error(String.format("Error to save event with EventType: %s", event.getEventType()));
sendEventImmediately(event);
}
return uri;
}
Expand Down Expand Up @@ -207,5 +209,17 @@ String[] getBatchOfEvents(final Cursor cursor) {

return new String[] {eventBuilder.toString(), lastEventId};
}

public void sendEventImmediately(AnalyticsEvent event) {
Runnable task = () -> {
NetRequest.uploadEvents("[" + event.toJSONObject().toString() + "]",
clickstreamContext.getClickstreamConfiguration(),
bundleSequenceId);
bundleSequenceId += 1;
clickstreamContext.getSystem().getPreferences()
.putInt(KEY_BUNDLE_SEQUENCE_ID_PREF, bundleSequenceId);
};
Executors.newSingleThreadExecutor().execute(task);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.net.Uri;

import com.amazonaws.logging.Log;
import com.amazonaws.logging.LogFactory;
import software.aws.solution.clickstream.client.AnalyticsEvent;
import software.aws.solution.clickstream.client.EventRecorder;

/**
* Clickstream Database Util.
Expand All @@ -31,6 +35,8 @@ public class ClickstreamDBUtil {
*/
private ClickstreamDBBase clickstreamDBBase;

private static final Log LOG = LogFactory.getLog(EventRecorder.class);

/**
* Constructs a ClickstreamDBUtil with the given Context.
*
Expand Down Expand Up @@ -58,7 +64,13 @@ public void closeDB() {
* @return An Uri of the record inserted.
*/
public Uri saveEvent(final AnalyticsEvent event) {
return clickstreamDBBase.insert(clickstreamDBBase.getContentUri(), generateContentValuesFromEvent(event));
Uri uri = null;
try {
uri = clickstreamDBBase.insert(clickstreamDBBase.getContentUri(), generateContentValuesFromEvent(event));
} catch (SQLException e) {
LOG.info("SQLException: " + e.getMessage());
}
return uri;
}

private ContentValues generateContentValuesFromEvent(final AnalyticsEvent event) {
Expand Down

0 comments on commit e0ab694

Please sign in to comment.