Skip to content

Commit d68f095

Browse files
author
Eason
committed
refactor: change run function
1 parent c983d12 commit d68f095

File tree

7 files changed

+149
-9
lines changed

7 files changed

+149
-9
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
# database
1111
*.sqlite
1212

13-
free-space
13+
free-space/

Cargo.lock

Lines changed: 67 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ rpc-client = { path = "./rpc-client" }
1111
storage = { path = "./storage" }
1212
tx-builder = { path = "./tx-builder" }
1313

14+
backtrace = "0.3"
15+
log = "0.4"
1416
serde = { version = "1.0", features = ["derive"] }
15-
tokio = { version = "1.28", features = ["macros", "rt"] }
17+
tokio = { version = "1.28", features = ["macros", "signal", "rt"] }
1618
toml = "0.7"
1719

1820
[workspace]

common/src/types/relation_db/transaction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use sea_orm::entity::prelude::*;
44
use serde::{Deserialize, Serialize};
55

66
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Deserialize, Serialize)]
7-
#[sea_orm(table_name = "transaction")]
7+
#[sea_orm(table_name = "transaction_")]
88
pub struct Model {
99
#[sea_orm(primary_key)]
1010
pub id: u32,

config.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
private_key = ""
2-
rpc_listening_address = "127.0.0.1:8000"
3-
rdb_url = ""
2+
rpc_listen_address = "127.0.0.1:8000"
3+
rdb_url = "sqlite:/free-space/spark.db"
44
kvdb_path = "free-space/db"

create_table.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CREATE TABLE IF NOT EXISTS TRANSACTION_(
2+
ID INTEGER PRIMARY KEY,
3+
ADDRESS TEXT,
4+
TIMESTAMP INT,
5+
EVENT INT,
6+
TX_HASH TEXT,
7+
TOTAL_AMOUNT INT,
8+
STAKE_AMOUNT INT,
9+
DELEGATE_AMOUNT INT,
10+
WITHDRAWABLE_AMOUNT INT,
11+
STAKE_RATE TEXT,
12+
DELEGATE_RATE TEXT,
13+
EPOCH INT,
14+
STATUS INT
15+
);

src/main.rs

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
mod config;
22

3-
use std::{env, sync::Arc};
3+
use std::{env, panic::PanicInfo, sync::Arc};
44

55
use api::{run_server, DefaultAPIAdapter};
6+
use backtrace::Backtrace;
67
use config::SparkConfig;
78
use storage::{SmtManager, TransactionHistory};
9+
#[cfg(unix)]
10+
use tokio::signal::unix as os_impl;
811

912
#[tokio::main]
1013
async fn main() {
@@ -14,9 +17,63 @@ async fn main() {
1417
let rdb = Arc::new(TransactionHistory::new(&config.rdb_url).await);
1518
let kvdb = Arc::new(SmtManager::new(&config.kvdb_path));
1619
let api_adapter = Arc::new(DefaultAPIAdapter::new(rdb, kvdb));
17-
let _handle = run_server(api_adapter, config.rpc_listen_address)
20+
let handle = run_server(api_adapter, config.rpc_listen_address)
1821
.await
1922
.unwrap();
2023

21-
println!("Hello, world!");
24+
tokio::spawn(handle.stopped());
25+
26+
set_ctrl_c_handle().await;
27+
}
28+
29+
async fn set_ctrl_c_handle() {
30+
let ctrl_c_handler = tokio::spawn(async {
31+
#[cfg(windows)]
32+
let _ = tokio::signal::ctrl_c().await;
33+
#[cfg(unix)]
34+
{
35+
let mut sigtun_int = os_impl::signal(os_impl::SignalKind::interrupt()).unwrap();
36+
let mut sigtun_term = os_impl::signal(os_impl::SignalKind::terminate()).unwrap();
37+
tokio::select! {
38+
_ = sigtun_int.recv() => {}
39+
_ = sigtun_term.recv() => {}
40+
};
41+
}
42+
});
43+
44+
// register channel of panic
45+
let (panic_sender, mut panic_receiver) = tokio::sync::mpsc::channel::<()>(1);
46+
47+
std::panic::set_hook(Box::new(move |info: &PanicInfo| {
48+
let panic_sender = panic_sender.clone();
49+
panic_log(info);
50+
panic_sender.try_send(()).expect("panic_receiver is droped");
51+
}));
52+
53+
tokio::select! {
54+
_ = ctrl_c_handler => { log::info!("ctrl + c is pressed, quit.") },
55+
_ = panic_receiver.recv() => { log::info!("child thread panic, quit.") },
56+
};
57+
}
58+
59+
fn panic_log(info: &PanicInfo) {
60+
let backtrace = Backtrace::new();
61+
let thread = std::thread::current();
62+
let name = thread.name().unwrap_or("unnamed");
63+
let location = info.location().unwrap(); // The current implementation always returns Some
64+
let msg = match info.payload().downcast_ref::<&'static str>() {
65+
Some(s) => *s,
66+
None => match info.payload().downcast_ref::<String>() {
67+
Some(s) => &**s,
68+
None => "Box<Any>",
69+
},
70+
};
71+
log::error!(
72+
target: "panic", "thread '{}' panicked at '{}': {}:{} {:?}",
73+
name,
74+
msg,
75+
location.file(),
76+
location.line(),
77+
backtrace,
78+
);
2279
}

0 commit comments

Comments
 (0)