-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathjob.rb
More file actions
126 lines (102 loc) · 2.84 KB
/
job.rb
File metadata and controls
126 lines (102 loc) · 2.84 KB
1
2
3
4
5
6
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# frozen_string_literal: true
class JobConfig < ActiveRecord::Base
def config=(config)
self.config_json = config if has_attribute?(:config_json)
super
end
def save(arg)
super(arg)
rescue Encoding::UndefinedConversionError
end
end
class Job < ActiveRecord::Base
class << self
SQL = {
queueable: 'RIGHT JOIN queueable_jobs on queueable_jobs.job_id = jobs.id'
}.freeze
def queueable
# sets jobs order based on priority first, ie: 5, nil, -5
jobs = where(state: :created).order(Arel.sql('COALESCE(priority, 0) desc')).order(:id)
jobs = jobs.joins(Arel.sql(SQL[:queueable])).order(:id) if ENV['USE_QUEUEABLE_JOBS']
jobs
end
def running
where(state: %i[queued received started]).order('jobs.id')
end
def private
where(private: true)
end
def public
where('jobs.private IS NULL OR jobs.private = ?', false)
end
def by_repo(id)
where(repository_id: id)
end
def by_owners(owners)
where(owned_by(owners))
end
def by_queue(queue)
where(queue:)
end
def owned_by(owners)
owners.map { |o| owner_id.eq(o.id).and(owner_type.eq(o.class.name)) }.inject(&:or)
end
def owner_id
arel_table[:owner_id]
end
def owner_type
arel_table[:owner_type]
end
end
FINISHED_STATES = %i[passed failed errored canceled].freeze
self.inheritance_column = :_disabled
belongs_to :repository
belongs_to :commit
belongs_to :source, polymorphic: true, autosave: true
belongs_to :owner, polymorphic: true
belongs_to :stage
belongs_to :config, foreign_key: :config_id, class_name: 'JobConfig'
has_one :queueable
serialize :config
serialize :debug_options
def paid?
owner&.paid? || false # prevents nil
end
def finished?
FINISHED_STATES.include?(state.try(:to_sym))
end
def queueable=(value)
if value
unless queueable
save!
create_queueable
end
queueable
else
Queueable.where(job_id: id).delete_all
end
rescue ActiveRecord::NotNullViolation
nil
end
def public?
!private?
end
def config
record = super
record = JSON.parse(record) if record.is_a?(String)
config = record&.config_json if record.respond_to?(:config_json) # TODO: remove once we've rolled over
config ||= record&.config
config ||= read_attribute(:config) if has_attribute?(:config)
config ||= {}
config = JSON.parse(config) if config.is_a?(String)
config.deep_symbolize_keys!
end
def name
config[:name]
end
def new_queueable
return if repository_id.blank? # to avoid trying save objects without repository_id
saved = new_record? ? save : true # saves if it is a new record
create_queueable if saved # it is allowed to create queueable records only for jobs which are persisting in the database
end
end