Skip to content

Commit

Permalink
Removed Sorbet for release
Browse files Browse the repository at this point in the history
  • Loading branch information
zachahn committed Aug 11, 2024
1 parent 8e65ec8 commit d2c63bf
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 142 deletions.
2 changes: 0 additions & 2 deletions app/models/disqualified/base_record.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# typed: strict

class Disqualified::BaseRecord < ActiveRecord::Base
self.abstract_class = true
end
12 changes: 0 additions & 12 deletions app/models/disqualified/record.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
# typed: strict

class Disqualified::Record < Disqualified::BaseRecord
extend T::Sig

self.table_name = "disqualified_jobs"

scope :runnable, -> { where(finished_at: nil, run_at: (..Time.now), locked_by: nil) }

sig do
params(id: T.nilable(T.any(Integer, String))).returns(Disqualified::Record)
end
def self.claim_one!(id: nil)
run_id = SecureRandom.uuid
association =
Expand All @@ -34,33 +27,28 @@ def self.claim_one!(id: nil)
Disqualified::Record.find_by!(locked_by: run_id)
end

sig { returns(Disqualified::Record) }
def run!
record = self.class.claim_one!(id:)
record.send(:instantiate_handler_and_perform_with_args)
record.finish
record
end

sig { void }
def finish
update!(locked_by: nil, locked_at: nil, finished_at: Time.now)
end

sig { void }
def requeue
retry_count = attempts - 1
sleep = (retry_count**4) + 15 + (rand(10) * (retry_count + 1))
unqueue(run_at: Time.now + sleep)
end

sig { params(run_at: T.nilable(Time)).void }
def unqueue(run_at: nil)
run_at ||= Time.now
update!(locked_by: nil, locked_at: nil, run_at:)
end

sig { void }
private def instantiate_handler_and_perform_with_args
raise Disqualified::Error::JobAlreadyFinished if !finished_at.nil?
raise Disqualified::Error::JobNotClaimed if locked_by.nil?
Expand Down
2 changes: 0 additions & 2 deletions lib/disqualified.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# typed: strict

module Disqualified
end

Expand Down
9 changes: 0 additions & 9 deletions lib/disqualified/active_job.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
# typed: strict

require "active_job"
require "disqualified"

class Disqualified::ActiveJobAdapter
extend T::Sig
include Disqualified::Job

sig { params(serialized_job_data: T::Hash[String, T.untyped]).void }
def perform(serialized_job_data)
::ActiveJob::Base.execute(serialized_job_data)
end
Expand All @@ -16,19 +12,14 @@ def perform(serialized_job_data)
module ActiveJob
module QueueAdapters
class DisqualifiedAdapter
extend T::Sig

sig { returns(T::Boolean) }
def enqueue_after_transaction_commit?
Disqualified.client_options.enqueue_after_transaction_commit
end

sig { params(job_data: ActiveJob::Base).void }
def enqueue(job_data)
Disqualified::ActiveJobAdapter.perform_async(job_data.serialize)
end

sig { params(job_data: ActiveJob::Base, timestamp: Numeric).void }
def enqueue_at(job_data, timestamp)
timestamp = Time.at(timestamp)
Disqualified::ActiveJobAdapter.perform_at(timestamp, job_data.serialize)
Expand Down
15 changes: 4 additions & 11 deletions lib/disqualified/cli.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# typed: strict

class Disqualified::CLI
extend T::Sig
include Disqualified::Logging

sig { void }
def self.run
cli = new(ARGV)
cli.run
Expand All @@ -16,18 +12,16 @@ class ServerEngine < Rails::Engine
end
end

sig { params(argv: T::Array[String]).void }
def initialize(argv)
@original_argv = argv
end

sig { void }
def run
require File.join(Dir.pwd, "config/environment")

option_parser.parse(@original_argv)

server_options = T.must(Disqualified.server_options)
server_options = Disqualified.server_options
delay_range = server_options.delay_range
error_hooks = server_options.error_hooks
logger = server_options.logger
Expand Down Expand Up @@ -57,14 +51,13 @@ def run

private

sig { returns(OptionParser) }
def option_parser
return T.must(@option_parser) if instance_variable_defined?(:@option_parser)
return @option_parser if instance_variable_defined?(:@option_parser)

option_parser = OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename($0)} [OPTIONS]"

server_options = T.must(Disqualified.server_options)
server_options = Disqualified.server_options

opts.on("--delay-low SECONDS", Numeric, "Default: #{server_options.delay_low}") do |value|
server_options.delay_low = value
Expand All @@ -84,6 +77,6 @@ def option_parser
end
end

@option_parser ||= T.let(option_parser, T.nilable(OptionParser))
@option_parser ||= option_parser
end
end
15 changes: 3 additions & 12 deletions lib/disqualified/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
# typed: strict

module Disqualified
extend T::Sig

@client_options = T.let(ClientConfiguration.new, ClientConfiguration)
@client_options = ClientConfiguration.new

class << self
extend T::Sig

sig { returns(Disqualified::ClientConfiguration) }
attr_accessor :client_options

sig { returns(T.nilable(Disqualified::ServerConfiguration)) }
attr_accessor :server_options

sig { params(block: T.proc.params(arg0: Disqualified::ClientConfiguration).void).void }
def configure_client(&block)
block.call(client_options)
end

# While client options are always run, server options only run in the "disqualified" process
sig { params(block: T.proc.params(arg0: Disqualified::ServerConfiguration).void).void }

def configure_server(&block)
if server_options
block.call(T.must(server_options))
block.call(server_options)
end
end
end
Expand Down
37 changes: 12 additions & 25 deletions lib/disqualified/configuration_structs.rb
Original file line number Diff line number Diff line change
@@ -1,56 +1,43 @@
# typed: strict

class Disqualified::ClientConfiguration
extend T::Sig

sig { void }
def initialize
@enqueue_after_transaction_commit = T.let(false, T::Boolean)
@enqueue_after_transaction_commit = false
end

sig { returns(T::Boolean) }
attr_accessor :enqueue_after_transaction_commit
end

class Disqualified::ServerConfiguration
extend T::Sig

sig { void }
def initialize
@delay_high = T.let(5.0, Numeric)
@delay_low = T.let(1.0, Numeric)
@logger = T.let(Rails.logger, T.untyped)
@pool_size = T.let(5, Integer)
@pwd = T.let(Dir.pwd, String)
@error_hooks = T.let([], T::Array[Disqualified::Logging::ERROR_HOOK_TYPE])
@delay_high = 5.0
@delay_low = 1.0
@logger = Rails.logger
@pool_size = 5
@pwd = Dir.pwd
@error_hooks = []
end

sig { returns(Numeric) }
attr_accessor :delay_high
sig { returns(Numeric) }

attr_accessor :delay_low
sig { returns(T::Array[Disqualified::Logging::ERROR_HOOK_TYPE]) }

attr_accessor :error_hooks
sig { returns(T.untyped) }

attr_accessor :logger
sig { returns(Integer) }

attr_accessor :pool_size
sig { returns(String) }

attr_accessor :pwd

private :error_hooks=

sig { returns(T::Range[Float]) }
def delay_range
delay_low.to_f..delay_high.to_f
end

sig { params(block: Disqualified::Logging::ERROR_HOOK_TYPE).void }
def on_error(&block)
error_hooks.push(block)
end

sig { returns(String) }
def to_s
"{ delay: #{delay_range}, pool_size: #{pool_size}, error_hooks_size: #{error_hooks.size} }"
end
Expand Down
2 changes: 0 additions & 2 deletions lib/disqualified/engine.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# typed: strict

module Disqualified
class Engine < ::Rails::Engine
end
Expand Down
2 changes: 0 additions & 2 deletions lib/disqualified/error.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# typed: strict

module Disqualified
module Error
class DisqualifiedError < StandardError
Expand Down
19 changes: 6 additions & 13 deletions lib/disqualified/job.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
# typed: strict

module Disqualified::Job
extend T::Helpers

module ClassMethods
extend T::Sig

sig { params(the_time: T.any(Time, Date, ActiveSupport::TimeWithZone), args: T.untyped).void }
def perform_at(the_time, *args)
Disqualified::Record.create!(
handler: T.unsafe(self).name,
handler: name,
arguments: JSON.dump(args),
queue: "default",
run_at: the_time
)
end

sig { params(args: T.untyped).void }
def perform_async(*args)
T.unsafe(self).perform_at(Time.now, *args)
perform_at(Time.now, *args)
end

sig { params(delay: T.any(Numeric, ActiveSupport::Duration), args: T.untyped).void }
def perform_in(delay, *args)
T.unsafe(self).perform_at(T.unsafe(Time.now) + delay, *args)
perform_at(Time.now + delay, *args)
end
end

mixes_in_class_methods(ClassMethods)
def self.included(klass)
klass.extend(ClassMethods)
end
end
6 changes: 0 additions & 6 deletions lib/disqualified/logging.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
# typed: strict

module Disqualified::Logging
extend T::Sig

ERROR_CONTEXT_TYPE = T.type_alias do
T::Hash[T.untyped, T.untyped]
end
Expand All @@ -13,7 +9,6 @@ module Disqualified::Logging

module_function

sig { params(parts: T.untyped).void }
def format_log(*parts)
*extras, message = parts

Expand All @@ -24,7 +19,6 @@ def format_log(*parts)
end
end

sig { params(error_hooks: T::Array[ERROR_HOOK_TYPE], error: Exception, context: ERROR_CONTEXT_TYPE).void }
def handle_error(error_hooks, error, context)
error_hooks.each do |hook|
hook.call(error, context)
Expand Down
5 changes: 0 additions & 5 deletions lib/disqualified/main.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
# typed: strict

class Disqualified::Main
extend T::Sig
include Disqualified::Logging

sig { params(error_hooks: T::Array[Disqualified::Logging::ERROR_HOOK_TYPE], logger: T.untyped).void }
def initialize(error_hooks:, logger:)
@error_hooks = error_hooks
@logger = logger
end

sig { void }
def call
Rails.application.reloader.wrap do
begin
Expand Down
Loading

0 comments on commit d2c63bf

Please sign in to comment.