serial_scheduler 0.2.0 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a3176f9491c9ca41665e74892f0ec5cca612b2590ef5d259eb595f03e3a80c1a
4
- data.tar.gz: 182974bb8a089dc7c229eaa9a23294f88cea9d17b5eaca68f8a98cc9b0fe6bc6
3
+ metadata.gz: 7b83a00e72a7ae3fd8db2f2ee0e7096b893bd518f018c8698170d4803819efee
4
+ data.tar.gz: d68d671f5e1bd8fc10158b7e81288f6c81de5ca9f184ad4b2cb029f3c4879c94
5
5
  SHA512:
6
- metadata.gz: 4f33dd9831bea10233bac83fb3dfec909ef297f5f78254dea7e87babd6191210da35c81e572762292238258d0a95faf161d957c70d6cca688bedda3e5b8cb461
7
- data.tar.gz: 5210390830383771b3fdcc69756116fd644f452e58b2c41ac6cfe3ef981329ac09c5792f3a6b0faefb8599652051dcfe4bbee4c5802ad5ab059a240509a80cfd
6
+ metadata.gz: 98d7363dd9ff8f7d23e9ed47e638d1daa4801f01fe9dce51881762ec6d3d97c351e0d5f31de2c7b5c9356bab674be28750897b21ba4b707d2824c667bb9f90c9
7
+ data.tar.gz: 5705053102ec7bc24cbad873fd2814661cdf9c04108873e37ddcdfded053f895c02dab3dc5a53bca466d13a3119042080f77519d817850aa5696b6d0f14d4177
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  class SerialScheduler
3
- VERSION = "0.2.0"
3
+ VERSION = "0.2.4"
4
4
  end
@@ -8,7 +8,7 @@ class SerialScheduler
8
8
  class Producer
9
9
  attr_reader :name, :next, :timeout, :block
10
10
 
11
- def initialize(name, interval: nil, timeout:, cron: nil, &block)
11
+ def initialize(name, timeout:, interval: nil, cron: nil, &block)
12
12
  if cron
13
13
  cron = Fugit.do_parse_cron(cron)
14
14
  elsif !interval || interval < 1 || !interval.is_a?(Integer)
@@ -43,7 +43,7 @@ class SerialScheduler
43
43
  end
44
44
  end
45
45
 
46
- def initialize(logger: Logger.new(STDOUT), error_handler: ->(e) { raise e })
46
+ def initialize(logger: Logger.new($stdout), error_handler: ->(e) { raise e })
47
47
  @logger = logger
48
48
  @error_handler = error_handler
49
49
 
@@ -52,8 +52,8 @@ class SerialScheduler
52
52
  end
53
53
 
54
54
  # start a new thread that enqueues an execution at given interval
55
- def add(*args, &block)
56
- @producers << Producer.new(*args, &block)
55
+ def add(*args, **kwargs, &block)
56
+ @producers << Producer.new(*args, **kwargs, &block)
57
57
  end
58
58
 
59
59
  def run
@@ -64,11 +64,12 @@ class SerialScheduler
64
64
  now = Time.now.to_i
65
65
  earliest = @producers.min_by(&:next)
66
66
  wait = [earliest.next - now, 0].max # do not wait when overdue
67
+ target = Time.at(now + wait)
67
68
 
68
69
  if wait > 0
69
- @logger.info message: "Waiting to start job", job: earliest.name, time: wait, at: Time.at(now).to_s
70
- wait.times do
71
- break if @stopped
70
+ @logger.info message: "Waiting to start job", job: earliest.name, in: wait, at: target.to_s
71
+ loop do
72
+ break if @stopped || Time.now >= target # need to re-check or long waits will drift by .3%
72
73
 
73
74
  sleep 1
74
75
  end
@@ -89,12 +90,10 @@ class SerialScheduler
89
90
  def execute_in_fork(producer)
90
91
  @logger.info message: "Executing job", job: producer.name
91
92
  pid = fork do
92
- begin
93
- Timeout.timeout producer.timeout, &producer.block
94
- rescue StandardError => e # do not rescue `Exception` so it can be `Interrupt`-ed
95
- @logger.error message: "Error in job", job: producer.name, error: e.message
96
- @error_handler.call(e)
97
- end
93
+ Timeout.timeout producer.timeout, &producer.block
94
+ rescue StandardError => e # do not rescue `Exception` so it can be `Interrupt`-ed
95
+ @logger.error message: "Error in job", job: producer.name, error: e.message
96
+ @error_handler.call(e)
98
97
  end
99
98
  Process.wait pid
100
99
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serial_scheduler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Grosser
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-14 00:00:00.000000000 Z
11
+ date: 2021-12-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fugit
@@ -45,14 +45,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
45
45
  requirements:
46
46
  - - ">="
47
47
  - !ruby/object:Gem::Version
48
- version: 2.3.0
48
+ version: 2.5.0
49
49
  required_rubygems_version: !ruby/object:Gem::Requirement
50
50
  requirements:
51
51
  - - ">="
52
52
  - !ruby/object:Gem::Version
53
53
  version: '0'
54
54
  requirements: []
55
- rubygems_version: 3.0.3
55
+ rubygems_version: 3.2.16
56
56
  signing_key:
57
57
  specification_version: 4
58
58
  summary: Simple scheduler for long-running and infrequent tasks, no threads, always