polling 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -67,6 +67,27 @@ result
67
67
  exec sleep 2
68
68
  sleep 2.998732089996338seconds (until 2013-03-24 23:38:50 +0900)
69
69
 
70
+ Async run
71
+
72
+ Polling.async_run 5 do
73
+ puts "exec sleep 2"
74
+ sleep 2
75
+ puts Time.now
76
+ end
77
+
78
+ async run result
79
+
80
+ start: 2013-03-31 07:17:58 +0900
81
+ exec sleep 2
82
+ 2013-03-31 07:18:00 +0900
83
+ sleep 2.998924seconds (until 2013-03-31 07:18:03 +0900)
84
+ exec sleep 2
85
+ 2013-03-31 07:18:05 +0900
86
+ sleep 2.999263seconds (until 2013-03-31 07:18:08 +0900)
87
+ exec sleep 2
88
+ 2013-03-31 07:18:10 +0900
89
+ sleep 2.998978seconds (until 2013-03-31 07:18:13 +0900)
90
+
70
91
  debug
71
92
 
72
93
  require 'polling'
@@ -90,31 +111,14 @@ Time which can be set up
90
111
  - "5s" or like string type [s|m|h|d]
91
112
  - [0,10,20,30,40,50] array class
92
113
 
93
- Support legacy interface.
94
114
  It starts at per minute 0 second.
95
115
 
96
116
  time = [0,10,20,30,40,50]
97
- Polling::run(time,debug=true) do
117
+ Polling::run time, debug=true do
98
118
  puts "test"
99
119
  sleep 2
100
120
  end
101
121
 
102
- The multiple of 60 is set up.
103
-
104
- time = [300]
105
- Polling::run(time) do
106
- puts "hoge"
107
- sleep 2
108
- end
109
-
110
- Please set a vlue that is divisible by 60.
111
-
112
- time = ["5s"] #or 5. support string is s,m,h,d.
113
- Polling::run(time) do
114
- puts "test"
115
- sleep 2
116
- end
117
-
118
122
  ## Copyright
119
123
 
120
124
  Copyright (c) 2012, hiro-su All rights reserved.
data/lib/polling.rb CHANGED
@@ -30,6 +30,9 @@ module Polling
30
30
  end
31
31
 
32
32
  interval = Validate.value interval
33
+ unless @debug
34
+ e.__send__(:start_print, e.stime)
35
+ end
33
36
 
34
37
  case interval
35
38
  when Array
@@ -42,6 +45,23 @@ module Polling
42
45
  $stderr.puts ex.message
43
46
  end
44
47
 
48
+ def async_run interval=@interval, debug=false
49
+ e = Engine.new
50
+ e.__send__(:start_print, 0)
51
+ loop do
52
+ before = Time.now
53
+ yield if block_given?
54
+ opts = {
55
+ interval: interval,
56
+ before: before,
57
+ after: Time.now,
58
+ debug: debug
59
+ }
60
+ stime = e.stime_async opts
61
+ Sleep.exec stime
62
+ end
63
+ end
64
+
45
65
  private
46
66
 
47
67
  def set_instance_variables variables
@@ -51,5 +71,6 @@ module Polling
51
71
  end
52
72
 
53
73
  alias start run
74
+ alias sync_run run
54
75
  end
55
76
  end
@@ -43,6 +43,14 @@ module Polling
43
43
  return opts[:init_time]
44
44
  end
45
45
 
46
+ def stime_async opts={}
47
+ stime = opts[:interval] - (opts[:after] - opts[:before])
48
+ debug debug: opts[:debug], stime: stime
49
+ stime
50
+ rescue => ex
51
+ $stderr.puts ex.to_s
52
+ end
53
+
46
54
  private
47
55
 
48
56
  def target target=@target, offset=@offset
@@ -61,6 +69,11 @@ module Polling
61
69
  @target -= init_time
62
70
  end
63
71
 
72
+ def start_print stime
73
+ until_time = Time.at(Time.now.to_f + stime)
74
+ $stdout.print "start: #{until_time}\n"
75
+ end
76
+
64
77
  def debug opts={}
65
78
  if opts[:debug]
66
79
  until_time = Time.at(Time.now.to_f + opts[:stime])
@@ -1,3 +1,3 @@
1
1
  module Polling
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
data/test/async_run.rb ADDED
@@ -0,0 +1,11 @@
1
+ require File.expand_path(File.dirname(__FILE__)) + "/helper.rb"
2
+
3
+ class Polling::Test < Test::Unit::TestCase
4
+ def test_async
5
+ Polling.async_run 5, true do
6
+ puts "exec sleep 2"
7
+ sleep 2
8
+ puts Time.now
9
+ end
10
+ end
11
+ end
data/test/run.rb CHANGED
@@ -2,10 +2,9 @@ require File.expand_path(File.dirname(__FILE__)) + "/helper.rb"
2
2
 
3
3
  class Polling::Test < Test::Unit::TestCase
4
4
  def test_run
5
- puts "start: #{Time.now}"
6
5
  Polling.setting offset: 5, debug: true
7
6
  #Polling::run [0,10,20,30,40,50] do
8
- Polling::run 10 do
7
+ Polling::run 5 do
9
8
  puts Time.now
10
9
  puts "exec sleep 2"
11
10
  sleep 2
data/test/test_engine.rb CHANGED
@@ -59,6 +59,12 @@ class Polling::Engine::Test < Test::Unit::TestCase
59
59
  assert_equal 610, @e.stime(target: @e.increment!(300))
60
60
  end
61
61
 
62
+ def test_stime_async
63
+ Time.stubs(:now).returns(Time.parse "2012/01/01 00:00:00")
64
+ assert_equal 3, @e.stime_async(interval: 5, before: Time.now-2, after: Time.now)
65
+ assert_equal 298, @e.stime_async(interval: 300, before: Time.now-2, after: Time.now)
66
+ end
67
+
62
68
  def test_target
63
69
  assert_equal 0, @e.__send__(:target, 0, 0)
64
70
  assert_equal 10, @e.__send__(:target, 10, 0)
@@ -92,6 +98,11 @@ class Polling::Engine::Test < Test::Unit::TestCase
92
98
  assert_equal -110, @e.__send__(:decrement!, 120)
93
99
  end
94
100
 
101
+ def test_start_print
102
+ Time.stubs(:now).returns(Time.parse "2012/01/01 00:00:30")
103
+ assert_nil @e.__send__(:start_print, 5)
104
+ end
105
+
95
106
  def test_debug
96
107
  Time.stubs(:now).returns(Time.parse "2012/01/01 00:00:30")
97
108
  opts = {
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: polling
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-26 00:00:00.000000000 Z
12
+ date: 2013-03-30 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: polling
15
15
  email:
@@ -30,6 +30,7 @@ files:
30
30
  - lib/polling/validate.rb
31
31
  - lib/polling/version.rb
32
32
  - polling.gemspec
33
+ - test/async_run.rb
33
34
  - test/helper.rb
34
35
  - test/run.rb
35
36
  - test/test_engine.rb
@@ -62,6 +63,7 @@ signing_key:
62
63
  specification_version: 3
63
64
  summary: polling
64
65
  test_files:
66
+ - test/async_run.rb
65
67
  - test/helper.rb
66
68
  - test/run.rb
67
69
  - test/test_engine.rb