Skip to content

Commit dadafc5

Browse files
authored
Merge pull request #9583 from Turbo87/async-populate
admin/populate: Convert to async
2 parents f038287 + 7512308 commit dadafc5

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/admin/populate.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use crate::{db, schema::version_downloads};
22

3-
use crate::tasks::spawn_blocking;
43
use diesel::prelude::*;
5-
use rand::{thread_rng, Rng};
4+
use diesel_async::scoped_futures::ScopedFutureExt;
5+
use diesel_async::{AsyncConnection, AsyncPgConnection, RunQueryDsl};
6+
use rand::rngs::StdRng;
7+
use rand::{Rng, SeedableRng};
68

79
#[derive(clap::Parser, Debug)]
810
#[command(
@@ -15,19 +17,17 @@ pub struct Opts {
1517
}
1618

1719
pub async fn run(opts: Opts) -> anyhow::Result<()> {
18-
spawn_blocking(move || {
19-
let mut conn = db::oneoff_connection()?;
20-
conn.transaction(|conn| update(opts, conn))?;
21-
Ok(())
22-
})
23-
.await
20+
let mut conn = db::oneoff_async_connection().await?;
21+
conn.transaction(|conn| update(opts, conn).scope_boxed())
22+
.await?;
23+
Ok(())
2424
}
2525

26-
fn update(opts: Opts, conn: &mut PgConnection) -> QueryResult<()> {
26+
async fn update(opts: Opts, conn: &mut AsyncPgConnection) -> QueryResult<()> {
2727
use diesel::dsl::*;
2828

2929
for id in opts.version_ids {
30-
let mut rng = thread_rng();
30+
let mut rng = StdRng::from_entropy();
3131
let mut dls = rng.gen_range(5_000i32..10_000);
3232

3333
for day in 0..90 {
@@ -39,7 +39,8 @@ fn update(opts: Opts, conn: &mut PgConnection) -> QueryResult<()> {
3939
version_downloads::downloads.eq(dls),
4040
version_downloads::date.eq(date(now - day.days())),
4141
))
42-
.execute(conn)?;
42+
.execute(conn)
43+
.await?;
4344
}
4445
}
4546
Ok(())

0 commit comments

Comments
 (0)