|
| 1 | +use alloy::hex::ToHexExt; |
1 | 2 | use alloy::rpc::types::Log; |
2 | | -use anyhow::Result; |
3 | | -use diesel::PgConnection; |
| 3 | +use anyhow::{Context, Result}; |
| 4 | +use diesel::{ExpressionMethods, PgConnection, RunQueryDsl}; |
| 5 | +use indexer_framework::schema::revise_rate_requests; |
4 | 6 | use tracing::warn; |
5 | 7 | use tracing::{info, instrument}; |
6 | 8 |
|
7 | 9 | #[instrument(level = "info", skip_all, parent = None, fields(block = log.block_number, idx = log.log_index))] |
8 | | -pub fn handle_job_revise_rate_cancelled(_conn: &mut PgConnection, log: Log) -> Result<()> { |
| 10 | +pub fn handle_job_revise_rate_cancelled(conn: &mut PgConnection, log: Log) -> Result<()> { |
9 | 11 | info!(?log, "processing"); |
10 | 12 |
|
11 | | - // while we do have enough context here to handle this properly, |
12 | | - // JobClosed makes us handle LockDeleted |
13 | | - // which also more or less handles this |
| 13 | + let id = log.topics()[1].encode_hex_with_prefix(); |
14 | 14 |
|
15 | | - info!("empty impl, supposed to be handled by LockDeleted"); |
| 15 | + info!(id, "cancelling job rate revision"); |
| 16 | + |
| 17 | + // target sql: |
| 18 | + // DELETE FROM revise_rate_requests |
| 19 | + // WHERE id = "<id>"; |
| 20 | + let count = diesel::delete(revise_rate_requests::table) |
| 21 | + .filter(revise_rate_requests::id.eq(&id)) |
| 22 | + .execute(conn) |
| 23 | + .context("failed to delete revise rate request")?; |
| 24 | + |
| 25 | + if count != 1 { |
| 26 | + // !!! should never happen |
| 27 | + // the only real condition is when the request does not exist or is already deleted |
| 28 | + // it is not a critical error, we can just move on |
| 29 | + warn!("did not expect to find a non existent request when cancelling job rate revision"); |
| 30 | + } |
| 31 | + |
| 32 | + info!(id, "deleted revise rate request"); |
16 | 33 |
|
17 | 34 | Ok(()) |
18 | 35 | } |
| 36 | + |
| 37 | +// TODO: add tests |
0 commit comments