View Source PhoenixAnalytics.Services.Batcher (PhoenixAnalytics v0.3.2)
A GenServer module for batching and inserting RequestLog entries into the database.
This module provides functionality to efficiently insert RequestLog entries by batching them and inserting them in bulk. It uses a GenServer to manage the state of the batch and periodically flush the batch to the database.
The batch is processed and inserted into the database either when it reaches 1_000 logs or after 1 second, whichever comes first.
Usage
To insert a RequestLog:
PhoenixAnalytics.Services.Batcher.insert(%PhoenixAnalytics.Entities.RequestLog{})
Alternatively, you can send an event to PubSub for request_log insertion:
PhoenixAnalytics.Services.PubSub.broadcast(:request_sent, %PhoenixAnalytics.Entities.RequestLog{})
The module will handle batching and inserting the logs automatically. This approach allows for handling distributed app scenarios.
Summary
Functions
Returns a specification to start this module under a supervisor.
Inserts a RequestLog into the batch queue. The batch is processed and inserted into the database either when it reaches 1_000 logs or after 1 second, whichever comes first.
Sends a batch of RequestLogs to be inserted into the database.
Functions
Returns a specification to start this module under a supervisor.
See Supervisor
.
Inserts a RequestLog into the batch queue. The batch is processed and inserted into the database either when it reaches 1_000 logs or after 1 second, whichever comes first.
Parameters
- request_log: A PhoenixAnalytics.Entities.RequestLog struct to be inserted.
Examples
iex> PhoenixAnalytics.Services.Batcher.insert(%PhoenixAnalytics.Entities.RequestLog{})
Note: You can also use PubSub to insert a RequestLog, which is useful for distributed app scenarios:
iex> PhoenixAnalytics.Services.PubSub.broadcast(:request_sent, %PhoenixAnalytics.Entities.RequestLog{})
Sends a batch of RequestLogs to be inserted into the database.
Parameters
- batch: A list of PhoenixAnalytics.Entities.RequestLog structs to be inserted.
Examples
iex> PhoenixAnalytics.Services.Batcher.send_batch([%PhoenixAnalytics.Entities.RequestLog{}, %PhoenixAnalytics.Entities.RequestLog{}])