Class: Mutx::Workers::MutxCron
- Inherits:
-
Object
- Object
- Mutx::Workers::MutxCron
- Includes:
- Sidekiq::Worker, Sidetiq::Schedulable
- Defined in:
- lib/mutx/background_jobs/workers/mutx_cron.rb
Instance Method Summary collapse
- #check_range_time(task) ⇒ Object
- #day_name ⇒ Object
-
#delete_started(started_task) ⇒ Object
validate.
- #execute(task) ⇒ Object
- #perform ⇒ Object
- #validate_and_execute(task) ⇒ Object
Instance Method Details
#check_range_time(task) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/mutx/background_jobs/workers/mutx_cron.rb', line 111 def check_range_time task init_hour = task[:start_time].match(/(\d*)/)[0] stop_hour = task[:stop_time].match(/(\d*)/)[0] @valid_hours_init = nil @valid_hours_stop = nil @valid = nil @valid_hours_init = [init_hour.to_i] (24-init_hour.to_i).times do if @valid_hours_init.last.eql? 23 @valid_hours_init << 00 else @valid_hours_init << @valid_hours_init.last + 1 end end @valid_hours_stop = [stop_hour.to_i] (stop_hour.to_i-1).times do @valid_hours_stop << @valid_hours_stop.last - 1 end @valid = @valid_hours_init + @valid_hours_stop.reverse @valid.pop if @valid.include? @@now_time.match(/(\d*)/)[0].to_i puts puts @valid puts @@now_time execute task else puts puts @valid puts "Actual time: #{@@now_time}" puts "ACTUAL TIME IS OUT OF RANGE FOR: #{task[:name]}" end end |
#day_name ⇒ Object
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/mutx/background_jobs/workers/mutx_cron.rb', line 88 def day_name() num = Chronic.parse("today").wday return "mo" if num.eql? 1 return "tu" if num.eql? 2 return "we" if num.eql? 3 return "th" if num.eql? 4 return "fr" if num.eql? 5 return "sa" if num.eql? 6 return "su" if num.eql? 0 end |
#delete_started(started_task) ⇒ Object
validate
171 172 173 174 175 |
# File 'lib/mutx/background_jobs/workers/mutx_cron.rb', line 171 def delete_started started_task started_task.each do |task| Mutx::Database::MongoConnector.delete_started_result task["_id"] end end |
#execute(task) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/mutx/background_jobs/workers/mutx_cron.rb', line 99 def execute task query_string = {} query_string = {"execution_name"=>"CRONNED-#{task[:cron_time]}-min"} task_name = task[:name] task_name.gsub!("%20"," ") puts puts "EXECUTING: #{task_name} right now" puts #puts Mutx::API::Tasks.cron_update task Mutx::API::Execution.start task_name, query_string end |
#perform ⇒ Object
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 |
# File 'lib/mutx/background_jobs/workers/mutx_cron.rb', line 15 def perform #def self.checker Mutx::Database::MongoConnector.new Mutx::Support::Configuration.db_connection_data running_task = [] #running_task = Mutx::Database::MongoConnector.running_now puts "######### STARTING CRON ##########" if running_task.eql? [] @days = [:mo,:tu,:we,:th,:fr,:sa,:su] @today = day_name #name of today #BEGIN# Select only cronned tasks that last_exec_time is OK to run cron_tasks = [] cron_tasks_list = Mutx::Database::MongoConnector.cron_tasks puts "#{cron_tasks_list.size} cronned task searched" cron_tasks_list.select{|line| cron_tasks << line if ( (((Time.now.utc - line[:last_exec_time].utc) + 1) >= (line[:cron_time].to_i * 60)) && (line[:task_status].eql? "on") )} cron_tasks_list = [] cron_tasks_list = cron_tasks puts "#{cron_tasks_list.size} POSSIBLE cronned task on time to run" #END# Select only cronned tasks that last_exec_time is OK to run #BEGIN# REMOVING POSSIBLE DUPLICATED TASKS cron_tasks_list_aux = [] cron_tasks_list_aux = cron_tasks_list cron_tasks_list = [] cron_tasks_list = cron_tasks_list_aux.uniq { |line| [line[:name]].join(":") } puts puts "REMOVING DUPLICATED TASKS WITH SAME NAME" if !cron_tasks_list_aux.size.eql? cron_tasks_list.size puts "#{cron_tasks_list.size} cronned task after removing possible duplicated tasks" #END# REMOVING POSSIBLE DUPLICATED TASKS #BEGIN#Check if task is running or started cron_tasks = [] cron_tasks_list.select{|line| cron_tasks << line if ((Mutx::Database::MongoConnector.running_for_task line[:name]) && (Mutx::Database::MongoConnector.only_started_for_task line[:name]) && (Mutx::Database::MongoConnector.only_running_for_task line[:name])).eql? []} cron_tasks_list = [] cron_tasks_list = cron_tasks puts "#{cron_tasks_list.size} cronned task ready to run (not running or started)" #END#Check if task is running or started # TASK must be right on time to run and unique cron_tasks_list.each do |task| @@now_time = Time.now.hour.to_s+":"+Time.now.min.to_s @@now_time = ("0" + @@now_time.match(/\d*/).to_s + ":" + @@now_time.match(/\:(\d*)/)[1].to_s) if @@now_time.match(/(\d*)/)[1].size.eql? 1 @@now_time = (@@now_time.match(/\d*/).to_s + ":0" + @@now_time.match(/\:(\d*)/)[1].to_s) if @@now_time.match(/\:(\d*)/)[1].size.eql? 1 @@last_exec_time = nil @@last_exec_time = task[:last_exec_time] # Update last_exec_time before to run (because this task is OK to run) Mutx::API::Tasks.cron_update task if (((Time.now.utc - @@last_exec_time.utc) + 1) >= (task[:cron_time].to_i * 60)) @none_day = true sleep 1 validate_and_execute task end#cron_task Mutx::Database::MongoConnector.force_close puts "######### ENDING CRON ##########" else ## si hay algo corriendo deja pasar toda la ejecucion hasta que finalice puts puts "HAY EJECUCIONES EN RUNNING O STARTED, SE ESPERA A QUE TERMINEN..." puts puts "######### ENDING CRON ##########" Mutx::Database::MongoConnector.force_close end end |
#validate_and_execute(task) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/mutx/background_jobs/workers/mutx_cron.rb', line 148 def validate_and_execute task @days.detect{|day| @none_day = "task_with_day" if task[:"#{day}"]} # If no day execution is seted, works as always if (((!task[:cron_time].eql? "") && (!task[:cron_time].eql? "0")) && (((Time.now.utc - @@last_exec_time.utc) + 1) >= (task[:cron_time].to_i * 60)) && (@none_day.eql? true)) execute task # If there is a day seted, and that day is today, and no execution time is seted elsif (((!task[:cron_time].eql? "") && (!task[:cron_time].eql? "0")) && (((Time.now.utc - @@last_exec_time.utc) + 1) >= (task[:cron_time].to_i * 60)) && (@none_day.eql? "task_with_day") && (task[:"#{@today}"].eql? "on") && (task[:start_time].empty?) && (task[:stop_time].empty?)) execute task # If there is a day seted, and that day is today, and execution range time is seted elsif (((!task[:cron_time].eql? "") && (!task[:cron_time].eql? "0")) && (((Time.now.utc - @@last_exec_time.utc) + 1) >= (task[:cron_time].to_i * 60)) && (@none_day.eql? "task_with_day") && (task[:"#{@today}"].eql? "on") && (!task[:start_time].nil?) && (!task[:stop_time].nil?) ) if task[:start_time] > task[:stop_time] #initial time is greater than stop time check_range_time task elsif (@@now_time.between? task[:start_time], task[:stop_time]) #stop time is greater than initial time execute task else puts "Out of date range for: #{task[:name]}" end end#if end |