Class: RecalcAbilitiesJob

Inherits:
ApplicationJob show all
Defined in:
/var/apps/qpixel/app/jobs/recalc_abilities_job.rb

Instance Method Summary collapse

Methods inherited from ApplicationJob

#exec, #initialize, #logger

Constructor Details

This class inherits a constructor from ApplicationJob

Instance Method Details

#perform(**options) ⇒ Object

Perform ability recalculations.

Parameters:

  • options (Hash)

    additional options for the job. Currently supports verbose and quiet.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File '/var/apps/qpixel/app/jobs/recalc_abilities_job.rb', line 7

def perform(**options)
  resolved = []
  destroy = []
  all = AbilityQueue.pending.to_a

  all.each do |q|
    cu = q.community_user
    u = cu&.user

    if cu.nil? || u.nil?
      destroy << q.id
      next
    end

    RequestContext.community = cu.community

    if options[:verbose] && !options[:quiet]
      logger.debug "Scope: Community     : #{cu.community.name} (#{cu.community.host})"
      logger.debug "       User          : #{u.username} (#{cu.user_id})"
      logger.debug "       CommunityUser : #{cu.id}"
    elsif !options[:verbose] && !options[:quiet]
      logger.debug "Scope: CommunityUser : #{cu.id}"
    end

    cu.recalc_abilities!

    # Grant mod ability if mod status is given
    if cu.at_least_moderator? && !cu.ability?('mod')
      cu.grant_ability!('mod')
    end

    resolved << q.id
  rescue => e
    logger.error "  Failed: #{e.class.name}: #{e.message}"
    logger.error e.backtrace

    if Rails.env.test?
      raise e
    end
  end

  AbilityQueue.where(id: resolved).update(completed: true)
  AbilityQueue.where(id: destroy).delete_all

  unless options[:quiet]
    logger.info "Completed, #{resolved.size}/#{all.size} tasks successful, #{destroy.size} tasks invalid"
  end
end