Automatic Audience Refresh
Overview
An "automated" audience is defined by filter conditions rather than a fixed member list, so its membership must be recomputed periodically to stay current.
The filter-builder logic lives in cms-api, which is where the actual computation happens. This worker acts purely as a durable dispatcher: it consumes a message from the queue and issues an HTTP call to an internal cms-api endpoint.
That split buys two things. RabbitMQ's retry and DLQ semantics come for free, and a computation that may run for several minutes never ties up a user-facing HTTP request.
Business Flow
- Receive an
AudienceRefreshPayloadcontainingaudienceIdplus whatever other fields the producer supplied. - Read the base URL from
CMS_API_BASE_URL(falling back tohttp://localhost:3000) and the key fromINTERNAL_API_KEY. - Issue a
POSTto/api/audiences-filter/internal/refreshon cms-api.- Headers:
x-internal-keycarrying the value ofINTERNAL_API_KEY, andContent-Type: application/json. - Body: the entire payload, forwarded verbatim.
- Timeout: 5 minutes, matching the legacy axios timeout so that large audiences still complete.
- Headers:
- On a 2xx response, log success and ack the message.
- On a transport error or a non-2xx status, log the failure and return an error so the mq layer decides what happens next — transient failures such as 5xx or timeouts are retried at 1s, 4s, and 9s before the message lands in the DLQ.
Key Files & Functions
internal/audience/refresh_consumer.goRefreshConsumer.OnAudienceRefresh(ctx, body)— the main handlerNewRefreshConsumer(cfg, log)andRegister(reg, cfg)- Constants:
refreshPathis/api/audiences-filter/internal/refresh,refreshTimeoutis 5 minutes logRefreshFailed()
internal/audience/payloads.go— definesAudienceRefreshPayloadcmd/worker/main.go— registers the consumer inrunMain()viaaudience.NewRefreshConsumer(cfg, log).Register(reg, cfg)- Queue:
audience_refresh(runtime profilemain)
Connections to Other Services
- Job source — cms-api-go, or a CMS-side cron, whenever an audience needs its membership recalculated.
- Outbound call — the internal cms-api-go endpoint
POST /api/audiences-filter/internal/refresh, protected by thex-internal-keyheader. - Environment variables —
CMS_API_BASE_URLandINTERNAL_API_KEY. - No direct database or LINE API access — this is a pure forwarding consumer.
- Eventual result —
line_user.audience_idsandaudience.infoare updated by cms-api itself.