trabajo-afectivo/app/models/job.rb

88 lines
2.3 KiB
Ruby
Raw Normal View History

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
validates :name, presence: true
2014-12-25 23:41:00 +00:00
before_create :updated_matching
before_update :updated_matching
notify_clients_support
2014-12-25 23:41:00 +00:00
def self.run
time = Time.zone.now
2014-12-25 23:41:00 +00:00
day_map = {
0 => 'sun',
1 => 'mon',
2 => 'tue',
3 => 'wed',
4 => 'thu',
5 => 'fri',
6 => 'sat',
}
jobs = Job.where( active: true )
2014-12-25 23:41:00 +00:00
jobs.each do |job|
# only execute jobs, older then 1 min, to give admin posibility to change
2015-11-30 13:29:23 +00:00
next if job.updated_at > Time.zone.now - 1.minute
2014-12-25 23:41:00 +00:00
# check if jobs need to be executed
# ignore if job was running within last 10 min.
next if job.last_run_at && job.last_run_at > Time.zone.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
tickets = Ticket.where( job.condition.permit! )
.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.zone.now
2014-12-25 23:41:00 +00:00
job.save
end
true
end
private
def updated_matching
count = Ticket.where( condition.permit! ).count
2014-12-25 23:41:00 +00:00
self.matching = count
end
def self.match_minutes(minutes)
minutes.gsub!(/(\d)\d/, '\\1')
2014-12-25 23:41:00 +00:00
minutes.to_s + '0'
end
private_class_method :match_minutes
end