Skip to content

Commit

Permalink
Merge pull request #9 from demml/add-pg_cron
Browse files Browse the repository at this point in the history
adding pg_cron config
  • Loading branch information
thorrester authored Jul 13, 2024
2 parents 6916c7c + 951e53b commit c33841f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "scouter-server"
version = "0.1.5"
version = "0.1.6"
edition = "2021"

authors = [
Expand Down
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ RUN apt-get update \
libssl-dev \
make \
pkg-config \
postgresql-server-dev-16
postgresql-server-dev-16 \
postgresql-16-cron

WORKDIR /monitor

Expand Down
12 changes: 12 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ services:
build:
context: .
dockerfile: Dockerfile
command:
[
"postgres",
"-c",
"log_statement=all",
"-c",
"log_destination=stderr",
"-c",
"shared_preload_libraries=pg_cron",
"-c",
"cron.database_name=monitor",
]
restart: always
environment:
POSTGRES_PASSWORD: admin
Expand Down
15 changes: 13 additions & 2 deletions migrations/20240614172639_setup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
CREATE SCHEMA if not exists scouter;
CREATE EXTENSION if not exists pg_partman SCHEMA scouter;

-- add pg_cron
-- run as superuser:
CREATE EXTENSION if not exists pg_cron;


CREATE ROLE partman_user WITH LOGIN;
GRANT ALL ON SCHEMA scouter TO partman_user;
GRANT USAGE ON SCHEMA cron TO partman_user;
GRANT ALL ON ALL TABLES IN SCHEMA scouter TO partman_user;
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA scouter TO partman_user;
GRANT EXECUTE ON ALL PROCEDURES IN SCHEMA scouter TO partman_user;
GRANT ALL ON SCHEMA scouter TO partman_user;
GRANT TEMPORARY ON DATABASE monitor to partman_user;

CREATE TABLE scouter.drift (
created_at timestamp not null default (timezone('utc', now())),
Expand Down Expand Up @@ -45,4 +50,10 @@ CREATE TABLE scouter.drift_profile (
cron varchar(256),
next_run timestamp,
PRIMARY KEY (name, repository, version)
);
);

-- Run maintenance every hour
SELECT cron.schedule('partition-maintenance', '0 * * * *', $$CALL partman.run_maintenance_proc()$$);

-- Run maintenance once a day at midnight utc with p_analyze set to true
SELECT cron.schedule('partition-maintenance-analyze', '30 0 * * *', $$CALL partman.run_maintenance_proc(0, true, true)$$);
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ async fn start_main_server() -> Result<(), anyhow::Error> {
.await
.with_context(|| "Failed to run migrations")?;

// setup background pg_partman run_maintenance function

// setup background task if kafka is enabled
if std::env::var("KAFKA_BROKER").is_ok() {
info!("🚀 Starting Kafka consumer");
let brokers = std::env::var("KAFKA_BROKER").unwrap();
let topics = vec![std::env::var("KAFKA_TOPIC").unwrap_or("scouter_monitoring".to_string())];
let group_id = std::env::var("KAFKA_GROUP").unwrap_or("scouter".to_string());
Expand Down

0 comments on commit c33841f

Please sign in to comment.