Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Price change fixes #71

Merged
merged 3 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

- fixed: Do not exit the price-change daemon when a rate is missing
- fixed: Make the rates backend configurable.
- fixed: Send a NACK when we fail to deliver a message.

## 2.4.0 (2024-12-16)

- added: Validate deviceTokens with a RegExp.
Expand Down
2 changes: 1 addition & 1 deletion src/daemons/priceChangeDaemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ runDaemon(async tools => {

// Grab the price:
const price = await tools.rates.getRate(currencyPair, now)
if (price == null) return
if (price == null) continue

// Check the triggers:
await checkPriceChange(tools, eventRow, now, price, 'day')
Expand Down
8 changes: 6 additions & 2 deletions src/daemons/publishDaemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ runDaemon(async tools => {
const consumer = await connections.queue.subscribe(
{ noAck: false },
message => {
handleMessage(connections, message).catch(error => {
handleMessage(connections, message).catch(async error => {
const nackStatus = await message.nack(true).then(
() => '',
() => ', nack failed'
)
logger.warn({
msg: 'Failed to handle message',
msg: 'Failed to handle message' + nackStatus,
body: message.bodyToString(),
error
})
Expand Down
1 change: 1 addition & 0 deletions src/db/couchSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const asSettings = asObject({
),
[]
),
ratesServer: asMaybe(asString, 'https://rates2.edge.app'),

// Mode toggles:
debugLogs: asMaybe(asBoolean, false),
Expand Down
6 changes: 4 additions & 2 deletions src/util/ratesCache.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { asEither, asNull, asObject, asString } from 'cleaners'
import fetch from 'node-fetch'

import { syncedSettings } from '../db/couchSettings'

export interface RatesCache {
getRate: (currencyPair: string, date: Date) => Promise<number | null>
trimCache: (before: Date) => void
}

const RATES_SERVER = 'https://rates2.edge.app'
const ROUNDING = 5 * 60 * 1000 // 5 minutes

export function makeRatesCache(): RatesCache {
Expand Down Expand Up @@ -49,8 +50,9 @@ async function getFromServer(
currencyPair: string,
date: Date
): Promise<number | null> {
const { ratesServer } = syncedSettings.doc
const response = await fetch(
`${RATES_SERVER}/v2/exchangeRate?currency_pair=${currencyPair}&date=${date.toISOString()}`
`${ratesServer}/v2/exchangeRate?currency_pair=${currencyPair}&date=${date.toISOString()}`
)
const json = await response.json()
const { exchangeRate } = asRateReply(json)
Expand Down
Loading