2014-12-25 23:41:00 +00:00
|
|
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
|
|
|
class Job < ApplicationModel
|
|
|
|
store :timeplan
|
|
|
|
store :condition
|
|
|
|
store :execute
|
2015-04-27 13:42:53 +00:00
|
|
|
validates :name, presence: true
|
2014-12-25 23:41:00 +00:00
|
|
|
|
|
|
|
before_create :updated_matching
|
|
|
|
before_update :updated_matching
|
|
|
|
|
2015-02-01 12:08:11 +00:00
|
|
|
notify_clients_support
|
2014-12-25 23:41:00 +00:00
|
|
|
|
|
|
|
def self.run
|
|
|
|
time = Time.new
|
|
|
|
day_map = {
|
|
|
|
0 => 'sun',
|
|
|
|
1 => 'mon',
|
|
|
|
2 => 'tue',
|
|
|
|
3 => 'wed',
|
|
|
|
4 => 'thu',
|
|
|
|
5 => 'fri',
|
|
|
|
6 => 'sat',
|
|
|
|
}
|
2015-04-27 13:42:53 +00:00
|
|
|
jobs = Job.where( active: true )
|
2014-12-25 23:41:00 +00:00
|
|
|
jobs.each do |job|
|
|
|
|
|
2015-02-01 12:08:11 +00:00
|
|
|
# only execute jobs, older then 1 min, to give admin posibility to change
|
|
|
|
next if job.updated_at > Time.now - 1.minutes
|
2014-12-25 23:41:00 +00:00
|
|
|
|
|
|
|
# check if jobs need to be executed
|
|
|
|
# ignore if job was running within last 10 min.
|
2015-02-01 12:08:11 +00:00
|
|
|
next if job.last_run_at && job.last_run_at > Time.now - 10.minutes
|
2014-12-25 23:41:00 +00:00
|
|
|
|
|
|
|
# check day
|
|
|
|
next if !job.timeplan['days'].include?( day_map[time.wday] )
|
|
|
|
|
|
|
|
# check hour
|
|
|
|
next if !job.timeplan['hours'].include?( time.hour.to_s )
|
|
|
|
|
|
|
|
# check min
|
|
|
|
next if !job.timeplan['minutes'].include?( match_minutes(time.min.to_s) )
|
|
|
|
|
|
|
|
# find tickets to change
|
2015-04-30 17:51:31 +00:00
|
|
|
tickets = Ticket.where( job.condition.permit! )
|
2015-05-01 12:27:57 +00:00
|
|
|
.order( '`tickets`.`created_at` DESC' )
|
|
|
|
.limit( 1_000 )
|
2014-12-25 23:41:00 +00:00
|
|
|
job.processed = tickets.count
|
|
|
|
tickets.each do |ticket|
|
2015-05-04 18:58:28 +00:00
|
|
|
logger.debug "CHANGE #{job.execute.inspect}"
|
2014-12-25 23:41:00 +00:00
|
|
|
changed = false
|
|
|
|
job.execute.each do |key, value|
|
|
|
|
changed = true
|
|
|
|
attribute = key.split('.', 2).last
|
2015-05-04 18:58:28 +00:00
|
|
|
logger.debug "-- #{Ticket.columns_hash[ attribute ].type}"
|
2014-12-25 23:41:00 +00:00
|
|
|
#value = 4
|
|
|
|
#if Ticket.columns_hash[ attribute ].type == :integer
|
2015-05-04 18:58:28 +00:00
|
|
|
# logger.debug "to i #{attribute}/#{value.inspect}/#{value.to_i.inspect}"
|
2014-12-25 23:41:00 +00:00
|
|
|
# #value = value.to_i
|
|
|
|
#end
|
|
|
|
ticket[attribute] = value
|
2015-05-04 18:58:28 +00:00
|
|
|
logger.debug "set #{attribute} = #{value.inspect}"
|
2014-12-25 23:41:00 +00:00
|
|
|
end
|
|
|
|
next if !changed
|
|
|
|
ticket.updated_by_id = 1
|
|
|
|
ticket.save
|
|
|
|
end
|
|
|
|
|
|
|
|
job.last_run_at = Time.now
|
|
|
|
job.save
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def updated_matching
|
|
|
|
count = Ticket.where( self.condition.permit! ).count
|
|
|
|
self.matching = count
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.match_minutes(minutes)
|
2015-04-27 13:20:16 +00:00
|
|
|
minutes.gsub!(/(\d)\d/, '\\1')
|
2014-12-25 23:41:00 +00:00
|
|
|
minutes.to_s + '0'
|
|
|
|
end
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|