Skip to content

Commit

Permalink
Merge pull request #34 from turingschool/jm/disable_inactive
Browse files Browse the repository at this point in the history
  • Loading branch information
noahdurbin authored Aug 30, 2024
2 parents 3cd5e37 + e03aee8 commit 54f2697
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
23 changes: 23 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
web: bundle exec puma -C config/puma.rb
worker: bundle exec sidekiq -C config/sidekiq.yml
2 changes: 2 additions & 0 deletions app/models/link.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 14 additions & 0 deletions app/sidekiq/disable_inactive_links_job.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions config/initializers/sidekiq.rb
Original file line number Diff line number Diff line change
@@ -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'
)
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,4 +17,6 @@
get 'top_links', to: 'links#top_links'
end
end

mount Sidekiq::Web => '/sidekiq'
end
3 changes: 3 additions & 0 deletions config/sidekiq.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:concurrency: 5
:queues:
- default
5 changes: 5 additions & 0 deletions db/migrate/20240830004510_add_disabled_to_links.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddDisabledToLinks < ActiveRecord::Migration[7.1]
def change
add_column :links, :disabled, :boolean, default: false
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

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

0 comments on commit 54f2697

Please sign in to comment.