You've already forked AstralRinth
forked from didirus/AstralRinth
Adjust some values in tax-related tasks (#4598)
* Adjust some values for tax processing * chore: query cache, clippy, fmt
This commit is contained in:
committed by
GitHub
parent
a4015d9df3
commit
f375913c62
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"db_name": "PostgreSQL",
|
"db_name": "PostgreSQL",
|
||||||
"query": "\n SELECT\n charges.id, charges.user_id, charges.price_id, charges.amount, charges.currency_code, charges.status, charges.due, charges.last_attempt,\n charges.charge_type, charges.subscription_id, charges.tax_amount, charges.tax_platform_id,\n -- Workaround for https://github.com/launchbadge/sqlx/issues/3336\n charges.subscription_interval AS \"subscription_interval?\",\n charges.payment_platform,\n charges.payment_platform_id AS \"payment_platform_id?\",\n charges.parent_charge_id AS \"parent_charge_id?\",\n charges.net AS \"net?\",\n\t\t\t\tcharges.tax_last_updated AS \"tax_last_updated?\",\n\t\t\t\tcharges.tax_drift_loss AS \"tax_drift_loss?\",\n charges.tax_transaction_version AS \"tax_transaction_version?\",\n charges.tax_platform_accounting_time AS \"tax_platform_accounting_time?\"\n FROM charges\n \n\t\t\tINNER JOIN users u ON u.id = charges.user_id\n\t\t\tWHERE\n\t\t\t status = 'open'\n\t\t\t AND COALESCE(tax_last_updated, '-infinity' :: TIMESTAMPTZ) < NOW() - INTERVAL '1 day'\n\t\t\t AND u.email IS NOT NULL\n\t\t\t AND due - INTERVAL '7 days' > NOW()\n AND due - INTERVAL '14 days' < NOW() -- Due between 7 and 14 days from now\n\t\t\tORDER BY COALESCE(tax_last_updated, '-infinity' :: TIMESTAMPTZ) ASC\n\t\t\tFOR NO KEY UPDATE SKIP LOCKED\n\t\t\tLIMIT $1\n\t\t\t",
|
"query": "\n SELECT\n charges.id, charges.user_id, charges.price_id, charges.amount, charges.currency_code, charges.status, charges.due, charges.last_attempt,\n charges.charge_type, charges.subscription_id, charges.tax_amount, charges.tax_platform_id,\n -- Workaround for https://github.com/launchbadge/sqlx/issues/3336\n charges.subscription_interval AS \"subscription_interval?\",\n charges.payment_platform,\n charges.payment_platform_id AS \"payment_platform_id?\",\n charges.parent_charge_id AS \"parent_charge_id?\",\n charges.net AS \"net?\",\n\t\t\t\tcharges.tax_last_updated AS \"tax_last_updated?\",\n\t\t\t\tcharges.tax_drift_loss AS \"tax_drift_loss?\",\n charges.tax_transaction_version AS \"tax_transaction_version?\",\n charges.tax_platform_accounting_time AS \"tax_platform_accounting_time?\"\n FROM charges\n \n\t\t\tINNER JOIN users u ON u.id = charges.user_id\n\t\t\tWHERE\n\t\t\t status = 'open'\n\t\t\t AND COALESCE(tax_last_updated, '-infinity' :: TIMESTAMPTZ) < NOW() - INTERVAL '1 day'\n\t\t\t AND u.email IS NOT NULL\n\t\t\t AND due - INTERVAL '7 days' > NOW()\n AND due - INTERVAL '30 days' < NOW() -- Due between 7 and 30 days from now\n\t\t\tORDER BY COALESCE(tax_last_updated, '-infinity' :: TIMESTAMPTZ) ASC\n\t\t\tFOR NO KEY UPDATE SKIP LOCKED\n\t\t\tLIMIT $1\n\t\t\t",
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [
|
"columns": [
|
||||||
{
|
{
|
||||||
@@ -138,5 +138,5 @@
|
|||||||
true
|
true
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"hash": "ce23f89106ef7b34f5a935f6e792d87b8805e87ac0cefb43828fc6d3aca52399"
|
"hash": "7d8de27065490edc560b0c81061295ca82f44546527e1a31c03e5bb7a07c1e63"
|
||||||
}
|
}
|
||||||
@@ -338,7 +338,7 @@ impl DBCharge {
|
|||||||
AND COALESCE(tax_last_updated, '-infinity' :: TIMESTAMPTZ) < NOW() - INTERVAL '1 day'
|
AND COALESCE(tax_last_updated, '-infinity' :: TIMESTAMPTZ) < NOW() - INTERVAL '1 day'
|
||||||
AND u.email IS NOT NULL
|
AND u.email IS NOT NULL
|
||||||
AND due - INTERVAL '7 days' > NOW()
|
AND due - INTERVAL '7 days' > NOW()
|
||||||
AND due - INTERVAL '14 days' < NOW() -- Due between 7 and 14 days from now
|
AND due - INTERVAL '30 days' < NOW() -- Due between 7 and 30 days from now
|
||||||
ORDER BY COALESCE(tax_last_updated, '-infinity' :: TIMESTAMPTZ) ASC
|
ORDER BY COALESCE(tax_last_updated, '-infinity' :: TIMESTAMPTZ) ASC
|
||||||
FOR NO KEY UPDATE SKIP LOCKED
|
FOR NO KEY UPDATE SKIP LOCKED
|
||||||
LIMIT $1
|
LIMIT $1
|
||||||
|
|||||||
@@ -35,9 +35,6 @@ use tracing::{debug, error, info, warn};
|
|||||||
|
|
||||||
/// Updates charges which need to have their tax amount updated. This is done within a timer to avoid reaching
|
/// Updates charges which need to have their tax amount updated. This is done within a timer to avoid reaching
|
||||||
/// Anrok API limits.
|
/// Anrok API limits.
|
||||||
///
|
|
||||||
/// The global rate limit for Anrok API operations is 10 RPS, so we run ~8 requests every second up
|
|
||||||
/// to the specified limit of processed charges.
|
|
||||||
async fn update_tax_amounts(
|
async fn update_tax_amounts(
|
||||||
pg: &PgPool,
|
pg: &PgPool,
|
||||||
redis: &RedisPool,
|
redis: &RedisPool,
|
||||||
@@ -45,17 +42,12 @@ async fn update_tax_amounts(
|
|||||||
stripe_client: &stripe::Client,
|
stripe_client: &stripe::Client,
|
||||||
limit: i64,
|
limit: i64,
|
||||||
) -> Result<(), ApiError> {
|
) -> Result<(), ApiError> {
|
||||||
let mut interval = tokio::time::interval(std::time::Duration::from_secs(1));
|
|
||||||
interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
|
||||||
|
|
||||||
let mut processed_charges = 0;
|
let mut processed_charges = 0;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
interval.tick().await;
|
|
||||||
|
|
||||||
let mut txn = pg.begin().await?;
|
let mut txn = pg.begin().await?;
|
||||||
|
|
||||||
let charges = DBCharge::get_updateable_lock(&mut *txn, 8).await?;
|
let charges = DBCharge::get_updateable_lock(&mut *txn, 5).await?;
|
||||||
|
|
||||||
if charges.is_empty() {
|
if charges.is_empty() {
|
||||||
info!("No more charges to process");
|
info!("No more charges to process");
|
||||||
@@ -1023,14 +1015,14 @@ pub async fn index_subscriptions(
|
|||||||
&redis,
|
&redis,
|
||||||
&anrok_client,
|
&anrok_client,
|
||||||
&stripe_client,
|
&stripe_client,
|
||||||
1000,
|
500,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
run_and_time(
|
run_and_time(
|
||||||
"update_tax_amounts",
|
"update_tax_amounts",
|
||||||
update_tax_amounts(&pool, &redis, &anrok_client, &stripe_client, 50),
|
update_tax_amounts(&pool, &redis, &anrok_client, &stripe_client, 500),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user