From 3c5bea50ac36ff7e79a61630da3b5050bc8792e4 Mon Sep 17 00:00:00 2001 From: James McHugh Date: Thu, 29 Aug 2024 21:34:53 -0400 Subject: [PATCH 1/2] Created cron job that disables inactive links daily --- Gemfile | 4 ++++ Gemfile.lock | 23 +++++++++++++++++++ app/models/link.rb | 2 ++ app/sidekiq/disable_inactive_links_job.rb | 14 +++++++++++ config/initializers/sidekiq.rb | 8 +++++++ config/routes.rb | 3 +++ config/sidekiq.yml | 3 +++ .../20240830004510_add_disabled_to_links.rb | 5 ++++ db/schema.rb | 3 ++- 9 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 app/sidekiq/disable_inactive_links_job.rb create mode 100644 config/initializers/sidekiq.rb create mode 100644 config/sidekiq.yml create mode 100644 db/migrate/20240830004510_add_disabled_to_links.rb diff --git a/Gemfile b/Gemfile index 12fdb72..66b394f 100644 --- a/Gemfile +++ b/Gemfile @@ -57,3 +57,7 @@ group :development do # Speed up commands on slow machines / big apps [https://github.com/rails/spring] # gem "spring" end + +gem 'redis' +gem 'sidekiq' +gem 'sidekiq-cron' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 3998d89..3f01e20 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -104,6 +104,8 @@ GEM docile (1.4.1) drb (2.2.1) erubi (1.13.0) + et-orbi (1.2.11) + tzinfo factory_bot (6.4.6) activesupport (>= 5.0.0) factory_bot_rails (6.4.3) @@ -116,6 +118,9 @@ GEM logger faraday-net_http (3.1.1) net-http + fugit (1.11.1) + et-orbi (~> 1, >= 1.2.11) + raabro (~> 1.4) globalid (1.2.1) activesupport (>= 6.1) i18n (1.14.5) @@ -175,6 +180,7 @@ GEM public_suffix (6.0.1) puma (6.4.2) nio4r (~> 2.0) + raabro (1.4.0) racc (1.8.1) rack (3.1.7) rack-cors (2.0.2) @@ -218,6 +224,10 @@ GEM rake (13.2.1) rdoc (6.7.0) psych (>= 4.0.0) + redis (5.3.0) + redis-client (>= 0.22.0) + redis-client (0.22.2) + connection_pool regexp_parser (2.9.2) reline (0.5.9) io-console (~> 0.5) @@ -240,6 +250,16 @@ GEM rspec-support (3.13.1) shoulda-matchers (6.4.0) activesupport (>= 5.2.0) + sidekiq (7.3.1) + concurrent-ruby (< 2) + connection_pool (>= 2.3.0) + logger + rack (>= 2.2.4) + redis-client (>= 0.22.2) + sidekiq-cron (1.12.0) + fugit (~> 1.8) + globalid (>= 1.0.1) + sidekiq (>= 6) simplecov (0.22.0) docile (~> 1.1) simplecov-html (~> 0.11) @@ -282,8 +302,11 @@ DEPENDENCIES puma (>= 5.0) rack-cors rails (~> 7.1.3, >= 7.1.3.4) + redis rspec-rails (~> 6.1) shoulda-matchers (~> 6.4) + sidekiq + sidekiq-cron simplecov (~> 0.22.0) tzinfo-data diff --git a/app/models/link.rb b/app/models/link.rb index 9b147cd..8bf2ec2 100644 --- a/app/models/link.rb +++ b/app/models/link.rb @@ -1,6 +1,8 @@ require 'securerandom' class Link < ApplicationRecord + default_scope { where(disabled: false) } + validates_presence_of :user_id validates_presence_of :original validates :short, uniqueness: true, presence: true diff --git a/app/sidekiq/disable_inactive_links_job.rb b/app/sidekiq/disable_inactive_links_job.rb new file mode 100644 index 0000000..1616733 --- /dev/null +++ b/app/sidekiq/disable_inactive_links_job.rb @@ -0,0 +1,14 @@ +class DisableInactiveLinksJob + include Sidekiq::Worker + queue_as :default + + def perform + links = Link.all + six_months_ago = Time.current - 15552000 #this is 180 days in seconds + links.each do |link| + if link.click_count < 50 && link.last_click != nil && link.last_click < six_months_ago + link.update(disabled: true) + end + end + end +end diff --git a/config/initializers/sidekiq.rb b/config/initializers/sidekiq.rb new file mode 100644 index 0000000..22956de --- /dev/null +++ b/config/initializers/sidekiq.rb @@ -0,0 +1,8 @@ +require 'sidekiq' +require 'sidekiq/cron/job' + +Sidekiq::Cron::Job.create( + name: 'Disable inactive links daily', + cron: '0 0 * * *', # This means daily at midnight + class: 'DisableInactiveLinksJob' +) \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index b519fe7..310ebba 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,7 @@ # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500. # Can be used by load balancers and uptime monitors to verify that the app is live. get 'up' => 'rails/health#show', as: :rails_health_check + require 'sidekiq/web' namespace :api do namespace :v1 do @@ -16,4 +17,6 @@ get 'top_links', to: 'links#top_links' end end + + mount Sidekiq::Web => '/sidekiq' end diff --git a/config/sidekiq.yml b/config/sidekiq.yml new file mode 100644 index 0000000..72f256c --- /dev/null +++ b/config/sidekiq.yml @@ -0,0 +1,3 @@ +:concurrency: 5 +:queues: + - default \ No newline at end of file diff --git a/db/migrate/20240830004510_add_disabled_to_links.rb b/db/migrate/20240830004510_add_disabled_to_links.rb new file mode 100644 index 0000000..56a4446 --- /dev/null +++ b/db/migrate/20240830004510_add_disabled_to_links.rb @@ -0,0 +1,5 @@ +class AddDisabledToLinks < ActiveRecord::Migration[7.1] + def change + add_column :links, :disabled, :boolean, default: false + end +end diff --git a/db/schema.rb b/db/schema.rb index f11c345..e82edf4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_08_27_163209) do +ActiveRecord::Schema[7.1].define(version: 2024_08_30_004510) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -31,6 +31,7 @@ t.datetime "updated_at", null: false t.integer "click_count", default: 0 t.datetime "last_click" + t.boolean "disabled", default: false t.index ["user_id"], name: "index_links_on_user_id" end From e03aee8d4723bfea9a35fa1c90edc7065646cef5 Mon Sep 17 00:00:00 2001 From: James McHugh Date: Thu, 29 Aug 2024 21:43:09 -0400 Subject: [PATCH 2/2] Added Procfile so that workers are configured during build --- Procfile | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Procfile diff --git a/Procfile b/Procfile new file mode 100644 index 0000000..fafa050 --- /dev/null +++ b/Procfile @@ -0,0 +1,2 @@ +web: bundle exec puma -C config/puma.rb +worker: bundle exec sidekiq -C config/sidekiq.yml \ No newline at end of file