inst-jobs 3.4.0 → 3.6.0

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: a4ab079657570b1e9ce94abdd938367ba912afa077ccad380f38f81862fe5a43
4
- data.tar.gz: a0366132ef9d5cced326cdecef3446217857e668760fae47a72f664060fc380a
3
+ metadata.gz: 5f938b9b6cacf60e67a5063bd1e1af4c80eafcfbc9872d747214b623f105352d
4
+ data.tar.gz: f2fc44b5a7c8a7918b0930ae68b44edd64b0acb2361bd741704f128ca9ac34af
5
5
  SHA512:
6
- metadata.gz: 04d51cfd152ee4a6e5145df09d1f8fec52d60c4eb74287e49d693a29f62498ca9a09655ac0dbb5ce6137817e309a7a47fb5918b737d41a2f78f64c6c8c43ff4e
7
- data.tar.gz: ed4b8778c404ea2a5f57644e71b5b5bdb8be9f135056acd8eba46dcb7fec58eb39cc5bf5deb05cd4bf3e6754334d7fedb9fe5239291b276bebc1595946d97b45
6
+ metadata.gz: 39efda83073f3165101918d4233c9cef7cd272189d7aac7ae21fb2310a7e0e14f69005773206b708edb368d4c92e1c2cedd352323c90b9df552612809920ec89
7
+ data.tar.gz: c70e2049abc42ab22a6f8b699435d7743d25550cd73e24248c489d112800472a47719d2371e952b29f9fd31a08162c1ee1458b825d69b7789165006a18dc5336
@@ -350,6 +350,82 @@ module Delayed
350
350
  [job_count, new_strand]
351
351
  end
352
352
 
353
+ # change priority, max_concurrent and hold state of every job with the tag
354
+ # that is not running, held ones included. Running jobs keep their settings.
355
+ # max_concurrent applies only to stranded jobs, and a strand ramps up to it
356
+ # gradually as its jobs finish. hold: true holds, hold: false releases.
357
+ # returns the number of jobs that at least one of the changes applies to
358
+ # (this is designed for use in a Rails console or a Jobs UI)
359
+ def self.manage_tag(tag, priority: nil, max_concurrent: nil, hold: nil, batch_size: 1_000)
360
+ raise ArgumentError, "missing tag" if tag.blank?
361
+ if priority.nil? && max_concurrent.nil? && hold.nil?
362
+ raise ArgumentError, "missing priority, max_concurrent or hold"
363
+ end
364
+ unless priority.nil? || (priority.is_a?(Integer) && priority.between?(Delayed::MIN_PRIORITY, Delayed::MAX_PRIORITY))
365
+ raise ArgumentError, "invalid priority"
366
+ end
367
+ unless max_concurrent.nil? || (max_concurrent.is_a?(Integer) && max_concurrent >= 1)
368
+ raise ArgumentError, "invalid max_concurrent"
369
+ end
370
+ raise ArgumentError, "invalid hold" unless [nil, true, false].include?(hold)
371
+
372
+ apply_tag_changes(manageable_tag_jobs(tag), priority:, max_concurrent:, hold:, batch_size:)
373
+ end
374
+
375
+ # returns { matched_jobs:, matched_strands:, held_jobs: } for the jobs manage_tag changes
376
+ def self.manage_tag_counts(tag)
377
+ scope = manageable_tag_jobs(tag)
378
+ {
379
+ matched_jobs: scope.count,
380
+ matched_strands: scope.where.not(strand: nil).distinct.count(:strand),
381
+ held_jobs: scope.where(locked_by: ON_HOLD_LOCKED_BY).count
382
+ }
383
+ end
384
+
385
+ # Updates the jobs in scope, in batches. Each CASE reads the row as it was
386
+ # before the update, so the order of the assignments, locked_by last
387
+ # included, doesn't matter.
388
+ def self.apply_tag_changes(scope, priority:, max_concurrent:, hold:, batch_size:)
389
+ now = db_time_now
390
+ assignments = []
391
+ affected = []
392
+ sql = ->(*fragment) { sanitize_sql_array(fragment) }
393
+
394
+ assignments << sql.call("priority = ?", priority) if priority
395
+ if max_concurrent
396
+ assignments << sql.call("max_concurrent = CASE WHEN strand IS NULL THEN max_concurrent ELSE ? END",
397
+ max_concurrent)
398
+ affected << scope.where.not(strand: nil)
399
+ end
400
+ if hold == true
401
+ assignments << sql.call("locked_at = CASE WHEN locked_by IS NULL THEN ? ELSE locked_at END", now)
402
+ assignments << sql.call("attempts = CASE WHEN locked_by IS NULL THEN ? ELSE attempts END", ON_HOLD_COUNT)
403
+ assignments << sql.call("locked_by = CASE WHEN locked_by IS NULL THEN ? ELSE locked_by END",
404
+ ON_HOLD_LOCKED_BY)
405
+ affected << scope.where(locked_by: nil)
406
+ elsif hold == false
407
+ held = sql.call("locked_by = ?", ON_HOLD_LOCKED_BY)
408
+ assignments << "locked_at = CASE WHEN #{held} THEN NULL ELSE locked_at END"
409
+ assignments << "attempts = CASE WHEN #{held} THEN 0 ELSE attempts END"
410
+ assignments << sql.call("run_at = CASE WHEN #{held} AND run_at < ? THEN ? ELSE run_at END", now, now)
411
+ assignments << "failed_at = CASE WHEN #{held} THEN NULL ELSE failed_at END"
412
+ assignments << "locked_by = CASE WHEN #{held} THEN NULL ELSE locked_by END"
413
+ affected << scope.where(locked_by: ON_HOLD_LOCKED_BY)
414
+ end
415
+
416
+ # a priority change applies to every job in scope, otherwise only to the ones another change applies to
417
+ scope = affected.reduce(:or) unless priority
418
+ scope.in_batches(of: batch_size).update_all(assignments.join(", "))
419
+ end
420
+ private_class_method :apply_tag_changes
421
+
422
+ def self.manageable_tag_jobs(tag)
423
+ raise ArgumentError, "missing tag" if tag.blank?
424
+
425
+ where(tag:).where(locked_by: [nil, ON_HOLD_LOCKED_BY])
426
+ end
427
+ private_class_method :manageable_tag_jobs
428
+
353
429
  def self.maybe_silence_periodic_log(&)
354
430
  if Settings.silence_periodic_log
355
431
  ::ActiveRecord::Base.logger.silence(&)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Delayed
4
- VERSION = "3.4.0"
4
+ VERSION = "3.6.0"
5
5
  end
@@ -7,11 +7,13 @@ require "socket"
7
7
  module Delayed
8
8
  class Worker
9
9
  class ConsulHealthCheck < HealthCheck
10
+ include Delayed::Logging
11
+
10
12
  self.type_name = :consul
11
13
 
12
14
  CONSUL_CONFIG_KEYS = %w[url acl_token].map(&:freeze).freeze
13
15
  DEFAULT_SERVICE_NAME = "inst-jobs_worker"
14
- attr_reader :service_client, :health_client
16
+ attr_reader :service_client, :health_client, :check_client
15
17
 
16
18
  def initialize(*, **)
17
19
  super
@@ -27,21 +29,23 @@ module Delayed
27
29
  end
28
30
  @service_client = Diplomat::Service.new(configuration: consul_config)
29
31
  @health_client = Diplomat::Health.new(configuration: consul_config)
32
+ @check_client = Diplomat::Check.new(configuration: consul_config)
30
33
  else
31
34
  @service_client = Diplomat::Service.new
32
35
  @health_client = Diplomat::Health.new
36
+ @check_client = Diplomat::Check.new
33
37
  end
34
38
  end
35
39
 
36
40
  def start
37
- @service_client.register({
38
- id: worker_name,
39
- name: service_name,
40
- check: check_attributes
41
- })
41
+ registered = register
42
+ start_heartbeat if registered && ttl_check?
43
+ registered
42
44
  end
43
45
 
44
46
  def stop
47
+ @heartbeat_thread&.kill
48
+ @heartbeat_thread = nil
45
49
  @service_client.deregister(worker_name)
46
50
  end
47
51
 
@@ -56,13 +60,56 @@ module Delayed
56
60
 
57
61
  private
58
62
 
63
+ def register
64
+ @service_client.register({
65
+ id: worker_name,
66
+ name: service_name,
67
+ check: check_attributes
68
+ })
69
+ end
70
+
71
+ def ttl_check?
72
+ @config.fetch(:check_type, "script").to_s == "ttl"
73
+ end
74
+
75
+ def check_id
76
+ "service:#{worker_name}"
77
+ end
78
+
79
+ # in seconds
80
+ def ttl
81
+ Integer(@config.fetch(:ttl, 60))
82
+ end
83
+
84
+ def start_heartbeat
85
+ @heartbeat_thread = Thread.new do
86
+ loop do
87
+ sleep(ttl / 3.0)
88
+ heartbeat
89
+ rescue => e
90
+ logger.warn("Consul TTL update failed for #{worker_name}: #{e.class}: #{e.message}")
91
+ end
92
+ end
93
+ end
94
+
95
+ def heartbeat
96
+ @check_client.pass(check_id)
97
+ rescue Faraday::ResourceNotFound
98
+ logger.warn("Consul forgot #{worker_name}, re-registering")
99
+ register
100
+ end
101
+
59
102
  def check_attributes
60
- {
61
- args: ["bash", "-c", check_script],
103
+ base = {
62
104
  status: "passing",
63
- interval: @config.fetch(:check_interval, "5m"),
64
105
  deregister_critical_service_after: @config.fetch(:deregister_service_delay, "10m")
65
- }.tap do |h|
106
+ }
107
+ return base.merge(ttl: "#{ttl}s") if ttl_check?
108
+
109
+ base.merge(
110
+ args: ["bash", "-c", check_script],
111
+ interval: @config.fetch(:check_interval, "5m")
112
+ ).tap do |h|
66
113
  h[:docker_container_id] = docker_container_id if @config["docker"]
67
114
  end
68
115
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inst-jobs
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.4.0
4
+ version: 3.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cody Cutrer
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2026-10-19 00:00:00.000000000 Z
13
+ date: 2026-12-01 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord
@@ -74,6 +74,20 @@ dependencies:
74
74
  - - "<"
75
75
  - !ruby/object:Gem::Version
76
76
  version: '3'
77
+ - !ruby/object:Gem::Dependency
78
+ name: benchmark
79
+ requirement: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ type: :runtime
85
+ prerelease: false
86
+ version_requirements: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
77
91
  - !ruby/object:Gem::Dependency
78
92
  name: debug_inspector
79
93
  requirement: !ruby/object:Gem::Requirement