punctual 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/job.rb +72 -0
- data/lib/punctual.rb +38 -0
- data/tests/punctual_test.rb +16 -0
- metadata +55 -0
data/lib/job.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
class Job
|
2
|
+
attr_accessor :name, :cron, :block, :last_executed
|
3
|
+
|
4
|
+
def initialize (name, cron, block)
|
5
|
+
@name = name
|
6
|
+
@cron = cron
|
7
|
+
@block = block
|
8
|
+
end
|
9
|
+
|
10
|
+
def should_execute?
|
11
|
+
min, hour, day_of_month, month, day_of_week = cron.split("\s")
|
12
|
+
if (day_of_week == "7")
|
13
|
+
day_of_week = 0
|
14
|
+
end
|
15
|
+
now = Time.now
|
16
|
+
|
17
|
+
if !field_check? min, now.min
|
18
|
+
return false
|
19
|
+
end
|
20
|
+
|
21
|
+
if !field_check? hour, now.hour
|
22
|
+
return false
|
23
|
+
end
|
24
|
+
|
25
|
+
if !field_check? day_of_month, now.day
|
26
|
+
return false
|
27
|
+
end
|
28
|
+
|
29
|
+
if !field_check? month, now.month
|
30
|
+
return false
|
31
|
+
end
|
32
|
+
|
33
|
+
if !field_check? day_of_week, now.wday
|
34
|
+
return false
|
35
|
+
end
|
36
|
+
|
37
|
+
return true;
|
38
|
+
end
|
39
|
+
|
40
|
+
def execute
|
41
|
+
@last_executed = Time.new
|
42
|
+
@block.call
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
def field_check? (cron_value, current_value)
|
47
|
+
if cron_value == "*"
|
48
|
+
return true
|
49
|
+
end
|
50
|
+
|
51
|
+
if cron_value =~ /\-/
|
52
|
+
start, finish = cron_value.split("\-")
|
53
|
+
start = start.to_i
|
54
|
+
finish = finish.to_i
|
55
|
+
if (start..finish) === current_value
|
56
|
+
return true
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
if cron_value =~ /\,/
|
61
|
+
cron_values = cron_value.split("\,")
|
62
|
+
if cron_values.include?(current_value.to_s)
|
63
|
+
return true
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
if cron_value.to_i == current_value.to_i
|
68
|
+
return true
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
data/lib/punctual.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
load 'job.rb'
|
2
|
+
|
3
|
+
class Punctual
|
4
|
+
attr_reader :jobs
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@sleep_time = 1
|
8
|
+
@clock_initialized = false
|
9
|
+
@jobs = Array.new
|
10
|
+
end
|
11
|
+
|
12
|
+
# register new job
|
13
|
+
def schedule(name, cron, &block)
|
14
|
+
job = Job.new(name, cron, block)
|
15
|
+
puts "Scheduling block: #{name}, #{cron}"
|
16
|
+
@jobs.push(job)
|
17
|
+
end
|
18
|
+
|
19
|
+
# starts the scheduler
|
20
|
+
def start()
|
21
|
+
t = Thread.new do
|
22
|
+
while true do
|
23
|
+
time = Time.new
|
24
|
+
if (time.sec == 0)
|
25
|
+
@sleep_time = 60
|
26
|
+
@clock_initialized = true
|
27
|
+
end
|
28
|
+
@jobs.each do |job|
|
29
|
+
if @clock_initialized == true && job.should_execute?
|
30
|
+
job.execute
|
31
|
+
end
|
32
|
+
end
|
33
|
+
sleep @sleep_time
|
34
|
+
end
|
35
|
+
end
|
36
|
+
t.join
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'punctual.rb'
|
3
|
+
|
4
|
+
class PunctualTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
@p = Punctual.new
|
7
|
+
@i = 0
|
8
|
+
@p.schedule("test job", "* * * * *") do
|
9
|
+
@i = @i + 1
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_schedule
|
14
|
+
assert_not_nil(@p.jobs)
|
15
|
+
end
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: punctual
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Eric Alexander
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-02-06 00:00:00 +00:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: eric.alexander@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- tests/punctual_test.rb
|
26
|
+
- lib/job.rb
|
27
|
+
- lib/punctual.rb
|
28
|
+
has_rdoc: true
|
29
|
+
homepage: http://ericalexander.tumblr.com
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: "0"
|
40
|
+
version:
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
version:
|
47
|
+
requirements: []
|
48
|
+
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 1.0.1
|
51
|
+
signing_key:
|
52
|
+
specification_version: 2
|
53
|
+
summary: Simple Ruby scheduler
|
54
|
+
test_files:
|
55
|
+
- tests/punctual_test.rb
|