Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add deployment flow for processor #90

Merged
merged 3 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea/
target/
.vscode/
scripts/
32 changes: 32 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Deploy to Staging and Production

on:
workflow_dispatch:
push:
branches:
- master

jobs:
deploy:
runs-on: ubuntu-latest
strategy:
matrix:
environment: [Staging, Production] # Matrix strategy for environments
env:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
ENVIRONMENT: ${{ matrix.environment }}
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Log in to Docker Hub
run: echo "$DOCKERHUB_TOKEN" | docker login -u "$DOCKERHUB_USERNAME" --password-stdin

- name: Build Docker image
run: docker build -t stagecodes/otr-processor:${{ matrix.environment }} .

- name: Push Docker image to Docker Hub
run: docker push stagecodes/otr-processor:${{ matrix.environment }}

# No deployment job beyond upload, the server will run the latest image when necessary.
93 changes: 92 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "otr-data-processor-rs"
name = "otr-processor"
version = "0.1.0"
edition = "2021"

Expand All @@ -10,7 +10,7 @@ name = "otr_processor"
path = "src/lib.rs"

[[bin]]
name = "otr-processor-cli"
name = "otr-processor"
path = "src/main.rs"

[dependencies]
Expand All @@ -34,6 +34,7 @@ rand_chacha = "0.3.1"
rand = "0.8.5"
thiserror = "1.0.56"
log = "0.4.22"
env_logger = "0.11.6"

[dev-dependencies]
criterion = { version = "0.5.1", features = ["html_reports"] }
Expand Down
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM rust:latest as builder

WORKDIR /usr/src/otr-processor
COPY . .

RUN cargo install --path .

CMD ["otr-processor"]

6 changes: 6 additions & 0 deletions src/database/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
utils::progress_utils::{progress_bar, progress_bar_spinner}
};
use itertools::Itertools;
use log::error;
use postgres_types::ToSql;
use std::{collections::HashMap, sync::Arc};
use tokio_postgres::{Client, Error, NoTls, Row};
Expand Down Expand Up @@ -366,6 +367,11 @@ impl DbClient {
query += &value_placeholders.join(", ");
query += " RETURNING id";

if value_placeholders.is_empty() {
error!("No player_rating data to save to database");
panic!();
}

// Execute the batch insert
let rows = self.client.query(query.as_str(), &[]).await.unwrap();

Expand Down
20 changes: 17 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use env_logger::Env;
use log::{debug, info};
use otr_processor::{
database::db::DbClient,
model::{otr_model::OtrModel, rating_utils::create_initial_ratings},
Expand All @@ -7,39 +9,51 @@ use std::{collections::HashMap, env};

#[tokio::main]
async fn main() {
// Initialize the logger
let _ = dotenv::dotenv();
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();

info!("Begin processing...");

let client: DbClient = client().await;

// 1. Rollback processing statuses of matches & tournaments
client.rollback_processing_statuses().await;
info!("Rollback processing statuses completed.");

// 2. Fetch matches and players for processing
let matches = client.get_matches().await;
let players = client.get_players().await;
debug!("Fetched {} matches and {} players.", matches.len(), players.len());

// 3. Generate initial ratings
let initial_ratings = create_initial_ratings(&players, &matches);
info!("Initial ratings generated.");

// 4. Generate country mapping and set
let country_mapping: HashMap<i32, String> = generate_country_mapping_players(&players);
info!("Country mapping generated.");

// 5. Create the model
let mut model = OtrModel::new(&initial_ratings, &country_mapping);
info!("OTR model created.");

// 6. Process matches
let results = model.process(&matches);
info!("Matches processed.");

// 7. Save results in database
client.save_results(&results).await;
info!("Results saved to database.");

// 8. Update all match processing statuses
client.roll_forward_processing_statuses(&matches).await;
info!("Processing statuses updated.");

println!("Processing complete");
info!("Processing complete");
}

async fn client() -> DbClient {
dotenv::dotenv().unwrap();

let connection_string = env::var("CONNECTION_STRING")
.expect("Expected CONNECTION_STRING environment variable for otr-db PostgreSQL connection.");

Expand Down
Loading