修改消息后台的启动和停止文件并整理文档

This commit is contained in:
not-like-juvenile
2026-03-11 16:42:33 +08:00
parent e67016a6f4
commit 9cc6dcc2a6
19 changed files with 327 additions and 375 deletions

View File

@@ -31,19 +31,26 @@ const fetchImpl = (globalThis.fetch ? globalThis.fetch.bind(globalThis) : (() =>
const SUPA_URL = (process.env.SUPA_URL || process.env.SUPA_URL_OVERRIDE || '').replace(/\/$/, '')
// Prefer service role key when present (server-side), to avoid RLS issues.
const SUPA_KEY = process.env.SERVICE_ROLE_KEY || process.env.SUPA_KEY || ''
const SUPA_USE_BEARER = (process.env.SUPA_USE_BEARER === 'true')
// notify-worker needs to read ml_orders which is often protected by RLS.
// To avoid changing behavior of other services, allow a worker-specific toggle.
const SUPA_USE_BEARER = (
process.env.NOTIFY_WORKER_SUPA_USE_BEARER ||
process.env.WORKER_SUPA_USE_BEARER ||
process.env.SUPA_USE_BEARER ||
'false'
) === 'true'
const POLL_MS = Number(process.env.NOTIFY_WORKER_POLL_MS || process.env.WORKER_POLL_MS || 2000)
const BATCH_SIZE = Number(process.env.NOTIFY_WORKER_BATCH_SIZE || process.env.WORKER_BATCH_SIZE || 20)
const RUN_ONCE = (process.env.RUN_ONCE === 'true' || process.env.RUN_ONCE === '1')
function supaFetch(restPath, opts = {}) {
function supaFetch(restPath, opts = {}, useBearer = false) {
const url = `${SUPA_URL}/rest/v1/${restPath}`
const headers = Object.assign({}, opts.headers || {}, {
apikey: SUPA_KEY,
Accept: 'application/json'
})
if (SUPA_USE_BEARER) headers.Authorization = `Bearer ${SUPA_KEY}`
if (useBearer) headers.Authorization = `Bearer ${SUPA_KEY}`
return fetchImpl(url, Object.assign({}, opts, { headers }))
}
@@ -58,7 +65,7 @@ function buildMessageId({ aud, waybill_id, dedupe_key }) {
async function fetchPendingQueue(limit) {
const q = `notify_queue?processed_at=is.null&order=created_at.asc&limit=${encodeURIComponent(String(limit))}`
const resp = await supaFetch(q, { method: 'GET' })
const resp = await supaFetch(q, { method: 'GET' }, false)
if (!resp.ok) {
const txt = await resp.text().catch(() => '')
throw new Error(`fetch notify_queue failed: HTTP ${resp.status} ${txt}`)
@@ -67,7 +74,11 @@ async function fetchPendingQueue(limit) {
}
async function fetchWaybill(waybillId) {
const resp = await supaFetch(`platform_express_waybills?id=eq.${encodeURIComponent(waybillId)}&select=id,order_id,order_no,carrier,tracking_no`, { method: 'GET' })
const resp = await supaFetch(
`platform_express_waybills?id=eq.${encodeURIComponent(waybillId)}&select=id,order_id,order_no,carrier,tracking_no`,
{ method: 'GET' },
false
)
if (!resp.ok) {
const txt = await resp.text().catch(() => '')
throw new Error(`fetch waybill failed: HTTP ${resp.status} ${txt}`)
@@ -77,7 +88,11 @@ async function fetchWaybill(waybillId) {
}
async function fetchOrderById(orderId) {
const resp = await supaFetch(`ml_orders?id=eq.${encodeURIComponent(orderId)}&select=id,order_no,user_id,merchant_id`, { method: 'GET' })
const resp = await supaFetch(
`ml_orders?id=eq.${encodeURIComponent(orderId)}&select=id,order_no,user_id,merchant_id`,
{ method: 'GET' },
SUPA_USE_BEARER
)
if (!resp.ok) {
const txt = await resp.text().catch(() => '')
throw new Error(`fetch order (id) failed: HTTP ${resp.status} ${txt}`)
@@ -87,7 +102,11 @@ async function fetchOrderById(orderId) {
}
async function fetchOrderByNo(orderNo) {
const resp = await supaFetch(`ml_orders?order_no=eq.${encodeURIComponent(orderNo)}&select=id,order_no,user_id,merchant_id`, { method: 'GET' })
const resp = await supaFetch(
`ml_orders?order_no=eq.${encodeURIComponent(orderNo)}&select=id,order_no,user_id,merchant_id`,
{ method: 'GET' },
SUPA_USE_BEARER
)
if (!resp.ok) {
const txt = await resp.text().catch(() => '')
throw new Error(`fetch order (order_no) failed: HTTP ${resp.status} ${txt}`)