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 toggl::TogglApi;
use worker::Worker; use worker::Worker;
@ -36,18 +36,18 @@ async fn main() {
// Init tracing // Init tracing
tracing_subscriber::fmt::init(); 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 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") sqlx::migrate!("./migrations")
.run(&mut db) .run(&db)
.await .await
.expect("Failed to run migrations"); .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") 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 // run our app with hyper, listening globally on port 3000
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await?; 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(()) Ok(())
} }

View File

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