This commit is contained in:
Joshua Coles 2024-07-27 21:13:31 +01:00
parent 67827bd051
commit 5ecd691e41
3 changed files with 24 additions and 18 deletions

View File

@ -1,4 +1,4 @@
use sqlx::{Connection, PgConnection};
use sqlx::{Connection, PgPool};
use toggl::TogglApi;
use worker::Worker;
@ -36,18 +36,18 @@ async fn main() {
// Init tracing
tracing_subscriber::fmt::init();
let api = TogglApi::new(sensitive::API_TOKEN, sensitive::WORKSPACE_ID);
let toggl_api = TogglApi::new(sensitive::API_TOKEN, sensitive::WORKSPACE_ID);
let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set");
let mut db = PgConnection::connect(&database_url).await.unwrap();
let mut db = PgPool::connect(&database_url).await.unwrap();
sqlx::migrate!("./migrations")
.run(&mut db)
.run(&db)
.await
.expect("Failed to run migrations");
let mut worker = Worker { db, toggl_api: api };
let worker = Worker { db, toggl_api };
server::serve().await.expect("Failed to start server")
}

View File

@ -13,7 +13,13 @@ pub async fn serve() -> Result<(), AppError> {
// run our app with hyper, listening globally on port 3000
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await?;
axum::serve(listener, app).await?;
axum::serve(listener, app)
.with_graceful_shutdown(async {
tokio::signal::ctrl_c()
.await
.expect("Failed to install CTRL+C signal handler");
})
.await?;
Ok(())
}

View File

@ -4,7 +4,7 @@ use crate::{AppError, TableSummary};
use chrono::{DateTime, NaiveDate, TimeDelta, Utc};
use itertools::Itertools;
use soa_rs::Soa;
use sqlx::PgConnection;
use sqlx::PgPool;
macro_rules! cast_slice {
($slice: expr, $typ: ty) => {
@ -22,26 +22,26 @@ macro_rules! cast_slice_opts {
}
pub struct Worker {
pub(crate) db: PgConnection,
pub(crate) db: PgPool,
pub(crate) toggl_api: TogglApi,
}
impl Worker {
async fn get_ids(&mut self) -> Result<TableSummary, AppError> {
let client_ids = sqlx::query!("select id from tracking_clients")
.fetch_all(&mut self.db)
.fetch_all(&self.db)
.await?;
let workspace_ids = sqlx::query!("select id from workspaces")
.fetch_all(&mut self.db)
.fetch_all(&self.db)
.await?;
let project_ids = sqlx::query!("select id from projects")
.fetch_all(&mut self.db)
.fetch_all(&self.db)
.await?;
let tag_ids = sqlx::query!("select id from tags")
.fetch_all(&mut self.db)
.fetch_all(&self.db)
.await?;
Ok(TableSummary {
@ -83,7 +83,7 @@ impl Worker {
pub async fn update(&mut self, default_look_back: TimeDelta) -> Result<(), AppError> {
let result = sqlx::query!("select max(updated_at) as last_updated_at from time_entries")
.fetch_optional(&mut self.db)
.fetch_optional(&self.db)
.await
.expect("Could not fetch max updated_at from time_entries");
@ -180,7 +180,7 @@ impl Worker {
time_entries.server_deleted_at() as _,
time_entries.permissions() as _,
)
.execute(&mut self.db)
.execute(&self.db)
.await?;
Ok(())
@ -210,7 +210,7 @@ impl Worker {
cast_slice!(workspaces.organization_id(), i64),
workspaces.name(),
)
.execute(&mut self.db)
.execute(&self.db)
.await?;
Ok(())
@ -267,7 +267,7 @@ impl Worker {
projects.can_track_time(),
projects.permissions() as _,
)
.execute(&mut self.db)
.execute(&self.db)
.await?;
Ok(())
@ -299,7 +299,7 @@ impl Worker {
tags.deleted_at() as _,
tags.permissions() as _,
)
.execute(&mut self.db)
.execute(&self.db)
.await?;
Ok(())
@ -337,7 +337,7 @@ impl Worker {
cast_slice!(clients.workspace_id(), i64),
clients.permissions() as _
)
.execute(&mut self.db)
.execute(&self.db)
.await?;
Ok(())