Class: WebappWorker::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/webapp_worker/job.rb

Constant Summary collapse

DIVISION =
/\d+-\d+[\/]\d+/
DIGIT =
/^\d+$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_supplied_hash = {}) ⇒ Job

Returns a new instance of Job.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/webapp_worker/job.rb', line 11

def initialize(user_supplied_hash={})
	standard_hash = { command:"", minute:"*", hour:"*", day:"*", month:"*", weekday:"*" }

	user_supplied_hash = {} unless user_supplied_hash
	user_supplied_hash = standard_hash.merge(user_supplied_hash)

	user_supplied_hash.each do |key,value|
		self.instance_variable_set("@#{key}", value)
		self.class.send(:define_method, key, proc{self.instance_variable_get("@#{key}")})
		self.class.send(:define_method, "#{key}=", proc{|x| self.instance_variable_set("@#{key}", x)})
	end

	self.parse_datetime
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



6
7
8
# File 'lib/webapp_worker/job.rb', line 6

def command
  @command
end

#dayObject

Returns the value of attribute day.



6
7
8
# File 'lib/webapp_worker/job.rb', line 6

def day
  @day
end

#hourObject

Returns the value of attribute hour.



6
7
8
# File 'lib/webapp_worker/job.rb', line 6

def hour
  @hour
end

#minuteObject

Returns the value of attribute minute.



6
7
8
# File 'lib/webapp_worker/job.rb', line 6

def minute
  @minute
end

#monthObject

Returns the value of attribute month.



6
7
8
# File 'lib/webapp_worker/job.rb', line 6

def month
  @month
end

#weekdayObject

Returns the value of attribute weekday.



6
7
8
# File 'lib/webapp_worker/job.rb', line 6

def weekday
  @weekday
end

Class Method Details

.new_from_yaml(yaml, environment) ⇒ Object



26
27
28
# File 'lib/webapp_worker/job.rb', line 26

def self.new_from_yaml(yaml,environment)
	job = self.new((YAML.load_file(yaml))[environment])
end

Instance Method Details

#current_timeObject



30
31
32
# File 'lib/webapp_worker/job.rb', line 30

def current_time
	return "#{Time.now.strftime("%w %m-%d %H:%M")}"
end

#fix_dayObject



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/webapp_worker/job.rb', line 101

def fix_day
	case @day.to_s
	when "*", nil, ""
		@day = []
		(1..31).each do |d|
			@day << self.make_string(d)
		end
	when DIVISION
		@day = self.fix_every(@day)
	when DIGIT
		@day = [self.make_string(@day)]
	end
end

#fix_every(value) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/webapp_worker/job.rb', line 44

def fix_every(value)
	divider = /\d+$/
	range = /^\d+-\d+/
	first_range = /^\d+/
	second_range = /\d+$/

	every = value.match(divider).to_s.to_i
	number_range = value.match(range).to_s
	first = number_range.match(first_range).to_s.to_i
	second = number_range.match(second_range).to_s.to_i

	range = []
	until first >= second do
		first = first + every
		break if first > second
		range << self.make_string(first)
	end

	return range
end

#fix_hourObject



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/webapp_worker/job.rb', line 87

def fix_hour
	case @hour.to_s
	when "*", nil, ""
		@hour = []
		(0..23).each do |h|
			@hour << self.make_string(h)
		end
	when DIVISION
		@hour = self.fix_every(@hour)
	when DIGIT
		@hour = [self.make_string(@hour)]
	end
end

#fix_minuteObject



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/webapp_worker/job.rb', line 73

def fix_minute
	case @minute.to_s
	when "*", nil, ""
		@minute = []
		(0..59).each do |m|
			@minute << self.make_string(m)
		end
	when DIVISION
		@minute = self.fix_every(@minute)
	when DIGIT
		@minute = [self.make_string(@minute)]
	end
end

#fix_monthObject



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/webapp_worker/job.rb', line 115

def fix_month
	case @month.to_s
	when "*", nil, ""
		@month = []
		(1..12).each do |m|
			@month << self.make_string(m)
		end
	when DIVISION
		@month = self.fix_every(@month)
	when DIGIT
		@month = [self.make_string(@month)]
	end
end

#fix_weekdayObject



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/webapp_worker/job.rb', line 129

def fix_weekday
	case @weekday.to_s
	when "*", nil, ""
		@weekday = []
		(0..6).each do |w|
			@weekday << w.to_s
		end
	when DIVISION
		@weekday = self.fix_every(@weekday)
	when DIGIT
		@weekday = [@weekday.to_s]
	end
end

#make_string(n) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/webapp_worker/job.rb', line 34

def make_string(n)
	test_n = n.to_s.length

	if test_n < 2 && test_n > 0
		return "0#{n}"
	else
		return n.to_s
	end
end

#next_run?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/webapp_worker/job.rb', line 159

def next_run?
	self.next_runs?(1)
end

#next_runs?(til) ⇒ Boolean

Returns:

  • (Boolean)


163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/webapp_worker/job.rb', line 163

def next_runs?(til)
	self.parse_datetime

	next_runs = []

	now = Time.now
	@weekday = self.next_times(@weekday,6,now.strftime("%w").to_i)
	@year = now.strftime("%Y")
	@month = self.next_times(@month,12,now.strftime("%m").to_i)
	@day = self.next_times(@day,31,now.strftime("%d").to_i)
	@hour = self.next_times(@hour,23,now.strftime("%H").to_i)
	@minute = self.next_times(@minute,60,now.strftime("%M").to_i)

	counter = 0
	catch :done do
		@month.each do |month|
			@day.each do |day|
				@weekday.each do |weekday|
					@hour.each do |hour|
						@minute.each do |minute|
							begin
								next_time = (DateTime.strptime("#{weekday} #{@year}-#{month}-#{day} #{hour}:#{minute}","%w %Y-%m-%d %H:%M")).to_time + 25200

								next unless next_time >= now
								next_runs << next_time
								counter = counter + 1
							rescue ArgumentError
								next
							end

							throw :done, next_runs if counter >= til
						end
					end
				end
			end
		end

		return next_runs
	end
end

#next_times(numbers, amount, time) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/webapp_worker/job.rb', line 143

def next_times(numbers,amount,time)
	future = {}

	numbers.each do |number|
		calculated = number.to_i - time
		calculated = (calculated + amount) unless calculated >= 0
		future.store(number,calculated)
	end

	sub = {}
	(future.sort_by { |key,value| value }).collect { |key,value| sub.store(key,value) }
	fn = sub.collect { |key,value| key }

	return fn
end

#parse_datetimeObject



65
66
67
68
69
70
71
# File 'lib/webapp_worker/job.rb', line 65

def parse_datetime
	self.fix_minute
	self.fix_hour
	self.fix_day
	self.fix_month
	self.fix_weekday
end