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 PGHero to the demo app #1294

Merged
merged 11 commits into from
Dec 3, 2024
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ jobs:
- name: Run linter
run: bin/lint --nofix

development_environment:
name: Development Environment
development_demo:
name: Tests for Development and Demo
runs-on: ubuntu-latest
timeout-minutes: 10
env:
Expand Down
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ platforms :ruby do
gem "tapioca", require: false
end

group :development, :demo, :production do
gem "pghero"
gem "sprockets-rails"
end

group :demo, :production do
gem "skylight"
end
Expand Down
11 changes: 11 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ GEM
ast (~> 2.4.1)
racc
pg (1.5.9)
pghero (3.6.1)
activerecord (>= 6.1)
prism (1.2.0)
protocol-hpack (1.5.1)
protocol-http (0.43.0)
Expand Down Expand Up @@ -445,6 +447,13 @@ GEM
prism (>= 0.28.0)
sorbet-static-and-runtime (>= 0.5.10187)
thor (>= 0.19.2)
sprockets (4.2.1)
concurrent-ruby (~> 1.0)
rack (>= 2.2.4, < 4)
sprockets-rails (3.5.2)
actionpack (>= 6.1)
activesupport (>= 6.1)
sprockets (>= 3.0.0)
stackprof (0.2.26)
stringio (3.1.2)
tapioca (0.16.4)
Expand Down Expand Up @@ -518,6 +527,7 @@ DEPENDENCIES
mdl
memory_profiler
pg
pghero
puma
rack-mini-profiler
rails
Expand All @@ -534,6 +544,7 @@ DEPENDENCIES
sorbet
sorbet-runtime
spoom
sprockets-rails
stackprof
tapioca
timecop
Expand Down
15 changes: 15 additions & 0 deletions demo/app/jobs/pg_hero_maintenance_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class PgHeroMaintenanceJob < ApplicationJob
include GoodJob::ActiveJobExtensions::Concurrency

good_job_control_concurrency_with(
key: "pg_hero_maintenance",
total_limit: 1
)

discard_on StandardError

def perform
PgHero.capture_query_stats
PgHero.clean_query_stats
end
end
11 changes: 11 additions & 0 deletions demo/config/initializers/good_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
}
end

Rails.configuration.after_initialize do
# TODO: It should not be necessary to manually initialize Active Record base
# This seemed to be introduced when PG Hero was added.
ActiveJob::Base
end

case Rails.env
when 'development'
GoodJob.on_thread_error = -> (error) { Rails.logger.warn("#{error}\n#{error.backtrace}") }
Expand Down Expand Up @@ -79,6 +85,11 @@
complex_schedule: {
cron: -> (last_ran) { last_ran ? last_ran + 17.hours : Time.now},
class: "OtherJob",
},
pg_hero_maintenance: {
cron: "*/10 * * * *", # Every 10 minutes
class: "PgHeroMaintenanceJob",
description: "Runs PG Hero maintenance",
}
}
end
Expand Down
2 changes: 2 additions & 0 deletions demo/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
mount GoodJob::Engine => "/good_job"

get :create_job, to: 'application#create_job'

mount PgHero::Engine, at: "pghero" if defined?(PgHero)
end
17 changes: 17 additions & 0 deletions demo/db/migrate/20240321162554_create_pghero_query_stats.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class CreatePgheroQueryStats < ActiveRecord::Migration[7.1]
def change
enable_extension "pg_stat_statements"

create_table :pghero_query_stats do |t|
t.text :database
t.text :user
t.text :query
t.integer :query_hash, limit: 8
t.float :total_time
t.integer :calls, limit: 8
t.timestamp :captured_at
end

add_index :pghero_query_stats, [:database, :captured_at]
end
end
11 changes: 11 additions & 0 deletions demo/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

ActiveRecord::Schema.define(version: 2024_08_01_143343) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements"
enable_extension "pgcrypto"
enable_extension "plpgsql"

Expand Down Expand Up @@ -104,4 +105,14 @@
t.index ["scheduled_at"], name: "index_good_jobs_on_scheduled_at", where: "(finished_at IS NULL)"
end

create_table "pghero_query_stats", force: :cascade do |t|
t.text "database"
t.text "user"
t.text "query"
t.bigint "query_hash"
t.float "total_time"
t.bigint "calls"
t.datetime "captured_at", precision: nil
t.index ["database", "captured_at"], name: "index_pghero_query_stats_on_database_and_captured_at"
end
end
Loading