Starting to resolve the issue on prediction3
This commit is contained in:
parent
20a26c88df
commit
844d1d62d5
@ -464,6 +464,13 @@ export async function processFirstDepositReferralCommissions(input: {
|
|||||||
* Pays immediately to the referrer bonus wallet after Sybil / caps / pool checks.
|
* Pays immediately to the referrer bonus wallet after Sybil / caps / pool checks.
|
||||||
* Test stakes are excluded. Deposit/withdrawal commission modes are unchanged.
|
* Test stakes are excluded. Deposit/withdrawal commission modes are unchanged.
|
||||||
*/
|
*/
|
||||||
|
function logPredictionCommissionSkip(
|
||||||
|
reason: string,
|
||||||
|
details: Record<string, string | number | boolean | null | undefined>
|
||||||
|
): void {
|
||||||
|
console.warn('[referral] prediction commission skipped:', reason, details);
|
||||||
|
}
|
||||||
|
|
||||||
export async function processPredictionReferralCommissions(input: {
|
export async function processPredictionReferralCommissions(input: {
|
||||||
betId: string;
|
betId: string;
|
||||||
referredUserId: string;
|
referredUserId: string;
|
||||||
@ -473,28 +480,70 @@ export async function processPredictionReferralCommissions(input: {
|
|||||||
/** Terminal bet outcome; neutral/refund must not call this. */
|
/** Terminal bet outcome; neutral/refund must not call this. */
|
||||||
betOutcome: 'won' | 'lost';
|
betOutcome: 'won' | 'lost';
|
||||||
}): Promise<void> {
|
}): Promise<void> {
|
||||||
if (input.betOutcome !== 'won' && input.betOutcome !== 'lost') return;
|
if (input.betOutcome !== 'won' && input.betOutcome !== 'lost') {
|
||||||
if (input.stakeAccountKind === 'test') return;
|
logPredictionCommissionSkip('invalid_outcome', {
|
||||||
if (input.stakeRaw <= 0n) return;
|
betId: input.betId,
|
||||||
|
betOutcome: input.betOutcome,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (input.stakeAccountKind === 'test') {
|
||||||
|
logPredictionCommissionSkip('test_stake', { betId: input.betId });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (input.stakeRaw <= 0n) {
|
||||||
|
logPredictionCommissionSkip('non_positive_stake', { betId: input.betId });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const settings = await referralRepository.getSettings();
|
const settings = await referralRepository.getSettings();
|
||||||
if (!settings.enabled || !settings.prediction_commissions_enabled) return;
|
if (!settings.enabled || !settings.prediction_commissions_enabled) {
|
||||||
|
logPredictionCommissionSkip('program_or_mode_disabled', {
|
||||||
|
betId: input.betId,
|
||||||
|
enabled: settings.enabled,
|
||||||
|
predictionCommissionsEnabled: settings.prediction_commissions_enabled,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const userRows = await query<Array<{ referred_by_user_id: string | null }>>(
|
const userRows = await query<Array<{ referred_by_user_id: string | null }>>(
|
||||||
'SELECT referred_by_user_id FROM users WHERE id = $1',
|
'SELECT referred_by_user_id FROM users WHERE id = $1',
|
||||||
[input.referredUserId]
|
[input.referredUserId]
|
||||||
);
|
);
|
||||||
const user = userRows[0];
|
const user = userRows[0];
|
||||||
if (!user?.referred_by_user_id) return;
|
if (!user?.referred_by_user_id) {
|
||||||
|
logPredictionCommissionSkip('no_referrer', {
|
||||||
|
betId: input.betId,
|
||||||
|
referredUserId: input.referredUserId,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const commissionWalletTypeId = settings.commission_wallet_type_id;
|
const commissionWalletTypeId = settings.commission_wallet_type_id;
|
||||||
if (input.walletTypeId !== commissionWalletTypeId) return;
|
if (input.walletTypeId !== commissionWalletTypeId) {
|
||||||
|
logPredictionCommissionSkip('wallet_type_mismatch', {
|
||||||
|
betId: input.betId,
|
||||||
|
betWalletTypeId: input.walletTypeId,
|
||||||
|
commissionWalletTypeId,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const levels = (await referralRepository.listLevels()).filter((l) => l.is_active);
|
const levels = (await referralRepository.listLevels()).filter((l) => l.is_active);
|
||||||
if (levels.length === 0) return;
|
if (levels.length === 0) {
|
||||||
|
logPredictionCommissionSkip('no_active_levels', { betId: input.betId });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const maxLevel = Math.max(...levels.map((l) => l.level_number));
|
const maxLevel = Math.max(...levels.map((l) => l.level_number));
|
||||||
const chain = await referralRepository.getReferralChain(input.referredUserId, maxLevel);
|
const chain = await referralRepository.getReferralChain(input.referredUserId, maxLevel);
|
||||||
|
if (chain.length === 0) {
|
||||||
|
logPredictionCommissionSkip('empty_referral_chain', {
|
||||||
|
betId: input.betId,
|
||||||
|
referredUserId: input.referredUserId,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
const eligibleAt = new Date();
|
const eligibleAt = new Date();
|
||||||
|
|
||||||
for (const { userId: beneficiaryId, level } of chain) {
|
for (const { userId: beneficiaryId, level } of chain) {
|
||||||
@ -502,10 +551,25 @@ export async function processPredictionReferralCommissions(input: {
|
|||||||
if (!levelConfig) continue;
|
if (!levelConfig) continue;
|
||||||
|
|
||||||
const percent = Number(levelConfig.commission_percent);
|
const percent = Number(levelConfig.commission_percent);
|
||||||
if (percent <= 0) continue;
|
if (percent <= 0) {
|
||||||
|
logPredictionCommissionSkip('zero_percent', {
|
||||||
|
betId: input.betId,
|
||||||
|
level,
|
||||||
|
beneficiaryId,
|
||||||
|
});
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
const commissionAmount = calcCommission(input.stakeRaw, percent);
|
const commissionAmount = calcCommission(input.stakeRaw, percent);
|
||||||
if (commissionAmount <= 0n) continue;
|
if (commissionAmount <= 0n) {
|
||||||
|
logPredictionCommissionSkip('commission_rounds_to_zero', {
|
||||||
|
betId: input.betId,
|
||||||
|
level,
|
||||||
|
stakeRaw: input.stakeRaw.toString(),
|
||||||
|
percent,
|
||||||
|
});
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
const sybil = await evaluateSybilForCommission(settings, beneficiaryId, input.referredUserId);
|
const sybil = await evaluateSybilForCommission(settings, beneficiaryId, input.referredUserId);
|
||||||
|
|
||||||
|
|||||||
@ -785,7 +785,7 @@ npm run phase-b:init
|
|||||||
npm run phase-b:up
|
npm run phase-b:up
|
||||||
```
|
```
|
||||||
|
|
||||||
Redis est **interne Docker uniquement** (pas de port 6379 sur l’hôte) et protégé par `REDIS_PASSWORD` dans `.env` (obligatoire). Si la prédiction / le Support affichent `Stream isn't writeable…` / `Connection is closed`, ou en cas de `READONLY` réplica : voir **[TROUBLESHOOTING-REDIS-PORT.md](./TROUBLESHOOTING-REDIS-PORT.md)**.
|
Redis est **interne Docker uniquement** (pas de port 6379 sur l’hôte) et protégé par `REDIS_PASSWORD` dans `.env` (obligatoire). En cas d’incident Redis (`Stream isn't writeable…`, `Connection is closed`, `READONLY`) : suivre le runbook **[REDIS-RUNBOOK.md](./REDIS-RUNBOOK.md)** (détails : [TROUBLESHOOTING-REDIS-PORT.md](./TROUBLESHOOTING-REDIS-PORT.md)).
|
||||||
|
|
||||||
Accès : **http://prediqo.redirectme.net:9080** (avec le port, en **http**)
|
Accès : **http://prediqo.redirectme.net:9080** (avec le port, en **http**)
|
||||||
|
|
||||||
|
|||||||
192
docs/REDIS-RUNBOOK.md
Normal file
192
docs/REDIS-RUNBOOK.md
Normal file
@ -0,0 +1,192 @@
|
|||||||
|
# Runbook Redis Prediqo (VPS)
|
||||||
|
|
||||||
|
Procédure à suivre **dès qu’un problème Redis se reproduit** (prédiction bloquée, Support offline, `READONLY`, `Stream isn't writeable…`, etc.).
|
||||||
|
|
||||||
|
Répertoire de travail (adapter si besoin) :
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd ~/projects_repository/games-project
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 0. Charger le mot de passe (sans `source .env`)
|
||||||
|
|
||||||
|
Ne pas faire `source .env` (risque d’exécuter des valeurs multi-mots comme commandes).
|
||||||
|
|
||||||
|
```bash
|
||||||
|
export REDIS_PASSWORD="$(grep -E '^REDIS_PASSWORD=' .env | cut -d= -f2-)"
|
||||||
|
# Vérifier que la variable n’est pas vide :
|
||||||
|
test -n "$REDIS_PASSWORD" && echo "REDIS_PASSWORD ok" || echo "MANQUANT dans .env"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. Reconnaître le symptôme
|
||||||
|
|
||||||
|
| Symptôme app / logs | Cause probable |
|
||||||
|
|---------------------|----------------|
|
||||||
|
| `Stream isn't writeable and enableOfflineQueue options is false` | Redis down ou injoignable depuis l’API/WS |
|
||||||
|
| `Connection is closed` | Idem |
|
||||||
|
| `READONLY You can't write against a read only replica` | Redis en **slave** (attaque / mauvais état) |
|
||||||
|
| `WRONGPASS` / `NOAUTH` sur `redis-cli` | Mot de passe shell faux / oublié `-a` |
|
||||||
|
| Conteneur `crypto_wallet_redis` Exit / unhealthy | Crash, mauvais `REDIS_PASSWORD`, volume |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. Diagnostic rapide (ordre fixe)
|
||||||
|
|
||||||
|
### 2.1 Conteneur up ?
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker compose ps redis
|
||||||
|
docker ps --filter name=crypto_wallet_redis
|
||||||
|
```
|
||||||
|
|
||||||
|
Attendu : **Up** / **healthy**.
|
||||||
|
|
||||||
|
### 2.2 Port 6379 **ne doit pas** être public
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo ss -tlnp | grep 6379 || echo "OK: 6379 fermé"
|
||||||
|
```
|
||||||
|
|
||||||
|
Si quelque chose écoute sur 6379 sur l’hôte : ce n’est **pas** Prediqo (compose ne publie plus ce port). Arrêter l’autre Redis / process ; **ne pas** republier 6379 pour Prediqo.
|
||||||
|
|
||||||
|
### 2.3 Auth + rôle master
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker exec crypto_wallet_redis redis-cli -a "$REDIS_PASSWORD" --no-auth-warning ping
|
||||||
|
# → PONG
|
||||||
|
|
||||||
|
docker exec crypto_wallet_redis redis-cli -a "$REDIS_PASSWORD" --no-auth-warning INFO replication
|
||||||
|
# → role:master
|
||||||
|
# → connected_slaves:0
|
||||||
|
```
|
||||||
|
|
||||||
|
Sans `-a` / sans export, `NOAUTH` est **normal** (Redis refuse l’accès anonyme).
|
||||||
|
|
||||||
|
### 2.4 Écriture depuis l’API
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker exec crypto_wallet_api node -e "
|
||||||
|
const Redis=require('ioredis');
|
||||||
|
const r=new Redis(process.env.REDIS_URL||'');
|
||||||
|
if(!process.env.REDIS_URL){console.error('NO_REDIS_URL');process.exit(1);}
|
||||||
|
r.set('write_probe','1').then(()=>{console.log('REDIS_WRITE_OK');return r.del('write_probe');})
|
||||||
|
.catch(e=>console.error('REDIS_FAIL',e.message))
|
||||||
|
.finally(()=>r.quit());
|
||||||
|
"
|
||||||
|
```
|
||||||
|
|
||||||
|
Attendu : `REDIS_WRITE_OK`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. Remediation selon le cas
|
||||||
|
|
||||||
|
### Cas A — Redis down / unhealthy / Stream isn't writeable
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker compose up -d redis
|
||||||
|
# Attendre healthy :
|
||||||
|
docker compose ps redis
|
||||||
|
|
||||||
|
docker compose up -d backend backend-ws backend-worker-bets backend-engine
|
||||||
|
```
|
||||||
|
|
||||||
|
Revérifier §2.3 et §2.4. Recharger Candle Predict / Support.
|
||||||
|
|
||||||
|
### Cas B — `role:slave` (READONLY)
|
||||||
|
|
||||||
|
Avec le compose durci, `REPLICAOF` est désactivé. Recréer Redis (le cache se reconstruit ; **Postgres** reste la source de vérité) :
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker compose stop redis
|
||||||
|
docker compose rm -f redis
|
||||||
|
docker compose up -d redis
|
||||||
|
|
||||||
|
docker exec crypto_wallet_redis redis-cli -a "$REDIS_PASSWORD" --no-auth-warning INFO replication
|
||||||
|
# doit être role:master
|
||||||
|
```
|
||||||
|
|
||||||
|
Si toujours `slave` après recreate :
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker compose stop redis
|
||||||
|
docker compose rm -f redis
|
||||||
|
docker volume ls | grep -i redis
|
||||||
|
# Exemple de nom : games-project_redis_data (adapter)
|
||||||
|
docker volume rm <NOM_VOLUME_redis_data>
|
||||||
|
docker compose up -d redis
|
||||||
|
```
|
||||||
|
|
||||||
|
Puis redémarrer les services app :
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker compose up -d backend backend-ws backend-worker-bets backend-engine
|
||||||
|
```
|
||||||
|
|
||||||
|
### Cas C — `WRONGPASS` alors que Redis est up
|
||||||
|
|
||||||
|
1. Vérifier la ligne dans `.env` : `REDIS_PASSWORD=...` (sans espaces / guillemets inutiles).
|
||||||
|
2. Ré-exporter (§0).
|
||||||
|
3. Recréer Redis **et** les backends pour qu’ils reprennent le mot de passe Compose :
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker compose up -d --force-recreate redis backend backend-ws backend-worker-bets backend-engine
|
||||||
|
```
|
||||||
|
|
||||||
|
### Cas D — `REDIS_FAIL` depuis l’API alors que `PONG` OK
|
||||||
|
|
||||||
|
L’API n’a probablement pas le bon `REDIS_URL` (ancien conteneur). Recréer les backends :
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker compose up -d --force-recreate backend backend-ws backend-worker-bets backend-engine
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. Checklist « tout est OK »
|
||||||
|
|
||||||
|
- [ ] `docker compose ps redis` → healthy
|
||||||
|
- [ ] `ss … 6379` → rien (fermé)
|
||||||
|
- [ ] `ping` → `PONG`
|
||||||
|
- [ ] `INFO replication` → `role:master`, `connected_slaves:0`
|
||||||
|
- [ ] Script API → `REDIS_WRITE_OK`
|
||||||
|
- [ ] Candle Predict / Support : plus d’erreurs stream
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. Noms de services utiles
|
||||||
|
|
||||||
|
| Service Compose | Conteneur |
|
||||||
|
|-----------------|-----------|
|
||||||
|
| `redis` | `crypto_wallet_redis` |
|
||||||
|
| `backend` | `crypto_wallet_api` |
|
||||||
|
| `backend-ws` | `crypto_wallet_ws` |
|
||||||
|
| `backend-worker-bets` | `crypto_wallet_worker_bets` |
|
||||||
|
| `backend-engine` | `crypto_wallet_engine` |
|
||||||
|
|
||||||
|
*(Il n’existe pas de service `backend-worker-engine`.)*
|
||||||
|
|
||||||
|
Logs :
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker logs crypto_wallet_redis --tail 80
|
||||||
|
docker logs crypto_wallet_api --tail 80
|
||||||
|
docker logs crypto_wallet_ws --tail 80
|
||||||
|
```
|
||||||
|
|
||||||
|
Ou : `npm run phase-b:logs` / `npm run phase-b:status`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6. Règles à ne pas enfreindre
|
||||||
|
|
||||||
|
1. **Ne jamais** republier `6379:6379` sur le VPS.
|
||||||
|
2. Garder `REDIS_PASSWORD` fort dans `.env` (non commité).
|
||||||
|
3. Preférer `export REDIS_PASSWORD="$(grep …)"` plutôt que `source .env`.
|
||||||
|
4. Après incident slave : recreate Redis (+ volume si besoin), puis confirmer `role:master`.
|
||||||
|
|
||||||
|
Détails / contexte : [TROUBLESHOOTING-REDIS-PORT.md](./TROUBLESHOOTING-REDIS-PORT.md) · déploiement : [DEPLOY-VPS.md](./DEPLOY-VPS.md).
|
||||||
@ -1,5 +1,7 @@
|
|||||||
# Dépannage Redis (port / auth / READONLY)
|
# Dépannage Redis (port / auth / READONLY)
|
||||||
|
|
||||||
|
> **En incident :** procédure pas à pas → **[REDIS-RUNBOOK.md](./REDIS-RUNBOOK.md)**.
|
||||||
|
|
||||||
## Sécurité (état attendu)
|
## Sécurité (état attendu)
|
||||||
|
|
||||||
Redis Prediqo est **durci** dans `docker-compose.yml` :
|
Redis Prediqo est **durci** dans `docker-compose.yml` :
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user