lowloop 0.8.15 → 0.8.17

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: a1222163a4798e1a2508a0a049a3345d49ecac4693b2d4a326afea93bc0ca103
4
+ data.tar.gz: cf9372d768bf6ed5f025a7fb3a15e8e74f89b2d54c2c91e226572b59a12251de
5
5
  SHA512:
6
- metadata.gz: cedb352e4c5be6fb4d006a6b0648576b78194536c5fd6b8a865efa7ae1190cbfc6de7a22e40b81f6cf99d0854879e54e3d3c6551abd1d3eeb5d8db9ba5faed30
7
- data.tar.gz: c1db12bf6b57885f51d6aa658766ebee0afa14863f0395b18f6da539458b9622773cc6984dbeacfb7329f53e706cae2280f6e8f3c789e3d1e07e283bca26adcf
6
+ metadata.gz: 308c54b96ada7d81d2db9725dd3974e8a7c68297b76c4e48b9b3cc4361031995e517ca56e9045f421f8dc96b13a9653f381893eebf71aa4db2fc66a3e8f258ad
7
+ data.tar.gz: a0fa638b481431f9742577698659ef1854905c8dda44f306de51986805657855ba1e051f64970b2080696e5e7d2506d763632a0de8fea55cfb0bdb22aa66b3b1
@@ -6,12 +6,15 @@ 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]
13
14
  class Interval < Low::Event
14
15
  UNITS = { every_second: 1, every_minute: 60, every_hour: 3600, every_day: 86_400 }.freeze
16
+ # Enough recent turns' outcomes for a dial of up to 60 marks like each second of a minute.
17
+ KEPT_OUTCOMES = 64
15
18
 
16
19
  attr_reader :seconds, :at_hour, :triggered_at
17
20
 
@@ -36,6 +39,7 @@ module Low
36
39
  @seconds = seconds
37
40
  @at_hour = at_hour
38
41
  @triggered_at = nil
42
+ @outcomes = {}
39
43
  end
40
44
 
41
45
  # Each tick starts a tree of its own so a job's events are let go like a request's.
@@ -48,23 +52,64 @@ module Low
48
52
  true
49
53
  end
50
54
 
51
- # Whether it's time to trigger it again, which it is the first time too. It's not due outside its hour.
55
+ # Whether a turn has started since its last. The turn it's first seen in started before the app did so it waits for
56
+ # the next. It's not due outside its hour.
52
57
  def due?(now)
58
+ @turn ||= turn_at(now)
59
+ @first_turn ||= @turn
53
60
  return false if at_hour && now.hour != at_hour
54
61
 
55
- triggered_at.nil? || now - triggered_at >= seconds
62
+ turn_at(now) > @turn
56
63
  end
57
64
 
58
- # Triggers its observers, counting the next turn from now.
65
+ # Triggers its observers, marking the turn it's in as run and noting whether it ran or failed.
59
66
  def tick(now = Time.now)
60
67
  @triggered_at = now
68
+ @turn = turn_at(now)
69
+ note_outcome(:running)
61
70
  trigger
71
+ note_outcome(:ran)
72
+ rescue StandardError
73
+ note_outcome(:failed)
74
+ raise
75
+ end
76
+
77
+ # What happened on the turn under way at that time: running or ran or failed. It's skipped when the turn passed
78
+ # without running since the app started and nil for a turn still to come or from before.
79
+ def outcome_at(time, now: Time.now)
80
+ turn = turn_at(time)
81
+ return @outcomes[turn] if @outcomes.key?(turn)
82
+ return unless @first_turn && turn > @first_turn && turn < turn_at(now)
83
+
84
+ :skipped
85
+ end
86
+
87
+ # Whether one of its turns starts at that second.
88
+ def starts_turn?(time)
89
+ (local_seconds(time) % seconds).zero?
62
90
  end
63
91
 
64
92
  # The same schedule observed in two places is one interval, so both are triggered together.
65
93
  def ==(other) = other.is_a?(Interval) && other.seconds == seconds && other.at_hour == at_hour
66
94
  def eql?(other) = self == other
67
95
  def hash = [self.class, seconds, at_hour].hash
96
+
97
+ private
98
+
99
+ def note_outcome(outcome)
100
+ @outcomes[@turn] = outcome
101
+ @outcomes.shift while @outcomes.size > KEPT_OUTCOMES
102
+ end
103
+
104
+ # How many turns have passed since the epoch on the server's clock.
105
+ def turn_at(time)
106
+ local_seconds(time) / seconds
107
+ end
108
+
109
+ # Counts in the server's time zone so a day's turns start at its midnight.
110
+ def local_seconds(time)
111
+ time.to_i + time.utc_offset
112
+ end
68
113
  end
69
114
  end
70
115
  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.17'
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.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - maedi