Class: Jobs
- Inherits:
-
Object
- Object
- Jobs
- Defined in:
- lib/zfben_rails_jobs.rb
Instance Method Summary collapse
- #add(opts = {}) ⇒ Object
- #data ⇒ Object
- #destroy ⇒ Object
- #finished? ⇒ Boolean
- #id ⇒ Object
- #import(id) ⇒ Object
- #list ⇒ Object
- #locked? ⇒ Boolean
- #path ⇒ Object
- #result ⇒ Object
- #run ⇒ Object
- #save ⇒ Object
Instance Method Details
#add(opts = {}) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/zfben_rails_jobs.rb', line 31 def add opts = {} unless locked? [:class, :method].each{ |key| opts[key] = opts[key].to_s.to_sym } @data[:list].push opts true else false end end |
#data ⇒ Object
68 69 70 |
# File 'lib/zfben_rails_jobs.rb', line 68 def data @data end |
#destroy ⇒ Object
53 54 55 56 57 58 |
# File 'lib/zfben_rails_jobs.rb', line 53 def destroy if ::File.exists? path ::File.delete path end @data = merge end |
#finished? ⇒ Boolean
98 99 100 101 |
# File 'lib/zfben_rails_jobs.rb', line 98 def finished? r = result r[:failed] == 0 && r[:pending] == 0 end |
#id ⇒ Object
60 61 62 |
# File 'lib/zfben_rails_jobs.rb', line 60 def id @data[:id] end |
#import(id) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/zfben_rails_jobs.rb', line 22 def import id if File.exists?(@data[:dir] + id) @data = merge ::YAML::load_file(@data[:dir] + id) true else false end end |
#list ⇒ Object
64 65 66 |
# File 'lib/zfben_rails_jobs.rb', line 64 def list @data[:list] end |
#locked? ⇒ Boolean
103 104 105 |
# File 'lib/zfben_rails_jobs.rb', line 103 def locked? @data[:locked] end |
#path ⇒ Object
72 73 74 |
# File 'lib/zfben_rails_jobs.rb', line 72 def path @data[:dir] + @data[:id] end |
#result ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/zfben_rails_jobs.rb', line 76 def result successed = 0 failed = 0 pending = 0 @data[:list].each{ |job| next if job.nil? if job.has_key? :status case job[:status] when :successed successed = successed + 1 when :failed failed = failed + 1 else pending = pending + 1 end else pending = pending + 1 end } { successed: successed, failed: failed, pending: pending, total: @data[:list].length } end |
#run ⇒ Object
107 108 109 110 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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/zfben_rails_jobs.rb', line 107 def run unless @data[:at].nil? if ::Time.now < @data[:at] return false end end @data[:locked] = true @data[:list].map!{ |job| if !job.has_key?(:status) || job[:status] != :successed if job[:class] != :eval unless Object.const_defined?(job[:class]) job[:status] = :failed job[:status_msg] = 'Class missing' next end obj = Object.const_get(job[:class]) unless obj.respond_to?(job[:method]) job[:status] = :failed job[:status_msg] = 'Method missing' next end begin obj.send job[:method], *job[:args] rescue => e job[:status] = :failed job[:status_msg] = e next end else begin Kernel.eval job[:method].to_s rescue => e job[:status] = :failed job[:status_msg] = e next end end job[:status] = :successed end job } if ::File.exists? path if finished? ::File.delete path else ::File.open(path, 'w'){ |f| f.write data.to_yaml } end end true end |
#save ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/zfben_rails_jobs.rb', line 43 def save unless locked? @data[:locked] = true ::File.open(path, 'w'){ |f| f.write @data.to_yaml } true else false end end |