Skip to content

Commit

Permalink
DEV: Format Ruby files with syntax_tree gem
Browse files Browse the repository at this point in the history
The Discourse team has adopted the syntax_tree gem to format Ruby files.
  • Loading branch information
tgxworld committed Aug 6, 2024
1 parent 89d5dc7 commit 72cf427
Show file tree
Hide file tree
Showing 17 changed files with 244 additions and 303 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ jobs:
- name: Lint
run: bundle exec rubocop

- name: Syntax Tree
run: |
bundle exec stree check Gemfile $(git ls-files '*.rb') $(git ls-files '*.rake') $(git ls-files '*.thor')
test:
runs-on: ubuntu-latest
timeout-minutes: 5
Expand All @@ -35,7 +39,7 @@ jobs:

strategy:
matrix:
ruby: ["3.0", "3.1", "3.2", "3.3"]
ruby: ["3.1", "3.2", "3.3"]

steps:
- uses: actions/checkout@v4
Expand Down
2 changes: 2 additions & 0 deletions .streerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--print-width=100
--plugins=plugin/trailing_comma
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: true
source 'https://rubygems.org'
source "https://rubygems.org"

gemspec
5 changes: 2 additions & 3 deletions app/models/mini_scheduler/stat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
if defined?(ActiveRecord::Base)
module MiniScheduler
class Stat < ActiveRecord::Base

self.table_name = 'scheduler_stats'
self.table_name = "scheduler_stats"

def self.purge_old
where('started_at < ?', 1.months.ago).delete_all
where("started_at < ?", 1.months.ago).delete_all
end
end
end
Expand Down
13 changes: 8 additions & 5 deletions lib/generators/mini_scheduler/install/install_generator.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# frozen_string_literal: true

require 'rails/generators'
require 'rails/generators/migration'
require 'active_record'
require "rails/generators"
require "rails/generators/migration"
require "active_record"

module MiniScheduler
module Generators
class InstallGenerator < ::Rails::Generators::Base
include Rails::Generators::Migration
source_root File.expand_path('../templates', __FILE__)
source_root File.expand_path("../templates", __FILE__)
desc "Generate files for MiniScheduler"

def self.next_migration_number(path)
Expand All @@ -17,7 +17,10 @@ def self.next_migration_number(path)
end

def copy_migrations
migration_template("create_mini_scheduler_stats.rb", "db/migrate/create_mini_scheduler_stats.rb")
migration_template(
"create_mini_scheduler_stats.rb",
"db/migrate/create_mini_scheduler_stats.rb",
)
end

def copy_initializer_file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,5 @@
end

if Sidekiq.server? && defined?(Rails)
Rails.application.config.after_initialize do
MiniScheduler.start
end
Rails.application.config.after_initialize { MiniScheduler.start }
end
23 changes: 8 additions & 15 deletions lib/mini_scheduler.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
# frozen_string_literal: true
require "mini_scheduler/engine"
require 'mini_scheduler/schedule'
require 'mini_scheduler/schedule_info'
require 'mini_scheduler/manager'
require 'mini_scheduler/distributed_mutex'
require 'sidekiq'
require "mini_scheduler/schedule"
require "mini_scheduler/schedule_info"
require "mini_scheduler/manager"
require "mini_scheduler/distributed_mutex"
require "sidekiq"

begin
require 'sidekiq/exception_handler'
require "sidekiq/exception_handler"
rescue LoadError
end

module MiniScheduler

def self.configure
yield self
end
Expand Down Expand Up @@ -69,18 +68,12 @@ def self.start(workers: 1)
Manager.discover_queues.each do |queue|
manager = Manager.new(queue: queue, workers: workers)

schedules.each do |schedule|
if schedule.queue == queue
manager.ensure_schedule!(schedule)
end
end
schedules.each { |schedule| manager.ensure_schedule!(schedule) if schedule.queue == queue }

Thread.new do
while true
begin
if !self.skip_schedule || !self.skip_schedule.call
manager.tick
end
manager.tick if !self.skip_schedule || !self.skip_schedule.call
rescue => e
# the show must go on
handle_job_exception(e, message: "While ticking scheduling manager")
Expand Down
13 changes: 4 additions & 9 deletions lib/mini_scheduler/distributed_mutex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

module MiniScheduler
class DistributedMutex
class Timeout < StandardError; end
class Timeout < StandardError
end

@default_redis = nil

Expand All @@ -15,7 +16,7 @@ def self.synchronize(key, redis = nil, &blk)
end

def initialize(key, redis)
raise ArgumentError.new('redis argument is nil') if redis.nil?
raise ArgumentError.new("redis argument is nil") if redis.nil?
@key = key
@redis = redis
@mutex = Mutex.new
Expand All @@ -32,7 +33,6 @@ def synchronize
attempts = 0
sleep_duration = BASE_SLEEP_DURATION
while !try_to_get_lock

sleep(sleep_duration)

if sleep_duration < MAX_SLEEP_DURATION
Expand All @@ -44,7 +44,6 @@ def synchronize
end

yield

ensure
@redis.del @key
@mutex.unlock
Expand All @@ -62,9 +61,7 @@ def try_to_get_lock
@redis.watch @key
time = @redis.get @key
if time && time.to_i < Time.now.to_i
got_lock = @redis.multi do
@redis.set @key, Time.now.to_i + 60
end
got_lock = @redis.multi { @redis.set @key, Time.now.to_i + 60 }
end
ensure
@redis.unwatch
Expand All @@ -73,7 +70,5 @@ def try_to_get_lock

got_lock
end

end

end
Loading

0 comments on commit 72cf427

Please sign in to comment.