-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added cron to update github profiles
- Loading branch information
Showing
11 changed files
with
240 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use error::Result; | ||
use store::github_projects; | ||
|
||
#[async_trait] | ||
pub trait DbRepositoryContract { | ||
/// | ||
/// Get github projects from db | ||
/// | ||
async fn get_projects(&self) -> Result<Vec<github_projects::Model>>; | ||
} | ||
|
||
#[async_trait] | ||
pub trait DbServiceContract { | ||
/// | ||
/// Update github project in db | ||
/// | ||
async fn update_project(&self, project: github_projects::ActiveModel) -> Result<()>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use std::time::Duration; | ||
|
||
use super::contract::{DbRepositoryContract, DbServiceContract}; | ||
use cronus::{Job, Schedule}; | ||
use error::Result; | ||
use sdks::github::GithubContract; | ||
use sea_orm::Set; | ||
use store::github_projects::{ActiveModel, Model}; | ||
use tokio::time::sleep; | ||
|
||
pub struct GithubProjectInfo< | ||
Repository: DbRepositoryContract, | ||
Service: DbServiceContract, | ||
Github: GithubContract, | ||
> { | ||
repository: Repository, | ||
service: Service, | ||
github: Github, | ||
} | ||
|
||
impl< | ||
Repository: DbRepositoryContract + Send + Sync + 'static, | ||
Service: DbServiceContract + Send + Sync + 'static, | ||
Github: GithubContract + Send + Sync + 'static, | ||
> GithubProjectInfo<Repository, Service, Github> | ||
{ | ||
/// | ||
/// Creates GithubRepositoriesCron | ||
/// | ||
pub fn new(repository: Repository, service: Service, github: Github) -> Self { | ||
Self { | ||
repository, | ||
service, | ||
github, | ||
} | ||
} | ||
|
||
/// | ||
/// Cron job that runs once a week | ||
/// | ||
pub async fn cron_job(&self) -> Result<()> { | ||
let projects = self.repository.get_projects().await?.into_iter(); | ||
|
||
for project in projects { | ||
if let Err(error) = self.update_project(project).await { | ||
error!("{}", error); | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
async fn update_project(&self, model: Model) -> Result<()> { | ||
for _ in 0..5 { | ||
match self.github.get_profile(&model.name).await { | ||
Ok(profile) => { | ||
let project = ActiveModel { | ||
url: Set(profile.site), | ||
followers: Set(profile.followers), | ||
profile_type: Set(Some(profile.profile_type.to_string())), | ||
..model.into() | ||
}; | ||
|
||
self.service.update_project(project).await?; | ||
|
||
break; | ||
} | ||
Err(error) => { | ||
error!("{}", error); | ||
} | ||
}; | ||
|
||
sleep(Duration::from_secs(60)).await; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl<Repository, Service, Github> Job for GithubProjectInfo<Repository, Service, Github> | ||
where | ||
Repository: DbRepositoryContract + Send + Sync + 'static, | ||
Service: DbServiceContract + Send + Sync + 'static, | ||
Github: GithubContract + Send + Sync + 'static, | ||
{ | ||
fn schedule(&self) -> Schedule { | ||
"0 0 0 * * Mon".parse().expect("Invalid schedule") | ||
} | ||
async fn job(&self) { | ||
if let Err(error) = self.cron_job().await { | ||
error!("{}", error); | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
backend/api/src/jobs/github_project_info/infrastructure/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
mod repository; | ||
mod service; | ||
|
||
pub use repository::PgRepository; | ||
pub use service::PgService; |
28 changes: 28 additions & 0 deletions
28
backend/api/src/jobs/github_project_info/infrastructure/repository.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use error::Result; | ||
use sea_orm::{DatabaseConnection, EntityTrait, Order, QueryOrder}; | ||
use std::sync::Arc; | ||
|
||
use super::super::contract::DbRepositoryContract; | ||
use store::github_projects::{Column, Entity, Model}; | ||
|
||
pub struct PgRepository { | ||
conn: Arc<DatabaseConnection>, | ||
} | ||
|
||
impl PgRepository { | ||
pub fn new(conn: Arc<DatabaseConnection>) -> Self { | ||
Self { conn } | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl DbRepositoryContract for PgRepository { | ||
async fn get_projects(&self) -> Result<Vec<Model>> { | ||
let projects = Entity::find() | ||
.order_by(Column::Name, Order::Asc) | ||
.all(self.conn.as_ref()) | ||
.await?; | ||
|
||
Ok(projects) | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
backend/api/src/jobs/github_project_info/infrastructure/service.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use error::Result; | ||
use sea_orm::{DatabaseConnection, EntityTrait}; | ||
use std::sync::Arc; | ||
|
||
use super::super::contract::DbServiceContract; | ||
use store::github_projects; | ||
|
||
pub struct PgService { | ||
conn: Arc<DatabaseConnection>, | ||
} | ||
|
||
impl PgService { | ||
pub fn new(conn: Arc<DatabaseConnection>) -> Self { | ||
Self { conn } | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl DbServiceContract for PgService { | ||
async fn update_project(&self, project: github_projects::ActiveModel) -> Result<()> { | ||
github_projects::Entity::update(project) | ||
.exec(self.conn.as_ref()) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
mod contract; | ||
mod domain; | ||
pub mod infrastructure; | ||
|
||
|
||
use cronus::Cronus; | ||
pub use domain::GithubProjectInfo; | ||
use infrastructure::{PgRepository, PgService}; | ||
use sdks::github::Github; | ||
use sea_orm::DatabaseConnection; | ||
use std::sync::Arc; | ||
|
||
/// | ||
/// Create and spawn github repositories job | ||
/// | ||
pub fn setup(cron: &Cronus, sea_pool: Arc<DatabaseConnection>) { | ||
let job = create_gr(sea_pool); | ||
cron.add(job).expect("Error adding job"); | ||
} | ||
|
||
/// | ||
/// Create GithubRepositoriesCron with default implementations | ||
/// | ||
fn create_gr( | ||
sea_pool: Arc<DatabaseConnection>, | ||
) -> GithubProjectInfo<PgRepository, PgService, Github> { | ||
let repository = PgRepository::new(sea_pool.clone()); | ||
let service = PgService::new(sea_pool); | ||
let github_api_key = config::get("GITHUB_KEY").ok(); | ||
|
||
let github = match github_api_key { | ||
Some(api_key) => Github::new_with_auth(api_key), | ||
None => Github::default(), | ||
}; | ||
|
||
GithubProjectInfo::new(repository, service, github) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters