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 redis and sidekiq to clean up expired blacklisted jwt #171

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ api/config/master.key
api/.DS_Store
api/.byebug_history

api/db/dump.rdb


####### client #######
# dependencies
Expand Down
3 changes: 3 additions & 0 deletions api/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,6 @@ gem "active_model_serializers", "~> 0.10.13"
gem 'byebug', '~> 11.1', '>= 11.1.3'

gem 'whenever', require: false

gem "sidekiq", "~> 7.1"
gem 'sidekiq-cron'
20 changes: 20 additions & 0 deletions api/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,18 @@ GEM
activesupport
chronic (0.10.2)
concurrent-ruby (1.2.2)
connection_pool (2.4.1)
crass (1.0.6)
date (3.3.3)
debug (1.7.1)
irb (>= 1.5.0)
reline (>= 0.3.1)
erubi (1.12.0)
et-orbi (1.2.7)
tzinfo
fugit (1.8.1)
et-orbi (~> 1, >= 1.2.7)
raabro (~> 1.4)
globalid (1.1.0)
activesupport (>= 5.0)
i18n (1.12.0)
Expand Down Expand Up @@ -129,6 +135,7 @@ GEM
pg (1.4.6)
puma (5.6.5)
nio4r (~> 2.0)
raabro (1.4.0)
racc (1.6.2)
rack (2.2.6.3)
rack-cors (2.0.1)
Expand Down Expand Up @@ -162,8 +169,19 @@ GEM
thor (~> 1.0)
zeitwerk (~> 2.5)
rake (13.0.6)
redis-client (0.14.1)
connection_pool
reline (0.3.2)
io-console (~> 0.5)
sidekiq (7.1.1)
concurrent-ruby (< 2)
connection_pool (>= 2.3.0)
rack (>= 2.2.4)
redis-client (>= 0.14.0)
sidekiq-cron (1.10.1)
fugit (~> 1.8)
globalid (>= 1.0.1)
sidekiq (>= 6)
thor (1.2.1)
timeout (0.3.2)
tzinfo (2.0.6)
Expand Down Expand Up @@ -191,6 +209,8 @@ DEPENDENCIES
puma (~> 5.0)
rack-cors
rails (~> 7.0.4, >= 7.0.4.2)
sidekiq (~> 7.1)
sidekiq-cron
tzinfo-data
whenever

Expand Down
9 changes: 9 additions & 0 deletions api/app/workers/cleanup_expired_jwt_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CleanupExpiredJwtJob
include Sidekiq::Worker

def perform(*args)
puts "Running cleanup_expired_jwt"
BlacklistedToken.where("expires_at < ? ", Time.current).delete_all
puts "Delete expired tokens"
end
end
6 changes: 6 additions & 0 deletions api/config/initializers/sidekiq_cron.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Sidekiq::Cron::Job.create(
name: 'Clean up expired tokens daily',
cron: '0 3 * * *', # Run at 3:00AM UTC daily
# cron: '*/1 * * * *', # Run every 1 minute
class: 'CleanupExpiredJwtJob'
)
3 changes: 3 additions & 0 deletions api/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
require 'sidekiq/web'

Rails.application.routes.draw do

#action cable server
mount ActionCable.server => "/cable"
mount Sidekiq::Web => '/sidekiq'

resources :messages
resources :likes, only: [:create, :destroy]
Expand Down
11 changes: 11 additions & 0 deletions api/config/sidekiq.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

:concurrency: 10
:pidfile: ./tmp/pids/sidekiq.pid
:logfile: ./log/sidekiq.log
:queues:
- default

:redis:
:url: redis://redis:6379
:driver: redis
:dir: ./api/db/redis
29 changes: 29 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
services:

db:
image: postgres
volumes:
- ./api/tmp/db:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: password
working_dir: /api

redis:
image: 'redis:7-alpine'
command: redis-server
ports:
- '6379:6379'

helper:
image: alpine
command: sh -c "echo never > /sys/kernel/mm/transparent_hugepage/enabled"
privileged: true

api:
image: rails-api-backend
build: api
Expand All @@ -19,6 +32,12 @@ services:
working_dir: /api
depends_on:
- db
- sidekiq
- redis
environment:
- RAILS_ENV=development
- REDIS_URL=redis://redis:6379/

client:
build: client
image: react-client-frontend
Expand All @@ -30,3 +49,13 @@ services:
- ./client:/client
environment:
POSTGRES_PASSWORD: password

sidekiq:
build: api
command: bundle exec sidekiq -C config/sidekiq.yml
volumes:
- './api:/api'
- '/api/tmp'
- './api/db:/api/db'
environment:
REDIS_URL: redis://redis:6379