lowloop 0.8.15 → 0.8.16

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c9b9fa4e113122de236834b5b0dfb760cc0717ba8201e3fb4569548360410ec4
4
- data.tar.gz: 203ac6d1b866c82b37a0135b308956d713ba54760b251c32fa9e4ef8bc3c8dde
3
+ metadata.gz: 53d6cf9b4dc4c2d7da0fafc75ea0a012f9c665de1a9707fc4f5cd0a6819eb0e2
4
+ data.tar.gz: 242c11146c4d2b3eda2f201a3181aa6b07bd0e683612eeb687e3d018236f76e9
5
5
  SHA512:
6
- metadata.gz: cedb352e4c5be6fb4d006a6b0648576b78194536c5fd6b8a865efa7ae1190cbfc6de7a22e40b81f6cf99d0854879e54e3d3c6551abd1d3eeb5d8db9ba5faed30
7
- data.tar.gz: c1db12bf6b57885f51d6aa658766ebee0afa14863f0395b18f6da539458b9622773cc6984dbeacfb7329f53e706cae2280f6e8f3c789e3d1e07e283bca26adcf
6
+ metadata.gz: 80f3afccac6eef15e81c970681f09e147fbca0344f52decdc362bed8b69590fcf4e54e4ff34691742f6d5def18680cfb2c7f101795cb63677cc0277c64074406
7
+ data.tar.gz: 71349ca0269cb29052e40b077e6edd930d64f503c1ee7256e65878e2a830cb2b5977ac8f98baada742b117467bac93db8892f153c0bbd9bc8f4137104ed9a43b
@@ -6,7 +6,8 @@ module Low
6
6
  module Loop
7
7
  # A schedule to observe to run code periodically, like "observe Interval[every_minute: 3]". LowLoop triggers each
8
8
  # observed interval when it's due with the "tick" action, on a worker thread of its own, so a slow job never holds up
9
- # requests. With "at_hour: 2" it's only due during that hour of the day.
9
+ # requests. Its turns follow the clock like :00 :03 :06 so restarting the app doesn't move them. With "at_hour: 2"
10
+ # it's only due during that hour of the day.
10
11
  #
11
12
  # Interval[every_second: 30]
12
13
  # Interval[every_minute: 3, at_hour: 2]
@@ -48,23 +49,43 @@ module Low
48
49
  true
49
50
  end
50
51
 
51
- # Whether it's time to trigger it again, which it is the first time too. It's not due outside its hour.
52
+ # Whether a turn has started since its last. The turn it's first seen in started before the app did so it waits for
53
+ # the next. It's not due outside its hour.
52
54
  def due?(now)
55
+ @turn ||= turn_at(now)
53
56
  return false if at_hour && now.hour != at_hour
54
57
 
55
- triggered_at.nil? || now - triggered_at >= seconds
58
+ turn_at(now) > @turn
56
59
  end
57
60
 
58
- # Triggers its observers, counting the next turn from now.
61
+ # Triggers its observers, marking the turn it's in as run.
59
62
  def tick(now = Time.now)
60
63
  @triggered_at = now
64
+ @turn = turn_at(now)
61
65
  trigger
62
66
  end
63
67
 
68
+ # Whether one of its turns starts at that second.
69
+ def starts_turn?(time)
70
+ (local_seconds(time) % seconds).zero?
71
+ end
72
+
64
73
  # The same schedule observed in two places is one interval, so both are triggered together.
65
74
  def ==(other) = other.is_a?(Interval) && other.seconds == seconds && other.at_hour == at_hour
66
75
  def eql?(other) = self == other
67
76
  def hash = [self.class, seconds, at_hour].hash
77
+
78
+ private
79
+
80
+ # How many turns have passed since the epoch on the server's clock.
81
+ def turn_at(time)
82
+ local_seconds(time) / seconds
83
+ end
84
+
85
+ # Counts in the server's time zone so a day's turns start at its midnight.
86
+ def local_seconds(time)
87
+ time.to_i + time.utc_offset
88
+ end
68
89
  end
69
90
  end
70
91
  end
data/lib/version.rb CHANGED
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Low
4
4
  module Loop
5
- VERSION = '0.8.15'
5
+ VERSION = '0.8.16'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lowloop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.15
4
+ version: 0.8.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - maedi