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

rysnc the pinned directory on deploy, clean unused pins in queue #10071

Merged
merged 3 commits into from
Dec 31, 2023
Merged
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: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ prod_rack_up_b_d : prod_link data_dir ensure_prod_env
# remotely deploy latest master in prod
prod_deploy : clean
$(CONTAINER_COMPOSE) run rack rake precompile && \
rsync --verbose --checksum public/assets/*.js public/assets/*.js.gz public/assets/version.json deploy@18xx:~/18xx/public/assets/ && \
rsync --verbose --checksum public/assets/*.js public/assets/*.js.gz public/assets/version.json public/pinned/*.js.gz deploy@18xx:~/18xx/public/assets/ && \
ssh -l deploy 18xx "source ~/.profile && cd ~/18xx/ && git pull && make prod_rack_up_b_d"

style:
Expand Down
21 changes: 21 additions & 0 deletions queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'logger'
require 'rufus-scheduler'
require 'require_all'
require 'sequel'
require_relative 'models'
require_rel './models'
require_relative 'lib/assets'
Expand All @@ -11,6 +12,8 @@
require_relative 'lib/mail'
require_relative 'lib/user_stats'

Sequel.extension :pg_json_ops

PRODUCTION = ENV['RACK_ENV'] == 'production'

LOGGER = Logger.new($stdout)
Expand All @@ -21,6 +24,8 @@

scheduler = Rufus::Scheduler.new

PINS_TO_KEEP = 3
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn’t you keep all pins that are still being played?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This constant ensures the latest pins are kept even if they're not used, otherwise all unused pins are discarded. I've added a comment and updated the constant name to clarify this.


def days_ago(days)
Time.now - (86_400 * days)
end
Expand All @@ -40,6 +45,22 @@ def days_ago(days)
Sequel.lit(filter, finished: days_ago(365), active: days_ago(90))
).all.each(&:archive!)

# clean up unused pins, except for the newest few
PINS_DIR = File.join(File.dirname(__FILE__), '..', 'public', 'pinned')
pins = Dir.glob("#{PINS_DIR}/*.js.gz")
.sort_by{ |f| File.mtime(f) }[0..-(1 + PINS_TO_KEEP)]
.map { |f| f.split(/[.\/]/)[-3] }
pins.each do |pin|
where_kwargs = {
Sequel.pg_jsonb_op(:settings).get_text('pin') => pin,
status: %w[active finished],
}
if Game.where(**where_kwargs).count.zero?
file = "#{PINS_DIR}/#{pin}.js.gz"
FileUtils.rm(file)
end
end

Game.where(status: 'new').all.each do |game|
if game.settings['unlisted']
game.destroy if game.created_at < days_ago(180)
Expand Down
Loading