schked 1.1.0 → 1.1.2

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: 660370465a4ff279d9bc26184108c07637d0fcd69f071e22e8dea9b5b79b29ae
4
- data.tar.gz: 9e54efb1455b8e48af9d67011008cb821aefe6bb691ec0325f1f0a9c8425ed10
3
+ metadata.gz: fe971fe49c1279ffd4cbc39948faea2fb82fa58434aa90814e1c1cbe2ebd99e2
4
+ data.tar.gz: 2b8800aa8128347a42e1f7fe2d73930293baae1ff258d6f1ddd24f21dff297c4
5
5
  SHA512:
6
- metadata.gz: f0ee5ac189460a35c87da4c6a889482f824985abd5561f5541e01157b6574e7d28957b9082cab272fc2cce04e779850ced784858e91da02554b3816a37483fee
7
- data.tar.gz: 68754d88a2fcc870c6e161d951b2ff2a00749d0c345401e9608c69cfde4055554f1275157b1e7af7c3a2f07efff037eb690619834daa3d289293ef33c27be6ac
6
+ metadata.gz: a8dcdca1dad927793b3c0f6356a1f6222a9e4081b20baca7df9391c93ea044f8f2f9bf7fa2db05c330616a0692597abbf8e2807a07d5c6f80bff0687bfd90911
7
+ data.tar.gz: 370cab83d6e000cbc0cb2c19be884738315434d3dd42d0bf718c1b094194231b77f8356d2782f6a7c773b98aa64f3eedc6de39f3a34206f8a3aa381645f5012e
data/exe/schked CHANGED
@@ -1,4 +1,4 @@
1
- #!/usr/bun/ruby
1
+ #!/usr/bin/ruby
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require "bundler/setup"
@@ -4,22 +4,30 @@ module Schked
4
4
  class RedisLocker
5
5
  attr_reader :lock_manager,
6
6
  :lock_id,
7
- :lock_ttl
7
+ :lock_ttl,
8
+ :logger
8
9
 
9
10
  LOCK_KEY = "schked:redis_locker"
10
11
  LOCK_TTL = 60_000 # ms
11
12
 
12
- def initialize(redis_servers, lock_ttl: LOCK_TTL)
13
+ def initialize(redis_servers, lock_ttl: LOCK_TTL, logger: Logger.new($stdout))
13
14
  @lock_manager = Redlock::Client.new(redis_servers, retry_count: 0)
14
15
  @lock_ttl = lock_ttl
16
+ @logger = logger
15
17
  end
16
18
 
17
19
  def lock
18
20
  valid_lock? || !!try_lock
21
+ rescue => e
22
+ logger.error("Failed to acquire a lock with error: #{e.message}")
23
+ false
19
24
  end
20
25
 
21
26
  def unlock
22
27
  lock_manager.unlock(lock_id) if valid_lock?
28
+ rescue => e
29
+ logger.error("Failed to release the lock with error: #{e.message}")
30
+ false
23
31
  end
24
32
 
25
33
  def extend_lock
@@ -28,6 +36,9 @@ module Schked
28
36
  @lock_id = lock_manager.lock(LOCK_KEY, lock_ttl, extend: lock_id, extend_only_if_locked: true)
29
37
 
30
38
  !!@lock_id
39
+ rescue => e
40
+ logger.error("Failed to extend the lock with error: #{e.message}")
41
+ false
31
42
  end
32
43
 
33
44
  def valid_lock?
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Schked
4
- VERSION = "1.1.0"
4
+ VERSION = "1.1.2"
5
5
  end
data/lib/schked/worker.rb CHANGED
@@ -7,7 +7,7 @@ module Schked
7
7
  def initialize(config:)
8
8
  @config = config
9
9
 
10
- @locker = RedisLocker.new(config.redis_servers, lock_ttl: 40_000) unless config.standalone?
10
+ @locker = RedisLocker.new(config.redis_servers, lock_ttl: 40_000, logger: config.logger) unless config.standalone?
11
11
  @scheduler = Rufus::Scheduler.new(trigger_lock: locker)
12
12
 
13
13
  watch_signals
@@ -46,21 +46,29 @@ module Schked
46
46
  def define_callbacks
47
47
  cfg = config
48
48
 
49
+ scheduler.define_singleton_method(:extract_job_name) do |job|
50
+ if job
51
+ job.opts[:as] || job.job_id
52
+ else
53
+ "unknown"
54
+ end
55
+ end
56
+
49
57
  scheduler.define_singleton_method(:on_error) do |job, error|
50
- cfg.logger.fatal("Task #{job.opts[:as] || job.job_id} failed with error: #{error.message}")
58
+ cfg.logger.fatal("Task #{extract_job_name(job)} failed with error: #{error.message}")
51
59
  cfg.logger.error(error.backtrace.join("\n")) if error.backtrace
52
60
 
53
61
  cfg.fire_callback(:on_error, job, error)
54
62
  end
55
63
 
56
64
  scheduler.define_singleton_method(:on_pre_trigger) do |job, time|
57
- cfg.logger.info("Started task: #{job.opts[:as] || job.job_id}")
65
+ cfg.logger.info("Started task: #{extract_job_name(job)}")
58
66
 
59
67
  cfg.fire_callback(:before_start, job, time)
60
68
  end
61
69
 
62
70
  scheduler.define_singleton_method(:on_post_trigger) do |job, time|
63
- cfg.logger.info("Finished task: #{job.opts[:as] || job.job_id}")
71
+ cfg.logger.info("Finished task: #{extract_job_name(job)}")
64
72
 
65
73
  cfg.fire_callback(:after_finish, job, time)
66
74
  end
@@ -80,7 +88,7 @@ module Schked
80
88
  Thread.new do
81
89
  loop do
82
90
  scheduler.shutdown(wait: 5) if @shutdown
83
- sleep 0.2
91
+ sleep 1
84
92
  end
85
93
  end
86
94
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schked
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Misha Merkushin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-19 00:00:00.000000000 Z
11
+ date: 2022-12-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redlock