63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
// This one does nothing except making sure only one token exists for each id
pub struct Token {
running: Arc<Mutex<HashSet<i32>>>,
my_id: i32,
}
impl Token {
fn new (running: &Arc<Mutex<HashSet<i32>>>, my_id: i32) -> Option<Token> {
let running = running.clone();
smol::block_on(async {
let mut set = running.lock_arc().await;
if set.contains(&my_id) {
None
} else {
set.insert(my_id);
Some(Token {
running,
my_id,
})
}
})
}
}
impl Drop for Token {
fn drop (&mut self) {
smol::block_on(async {
let mut set = self.running.lock_arc().await;
set.remove(&self.my_id);
})
}
}
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
<
|
|
<
>
|
|
|
|
|
|
|
<
>
>
>
>
|
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
// This one does nothing except making sure only one token exists for each id
pub struct Token {
running: Arc<Mutex<HashSet<i32>>>,
my_id: i32,
}
impl Token {
/// Attempts to acquire a per-id token by inserting `my_id` into the shared `running` set.
///
/// If the id was not already present, the function inserts it and returns `Some(Token)`.
/// When the returned `Token` is dropped, the id will be removed from the `running` set,
/// allowing subsequent acquisitions for the same id.
///
/// # Parameters
///
/// - `running`: Shared set tracking active ids.
/// - `my_id`: Identifier to acquire a token for.
///
/// # Returns
///
/// `Ok(Token)` if the id was successfully acquired, `Error` if a token for the id is already active.
async fn new (running: &Arc<Mutex<HashSet<i32>>>, my_id: i32) -> Result<Token> {
let running = running.clone();
let mut set = running.lock_arc().await;
if set.contains(&my_id) {
bail!("Token already taken");
} else {
set.insert(my_id);
Ok(Token {
running,
my_id,
})
}
}
}
impl Drop for Token {
/// Releases this token's claim on the shared running-set when the token is dropped.
///
/// The token's identifier is removed from the shared `running` set so that future
/// operations for the same id may proceed.
fn drop (&mut self) {
smol::block_on(async {
let mut set = self.running.lock_arc().await;
set.remove(&self.my_id);
})
}
}
|
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
).await.stack()
}
pub async fn check (&self, id: i32, real: bool, last_scrape: Option<DateTime<Local>>) -> Result<String> {
let mut posted: i32 = 0;
let mut conn = self.db.begin().await.stack()?;
let token = Token::new(&self.running, id);
if token.is_none() {
bail!("check is already running");
}
let source = conn.get_source(id, self.owner_chat).await.stack()?;
conn.set_scrape(id).await.stack()?;
let destination = ChatPeerId::from(match real {
true => source.channel_id,
false => source.owner,
});
let mut this_fetch: Option<DateTime<chrono::FixedOffset>> = None;
|
|
<
<
<
|
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
).await.stack()
}
pub async fn check (&self, id: i32, real: bool, last_scrape: Option<DateTime<Local>>) -> Result<String> {
let mut posted: i32 = 0;
let mut conn = self.db.begin().await.stack()?;
let _token = Token::new(&self.running, id).await.stack()?;
let source = conn.get_source(id, self.owner_chat).await.stack()?;
conn.set_scrape(id).await.stack()?;
let destination = ChatPeerId::from(match real {
true => source.channel_id,
false => source.owner,
});
let mut this_fetch: Option<DateTime<chrono::FixedOffset>> = None;
|