1class ArchiveSignaturesJob < ApplicationJob
 
2  queue_as :low_priority
 
3
  • DuplicateMethodCall - calls 'Time.current' 2 times » reek
  • DuplicateMethodCall - calls 'petition.signatures' 3 times » reek
  • DuplicateMethodCall - calls 'petition.signatures.unarchived' 3 times » reek
  • DuplicateMethodCall - calls 'petition.signatures.unarchived.exists?' 2 times » reek
  • FeatureEnvy - refers to 'signature' more than self (maybe move it to another class?) » reek
  • NestedIterators - contains iterators nested 2 deep » reek
  • TooManyStatements - has approx 39 statements » reek
  • UncommunicativeVariableName - has the variable name 's' » reek
  • Complexity 10 » saikuro
  • Method "perform" has 63 lines. It should have 20 or less. » roodi
4  def perform(petition, archived_petition, limit: 1000)
 
5    terminating = false
 
6
 
7    worker = trap("TERM") do
 
8      terminating = true
 
 9      worker.call
 
10    end
 
 
12    Appsignal.without_instrumentation do
 
13      if petition.signatures.unarchived.exists?
 
14        signatures = petition.signatures.unarchived.batch(limit: limit)
 
 
16        signatures.each do |signature|
 
17          signature.with_lock do
 
18            unless signature.archived_at?
 
19              archived_signature = Archived::Signature.new do |s|
 
20                s.petition_id = signature.petition_id
 
21                s.uuid = signature.uuid
 
22                s.state = signature.state
 
23                s.number = signature.number
 
24                s.name = signature.name
 
25                s.email = signature.email
 
26                s.postcode = signature.postcode
 
27                s.location_code = signature.location_code
 
28                s.constituency_id = signature.constituency_id
 
29                s.ip_address = signature.ip_address
 
30                s.perishable_token = signature.perishable_token
 
31                s.unsubscribe_token = signature.unsubscribe_token
 
32                s.notify_by_email = signature.notify_by_email
 
33                s.validated_at = signature.validated_at
 
34                s.invalidation_id = signature.invalidation_id
 
35                s.invalidated_at = signature.invalidated_at
 
36                s.government_response_email_at = signature.government_response_email_at
 
37                s.debate_scheduled_email_at = signature.debate_scheduled_email_at
 
38                s.debate_outcome_email_at = signature.debate_outcome_email_at
 
39                s.petition_email_at = signature.petition_email_at
 
40                s.creator = signature.creator?
 
41                s.sponsor = signature.sponsor?
 
42                s.created_at = signature.created_at
 
43                s.updated_at = signature.updated_at
 
44              end
 
 
46              archived_signature.save!(validate: false)
 
47              signature.update_column(:archived_at, Time.current)
 
48            end
 
49          end
 
 
51          if terminating
 
52            reschedule_job(petition, archived_petition, limit: limit)
 
53            return true
 
54          end
 
55        end
 
56      end
 
57    end
 
 
59    if petition.signatures.unarchived.exists?
 
60      self.class.perform_later(petition, archived_petition, limit: limit)
 
61    else
 
62      petition.update_column(:archived_at, Time.current)
 
63    end
 
 
65  ensure
 
66    trap "TERM", worker
 
67  end
 
 
69  private
 
  • LongParameterList - has 4 parameters » reek
  • UnusedParameters - has unused parameter 'limit' » reek
  • Complexity 1 » saikuro
71  def reschedule_job(petition, archived_petition, limit: 1000, wait_until: 5.minutes.from_now)
 
72    self.class.set(wait_until: wait_until).perform_later(petition, archived_petition)
 
73  end
 
74end