Skip to content

Commit

Permalink
background_job: stop using a class var
Browse files Browse the repository at this point in the history
This causes problems when using the async mode because all the fibers
share that @Args class var. Luckily, with the current code, the only
attribute affected is "enqueue_time", which is not
accurate anyway (see #18).

Even without taking the async mode into consideration using a class var
is counter-intuitive. The arguments of a particular job (service ID, app
ID, etc.) should belong to an instance, they shouldn't be stored in a class var.
  • Loading branch information
davidor committed Apr 14, 2020
1 parent 9b56e59 commit 484d359
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
15 changes: 7 additions & 8 deletions lib/3scale/backend/background_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ class BackgroundJob

class << self
def perform(*args)
@args = args
perform_wrapper
perform_wrapper(args)
end

def perform_logged
def perform_logged(*_args)
raise "This should be overloaded."
end

Expand All @@ -25,13 +24,13 @@ def hooks

private

def enqueue_time
@args.last or raise('Enqueue time not specified')
def enqueue_time(args)
args.last or raise('Enqueue time not specified')
end

def perform_wrapper
def perform_wrapper(args)
start_time = Time.now.getutc
status_ok, message = perform_logged(*@args)
status_ok, message = perform_logged(*args)
stats_mem = Memoizer.stats
end_time = Time.now.getutc

Expand All @@ -41,7 +40,7 @@ def perform_wrapper
if status_ok
Worker.logger.info(prefix +
" #{(end_time - start_time).round(5)}" +
" #{(end_time.to_f - enqueue_time).round(5)}"+
" #{(end_time.to_f - enqueue_time(args)).round(5)}"+
" #{stats_mem[:size]} #{stats_mem[:count]} #{stats_mem[:hits]}")

if configuration.worker_prometheus_metrics.enabled
Expand Down
4 changes: 2 additions & 2 deletions lib/3scale/backend/stats/partition_eraser_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ def validate_job(job, offset, length)
end
end

def enqueue_time
@args[0]
def enqueue_time(args)
args[0]
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/3scale/backend/stats/partition_generator_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def perform_logged(_enqueue_time, service_id, applications, metrics,

private

def enqueue_time
@args[0]
def enqueue_time(args)
args[0]
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/3scale/backend/transactor/report_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def perform_logged(service_id, raw_transactions, _enqueue_time, context_info = {

private

def enqueue_time
@args[2]
def enqueue_time(args)
args[2]
end

def parse_transactions(service_id, raw_transactions, request_info)
Expand Down

0 comments on commit 484d359

Please sign in to comment.