postburner 1.0.0.pre.1 → 1.0.0.pre.3
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 +4 -4
- data/README.md +204 -117
- data/app/models/postburner/job.rb +12 -12
- data/app/models/postburner/mailer.rb +2 -2
- data/config/postburner.yml.example +32 -30
- data/lib/generators/postburner/install/install_generator.rb +10 -0
- data/lib/generators/postburner/install/templates/config/postburner.yml +144 -0
- data/lib/postburner/active_job/adapter.rb +22 -9
- data/lib/postburner/beanstalkd.rb +18 -39
- data/lib/postburner/configuration.rb +42 -15
- data/lib/postburner/queue_config.rb +25 -22
- data/lib/postburner/strategies/queue.rb +2 -2
- data/lib/postburner/version.rb +1 -1
- data/lib/postburner/workers/worker.rb +27 -28
- metadata +2 -1
|
@@ -12,7 +12,7 @@ module Postburner
|
|
|
12
12
|
# @example Creating and queueing a job
|
|
13
13
|
# class ProcessDonation < Postburner::Job
|
|
14
14
|
# queue 'critical'
|
|
15
|
-
#
|
|
15
|
+
# priority 0
|
|
16
16
|
#
|
|
17
17
|
# def perform(args)
|
|
18
18
|
# donation = Donation.find(args['donation_id'])
|
|
@@ -75,7 +75,7 @@ module Postburner
|
|
|
75
75
|
include Callbacks
|
|
76
76
|
|
|
77
77
|
# Instance-level queue configuration (overrides class-level defaults)
|
|
78
|
-
attr_writer :
|
|
78
|
+
attr_writer :priority, :ttr
|
|
79
79
|
|
|
80
80
|
LOG_LEVELS = [
|
|
81
81
|
:debug,
|
|
@@ -619,32 +619,32 @@ module Postburner
|
|
|
619
619
|
self.class.queue
|
|
620
620
|
end
|
|
621
621
|
|
|
622
|
-
# Returns the
|
|
622
|
+
# Returns the priority for this job instance.
|
|
623
623
|
#
|
|
624
624
|
# Checks instance-level override first, then falls back to class-level configuration.
|
|
625
625
|
#
|
|
626
626
|
# @return [Integer, nil] Priority (lower = higher priority)
|
|
627
627
|
#
|
|
628
628
|
# @example Instance-level override
|
|
629
|
-
# job = MyJob.create!(args: {},
|
|
630
|
-
# job.
|
|
629
|
+
# job = MyJob.create!(args: {}, priority: 1500)
|
|
630
|
+
# job.priority # => 1500
|
|
631
631
|
#
|
|
632
|
-
def
|
|
633
|
-
@
|
|
632
|
+
def priority
|
|
633
|
+
@priority || self.class.priority
|
|
634
634
|
end
|
|
635
635
|
|
|
636
|
-
# Returns the
|
|
636
|
+
# Returns the TTR (time-to-run) for this job instance.
|
|
637
637
|
#
|
|
638
638
|
# Checks instance-level override first, then falls back to class-level configuration.
|
|
639
639
|
#
|
|
640
640
|
# @return [Integer, nil] TTR in seconds
|
|
641
641
|
#
|
|
642
642
|
# @example Instance-level override
|
|
643
|
-
# job = MyJob.create!(args: {},
|
|
644
|
-
# job.
|
|
643
|
+
# job = MyJob.create!(args: {}, ttr: 600)
|
|
644
|
+
# job.ttr # => 600
|
|
645
645
|
#
|
|
646
|
-
def
|
|
647
|
-
@
|
|
646
|
+
def ttr
|
|
647
|
+
@ttr || self.class.ttr
|
|
648
648
|
end
|
|
649
649
|
|
|
650
650
|
def tube_name
|
|
@@ -27,7 +27,7 @@ module Postburner
|
|
|
27
27
|
#
|
|
28
28
|
def with(params={})
|
|
29
29
|
self.args.merge!(
|
|
30
|
-
'params' => ActiveJob::Arguments.serialize(params)
|
|
30
|
+
'params' => ::ActiveJob::Arguments.serialize(params)
|
|
31
31
|
)
|
|
32
32
|
self
|
|
33
33
|
end
|
|
@@ -62,7 +62,7 @@ module Postburner
|
|
|
62
62
|
# Get the deserialized params.
|
|
63
63
|
#
|
|
64
64
|
def params
|
|
65
|
-
ActiveJob::Arguments.deserialize(self.args['params']).to_h
|
|
65
|
+
::ActiveJob::Arguments.deserialize(self.args['params']).to_h
|
|
66
66
|
end
|
|
67
67
|
|
|
68
68
|
def perform(args)
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
# Scale by adjusting forks and threads per worker:
|
|
21
21
|
# - Development: forks=0, threads=1 (single-threaded, easiest debugging)
|
|
22
22
|
# - Staging: forks=0, threads=10 (multi-threaded, moderate load)
|
|
23
|
-
# - Production: forks=4, threads=10 (40 concurrent jobs per
|
|
23
|
+
# - Production: forks=4, threads=10 (40 concurrent jobs per worker)
|
|
24
24
|
#
|
|
25
25
|
|
|
26
26
|
default: &default
|
|
@@ -34,7 +34,7 @@ development:
|
|
|
34
34
|
workers:
|
|
35
35
|
default:
|
|
36
36
|
# Single-threaded, single process (simplest for debugging)
|
|
37
|
-
#
|
|
37
|
+
# If not specified, uses env-level defaults
|
|
38
38
|
queues:
|
|
39
39
|
- default
|
|
40
40
|
- mailers
|
|
@@ -45,34 +45,39 @@ test:
|
|
|
45
45
|
workers:
|
|
46
46
|
default:
|
|
47
47
|
# Test mode uses inline strategies automatically
|
|
48
|
-
# Defaults: forks=0, threads=1, gc_limit=nil
|
|
49
48
|
queues:
|
|
50
49
|
- default
|
|
51
50
|
|
|
52
|
-
staging:
|
|
51
|
+
staging: # <- environment config
|
|
53
52
|
<<: *default
|
|
54
53
|
|
|
55
|
-
|
|
54
|
+
# Env-level defaults (use default_ prefix)
|
|
55
|
+
default_threads: 10
|
|
56
|
+
default_gc_limit: 5000
|
|
57
|
+
|
|
58
|
+
workers: # <- worker config overrides
|
|
56
59
|
default:
|
|
57
60
|
# Multi-threaded, single process (moderate concurrency)
|
|
58
|
-
|
|
59
|
-
default_gc_limit: 5000
|
|
61
|
+
# Uses env-level defaults: default_threads=10, default_gc_limit=5000
|
|
60
62
|
queues:
|
|
61
63
|
- critical
|
|
62
64
|
- default
|
|
63
65
|
- mailers
|
|
64
66
|
|
|
65
|
-
production:
|
|
67
|
+
production: # <- environment config, i.e. defaults, NOT worker config
|
|
66
68
|
<<: *default
|
|
67
69
|
|
|
68
|
-
#
|
|
70
|
+
# Env-level defaults (use default_ prefix)
|
|
71
|
+
default_forks: 2
|
|
72
|
+
default_threads: 10
|
|
73
|
+
default_gc_limit: 5000
|
|
74
|
+
|
|
75
|
+
# Example 1: Single worker using env defaults
|
|
69
76
|
# Run: bin/postburner
|
|
70
77
|
#
|
|
71
78
|
# workers:
|
|
72
79
|
# default:
|
|
73
|
-
# default_forks
|
|
74
|
-
# default_threads: 10
|
|
75
|
-
# default_gc_limit: 5000
|
|
80
|
+
# # Uses default_forks=2, default_threads=10
|
|
76
81
|
# queues:
|
|
77
82
|
# - critical
|
|
78
83
|
# - default
|
|
@@ -84,21 +89,20 @@ production:
|
|
|
84
89
|
# bin/postburner --worker imports (4 forks, 1 thread each)
|
|
85
90
|
# bin/postburner --worker general (2 forks, 100 threads each)
|
|
86
91
|
#
|
|
87
|
-
workers:
|
|
92
|
+
workers: # <- worker config, i.e. overrides, NOT environment config
|
|
88
93
|
# Heavy, memory-intensive jobs - more processes, fewer threads
|
|
89
94
|
imports:
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
95
|
+
forks: 4 # Overrides default_forks
|
|
96
|
+
threads: 1 # Overrides default_threads
|
|
97
|
+
gc_limit: 500 # Overrides default_gc_limit
|
|
93
98
|
queues:
|
|
94
99
|
- imports
|
|
95
100
|
- data_processing
|
|
96
101
|
|
|
97
|
-
# General jobs -
|
|
102
|
+
# General jobs - uses env defaults (forks=2, threads=10)
|
|
103
|
+
# Override threads for higher concurrency
|
|
98
104
|
general:
|
|
99
|
-
|
|
100
|
-
default_threads: 100
|
|
101
|
-
default_gc_limit: 5000
|
|
105
|
+
threads: 100 # Overrides default_threads (forks uses default_forks=2)
|
|
102
106
|
queues:
|
|
103
107
|
- default
|
|
104
108
|
- mailers
|
|
@@ -112,27 +116,25 @@ production:
|
|
|
112
116
|
#
|
|
113
117
|
# workers:
|
|
114
118
|
# critical:
|
|
115
|
-
#
|
|
116
|
-
#
|
|
117
|
-
#
|
|
119
|
+
# forks: 1
|
|
120
|
+
# threads: 1
|
|
121
|
+
# gc_limit: 100
|
|
118
122
|
# queues:
|
|
119
123
|
# - critical
|
|
120
124
|
#
|
|
121
125
|
# default:
|
|
122
|
-
#
|
|
123
|
-
#
|
|
124
|
-
# default_gc_limit: 5000
|
|
126
|
+
# forks: 4
|
|
127
|
+
# threads: 10
|
|
125
128
|
# queues:
|
|
126
129
|
# - default
|
|
127
130
|
#
|
|
128
131
|
# mailers:
|
|
129
|
-
#
|
|
130
|
-
#
|
|
131
|
-
# default_gc_limit: 2000
|
|
132
|
+
# forks: 2
|
|
133
|
+
# threads: 5
|
|
132
134
|
# queues:
|
|
133
135
|
# - mailers
|
|
134
136
|
|
|
135
|
-
#
|
|
137
|
+
# Env-Level Defaults (can be overridden per worker):
|
|
136
138
|
#
|
|
137
139
|
# default_queue: default # Default queue name (optional)
|
|
138
140
|
# default_priority: 65536 # Lower = higher priority (optional, 0 is highest)
|
|
@@ -6,6 +6,16 @@ class Postburner::InstallGenerator < Rails::Generators::Base
|
|
|
6
6
|
install_migration! 'create_postburner_jobs'
|
|
7
7
|
end
|
|
8
8
|
|
|
9
|
+
def copy_config_file
|
|
10
|
+
destination = "config/postburner.yml"
|
|
11
|
+
|
|
12
|
+
if File.exist?(File.join(destination_root, destination))
|
|
13
|
+
say_status "skip", destination, :yellow
|
|
14
|
+
else
|
|
15
|
+
template "config/postburner.yml", destination
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
9
19
|
def self.next_migration_number(dirname)
|
|
10
20
|
timestamp = Time.now.utc.strftime("%Y%m%d%H%M%S")
|
|
11
21
|
stem = timestamp[0..-3]
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
# Postburner Configuration Example
|
|
2
|
+
#
|
|
3
|
+
# Copy this file to config/postburner.yml and customize for your environment.
|
|
4
|
+
#
|
|
5
|
+
# ## Named Workers Configuration
|
|
6
|
+
#
|
|
7
|
+
# Postburner uses named worker configurations to support different deployment patterns:
|
|
8
|
+
# - Single worker: bin/postburner (auto-selects the single worker)
|
|
9
|
+
# - Multiple workers: bin/postburner --worker <name> (must specify which worker)
|
|
10
|
+
#
|
|
11
|
+
# Each worker can have different fork/thread settings and process different queues.
|
|
12
|
+
# This enables running different queue groups in separate OS processes with distinct
|
|
13
|
+
# concurrency profiles.
|
|
14
|
+
#
|
|
15
|
+
# ## Puma-Style Architecture
|
|
16
|
+
#
|
|
17
|
+
# - **forks: 0** = Single process with thread pool (development/staging)
|
|
18
|
+
# - **forks: 1+** = Multiple processes with thread pools (production)
|
|
19
|
+
#
|
|
20
|
+
# Scale by adjusting forks and threads per worker:
|
|
21
|
+
# - Development: forks=0, threads=1 (single-threaded, easiest debugging)
|
|
22
|
+
# - Staging: forks=0, threads=10 (multi-threaded, moderate load)
|
|
23
|
+
# - Production: forks=4, threads=10 (40 concurrent jobs per worker)
|
|
24
|
+
#
|
|
25
|
+
|
|
26
|
+
default: &default
|
|
27
|
+
# Beanstalkd connection URL
|
|
28
|
+
# Override with ENV['BEANSTALK_URL'] if set
|
|
29
|
+
beanstalk_url: <%= ENV['BEANSTALK_URL'] || 'beanstalk://localhost:11300' %>
|
|
30
|
+
|
|
31
|
+
development:
|
|
32
|
+
<<: *default
|
|
33
|
+
|
|
34
|
+
workers:
|
|
35
|
+
default:
|
|
36
|
+
# Single-threaded, single process (simplest for debugging)
|
|
37
|
+
# If not specified, uses env-level defaults
|
|
38
|
+
queues:
|
|
39
|
+
- default
|
|
40
|
+
- mailers
|
|
41
|
+
|
|
42
|
+
test:
|
|
43
|
+
<<: *default
|
|
44
|
+
|
|
45
|
+
workers:
|
|
46
|
+
default:
|
|
47
|
+
# Test mode uses inline strategies automatically
|
|
48
|
+
queues:
|
|
49
|
+
- default
|
|
50
|
+
|
|
51
|
+
staging: # <- environment config
|
|
52
|
+
<<: *default
|
|
53
|
+
|
|
54
|
+
# Env-level defaults (use default_ prefix)
|
|
55
|
+
default_threads: 10
|
|
56
|
+
default_gc_limit: 5000
|
|
57
|
+
|
|
58
|
+
workers: # <- worker config overrides
|
|
59
|
+
default:
|
|
60
|
+
# Multi-threaded, single process (moderate concurrency)
|
|
61
|
+
# Uses env-level defaults: default_threads=10, default_gc_limit=5000
|
|
62
|
+
queues:
|
|
63
|
+
- critical
|
|
64
|
+
- default
|
|
65
|
+
- mailers
|
|
66
|
+
|
|
67
|
+
production: # <- environment config, i.e. defaults, NOT worker config
|
|
68
|
+
<<: *default
|
|
69
|
+
|
|
70
|
+
# Env-level defaults (use default_ prefix)
|
|
71
|
+
default_forks: 2
|
|
72
|
+
default_threads: 10
|
|
73
|
+
default_gc_limit: 5000
|
|
74
|
+
|
|
75
|
+
# Example 1: Single worker using env defaults
|
|
76
|
+
# Run: bin/postburner
|
|
77
|
+
#
|
|
78
|
+
# workers:
|
|
79
|
+
# default:
|
|
80
|
+
# # Uses default_forks=2, default_threads=10
|
|
81
|
+
# queues:
|
|
82
|
+
# - critical
|
|
83
|
+
# - default
|
|
84
|
+
# - mailers
|
|
85
|
+
# - imports
|
|
86
|
+
|
|
87
|
+
# Example 2: Multiple workers with different concurrency profiles
|
|
88
|
+
# Run separate processes:
|
|
89
|
+
# bin/postburner --worker imports (4 forks, 1 thread each)
|
|
90
|
+
# bin/postburner --worker general (2 forks, 100 threads each)
|
|
91
|
+
#
|
|
92
|
+
workers: # <- worker config, i.e. overrides, NOT environment config
|
|
93
|
+
# Heavy, memory-intensive jobs - more processes, fewer threads
|
|
94
|
+
imports:
|
|
95
|
+
forks: 4 # Overrides default_forks
|
|
96
|
+
threads: 1 # Overrides default_threads
|
|
97
|
+
gc_limit: 500 # Overrides default_gc_limit
|
|
98
|
+
queues:
|
|
99
|
+
- imports
|
|
100
|
+
- data_processing
|
|
101
|
+
|
|
102
|
+
# General jobs - uses env defaults (forks=2, threads=10)
|
|
103
|
+
# Override threads for higher concurrency
|
|
104
|
+
general:
|
|
105
|
+
threads: 100 # Overrides default_threads (forks uses default_forks=2)
|
|
106
|
+
queues:
|
|
107
|
+
- default
|
|
108
|
+
- mailers
|
|
109
|
+
- notifications
|
|
110
|
+
|
|
111
|
+
# Example 3: Fine-grained control with multiple specialized workers
|
|
112
|
+
# Run separate processes:
|
|
113
|
+
# bin/postburner --worker critical
|
|
114
|
+
# bin/postburner --worker default
|
|
115
|
+
# bin/postburner --worker mailers
|
|
116
|
+
#
|
|
117
|
+
# workers:
|
|
118
|
+
# critical:
|
|
119
|
+
# forks: 1
|
|
120
|
+
# threads: 1
|
|
121
|
+
# gc_limit: 100
|
|
122
|
+
# queues:
|
|
123
|
+
# - critical
|
|
124
|
+
#
|
|
125
|
+
# default:
|
|
126
|
+
# forks: 4
|
|
127
|
+
# threads: 10
|
|
128
|
+
# queues:
|
|
129
|
+
# - default
|
|
130
|
+
#
|
|
131
|
+
# mailers:
|
|
132
|
+
# forks: 2
|
|
133
|
+
# threads: 5
|
|
134
|
+
# queues:
|
|
135
|
+
# - mailers
|
|
136
|
+
|
|
137
|
+
# Env-Level Defaults (can be overridden per worker):
|
|
138
|
+
#
|
|
139
|
+
# default_queue: default # Default queue name (optional)
|
|
140
|
+
# default_priority: 65536 # Lower = higher priority (optional, 0 is highest)
|
|
141
|
+
# default_ttr: 300 # Time-to-run in seconds (optional)
|
|
142
|
+
# default_threads: 1 # Thread count per fork (optional, defaults to 1)
|
|
143
|
+
# default_forks: 0 # Fork count (optional, defaults to 0 = single process)
|
|
144
|
+
# default_gc_limit: nil # Exit after N jobs for restart (optional, nil = no limit)
|
|
@@ -58,6 +58,19 @@ module ActiveJob
|
|
|
58
58
|
end
|
|
59
59
|
end
|
|
60
60
|
|
|
61
|
+
# Indicates whether the adapter supports enqueuing after transaction commit.
|
|
62
|
+
#
|
|
63
|
+
# Returns true because Postburner uses Beanstalkd (external queue),
|
|
64
|
+
# which means jobs are safely enqueued outside the database transaction.
|
|
65
|
+
# This allows Rails to automatically defer job enqueuing until after
|
|
66
|
+
# the current database transaction commits.
|
|
67
|
+
#
|
|
68
|
+
# @return [Boolean] Always returns true
|
|
69
|
+
#
|
|
70
|
+
def enqueue_after_transaction_commit?
|
|
71
|
+
true
|
|
72
|
+
end
|
|
73
|
+
|
|
61
74
|
private
|
|
62
75
|
|
|
63
76
|
# Checks if a job should be tracked in PostgreSQL.
|
|
@@ -87,7 +100,7 @@ module ActiveJob
|
|
|
87
100
|
# Create Postburner::TrackedJob record
|
|
88
101
|
tracked_job = Postburner::TrackedJob.create!(
|
|
89
102
|
args: Postburner::ActiveJob::Payload.serialize_for_tracked(job),
|
|
90
|
-
run_at: timestamp,
|
|
103
|
+
run_at: timestamp ? Time.zone.at(timestamp) : nil,
|
|
91
104
|
queued_at: Time.zone.now
|
|
92
105
|
)
|
|
93
106
|
|
|
@@ -98,10 +111,10 @@ module ActiveJob
|
|
|
98
111
|
Postburner.connected do |conn|
|
|
99
112
|
tube_name = expand_tube_name(job.queue_name)
|
|
100
113
|
|
|
101
|
-
# Get priority and TTR
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
ttr = job.class.respond_to?(:
|
|
114
|
+
# Get priority and TTR
|
|
115
|
+
# Priority order: job.priority (from .set or class.priority) > default
|
|
116
|
+
pri = job.priority || Postburner.configuration.default_priority
|
|
117
|
+
ttr = job.class.respond_to?(:postburner_ttr) && job.class.postburner_ttr ||
|
|
105
118
|
Postburner.configuration.default_ttr
|
|
106
119
|
|
|
107
120
|
bkid = conn.tubes[tube_name].put(
|
|
@@ -132,10 +145,10 @@ module ActiveJob
|
|
|
132
145
|
Postburner.connected do |conn|
|
|
133
146
|
tube_name = expand_tube_name(job.queue_name)
|
|
134
147
|
|
|
135
|
-
# Get priority and TTR
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
ttr = job.class.respond_to?(:
|
|
148
|
+
# Get priority and TTR
|
|
149
|
+
# Priority order: job.priority (from .set or class.priority) > default
|
|
150
|
+
pri = job.priority || Postburner.configuration.default_priority
|
|
151
|
+
ttr = job.class.respond_to?(:postburner_ttr) && job.class.postburner_ttr ||
|
|
139
152
|
Postburner.configuration.default_ttr
|
|
140
153
|
|
|
141
154
|
conn.tubes[tube_name].put(
|
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
module Postburner
|
|
4
4
|
# Beanstalkd-specific configuration DSL for ActiveJob classes using Postburner.
|
|
5
5
|
#
|
|
6
|
-
# Provides
|
|
7
|
-
#
|
|
8
|
-
# to use Beanstalkd-specific configuration.
|
|
6
|
+
# Provides TTR (time-to-run) configuration for Beanstalkd. For priority, use
|
|
7
|
+
# ActiveJob's built-in `priority` method. Include this module in your ActiveJob
|
|
8
|
+
# classes to use Beanstalkd-specific configuration.
|
|
9
9
|
#
|
|
10
10
|
# @note Automatically included when you include Postburner::Tracked
|
|
11
11
|
#
|
|
@@ -14,8 +14,8 @@ module Postburner
|
|
|
14
14
|
# include Postburner::Beanstalkd
|
|
15
15
|
#
|
|
16
16
|
# queue_as :critical
|
|
17
|
-
#
|
|
18
|
-
#
|
|
17
|
+
# priority 0 # Highest priority (uses ActiveJob's priority)
|
|
18
|
+
# ttr 300 # 5 minutes to complete
|
|
19
19
|
#
|
|
20
20
|
# def perform(payment_id)
|
|
21
21
|
# # ...
|
|
@@ -26,8 +26,8 @@ module Postburner
|
|
|
26
26
|
# class ProcessPayment < ApplicationJob
|
|
27
27
|
# include Postburner::Tracked # Includes Beanstalkd automatically
|
|
28
28
|
#
|
|
29
|
-
#
|
|
30
|
-
#
|
|
29
|
+
# priority 0
|
|
30
|
+
# ttr 600
|
|
31
31
|
#
|
|
32
32
|
# def perform(payment_id)
|
|
33
33
|
# log "Processing payment"
|
|
@@ -39,54 +39,33 @@ module Postburner
|
|
|
39
39
|
extend ActiveSupport::Concern
|
|
40
40
|
|
|
41
41
|
included do
|
|
42
|
-
class_attribute :postburner_priority, default: nil
|
|
43
42
|
class_attribute :postburner_ttr, default: nil
|
|
44
43
|
end
|
|
45
44
|
|
|
46
45
|
class_methods do
|
|
47
|
-
# Sets or returns the
|
|
48
|
-
#
|
|
49
|
-
# Lower numbers = higher priority in Beanstalkd (0 is highest).
|
|
50
|
-
# Falls back to Postburner.configuration.default_priority if not set.
|
|
51
|
-
#
|
|
52
|
-
# @param pri [Integer, nil] Priority to set (0-4294967295), or nil to get current value
|
|
53
|
-
#
|
|
54
|
-
# @return [Integer, nil] Current priority when getting, nil when setting
|
|
55
|
-
#
|
|
56
|
-
# @example Set priority
|
|
57
|
-
# queue_priority 0 # Highest priority
|
|
58
|
-
#
|
|
59
|
-
# @example Get priority
|
|
60
|
-
# ProcessPayment.queue_priority # => 0
|
|
61
|
-
#
|
|
62
|
-
def queue_priority(pri = nil)
|
|
63
|
-
if pri
|
|
64
|
-
self.postburner_priority = pri
|
|
65
|
-
nil
|
|
66
|
-
else
|
|
67
|
-
postburner_priority
|
|
68
|
-
end
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
# Sets or returns the queue TTR (time to run).
|
|
46
|
+
# Sets or returns the TTR (time to run).
|
|
72
47
|
#
|
|
73
48
|
# Number of seconds Beanstalkd will wait for job completion before
|
|
74
49
|
# making it available again. Falls back to Postburner.configuration.default_ttr
|
|
75
50
|
# if not set.
|
|
76
51
|
#
|
|
77
|
-
#
|
|
52
|
+
# From Beanstalkd: If the worker does not delete, release, or bury the
|
|
53
|
+
# job within <ttr> seconds, the job will time out and the server will
|
|
54
|
+
# release the job.
|
|
55
|
+
#
|
|
56
|
+
# @param seconds [Integer, nil] Timeout in seconds, or nil to get current value
|
|
78
57
|
#
|
|
79
58
|
# @return [Integer, nil] Current TTR when getting, nil when setting
|
|
80
59
|
#
|
|
81
60
|
# @example Set TTR
|
|
82
|
-
#
|
|
61
|
+
# ttr 300 # 5 minutes
|
|
83
62
|
#
|
|
84
63
|
# @example Get TTR
|
|
85
|
-
# ProcessPayment.
|
|
64
|
+
# ProcessPayment.ttr # => 300
|
|
86
65
|
#
|
|
87
|
-
def
|
|
88
|
-
if
|
|
89
|
-
self.postburner_ttr =
|
|
66
|
+
def ttr(seconds = nil)
|
|
67
|
+
if seconds
|
|
68
|
+
self.postburner_ttr = seconds
|
|
90
69
|
nil
|
|
91
70
|
else
|
|
92
71
|
postburner_ttr
|
|
@@ -27,8 +27,8 @@ module Postburner
|
|
|
27
27
|
# @option options [String] :default_queue Default queue name (default: 'default')
|
|
28
28
|
# @option options [Integer] :default_priority Default job priority (default: 65536, lower = higher priority)
|
|
29
29
|
# @option options [Integer] :default_ttr Default time-to-run in seconds (default: 300)
|
|
30
|
-
# @option options [Integer] :default_threads Default thread count per
|
|
31
|
-
# @option options [Integer] :default_forks Default fork count
|
|
30
|
+
# @option options [Integer] :default_threads Default thread count per fork (default: 1)
|
|
31
|
+
# @option options [Integer] :default_forks Default fork count (default: 0, single process)
|
|
32
32
|
# @option options [Integer] :default_gc_limit Default GC limit for worker restarts (default: nil, no limit)
|
|
33
33
|
#
|
|
34
34
|
def initialize(options = {})
|
|
@@ -62,13 +62,36 @@ module Postburner
|
|
|
62
62
|
# @example Multiple workers (must specify)
|
|
63
63
|
# config = Postburner::Configuration.load_yaml('config/postburner.yml', 'production', 'imports')
|
|
64
64
|
#
|
|
65
|
+
# @example config/postburner.yml example
|
|
66
|
+
#
|
|
67
|
+
# default: &default
|
|
68
|
+
# beanstalk_url: beanstalk://localhost:11300
|
|
69
|
+
# default_priority: 131072 # change default priority from 65536 to 131072
|
|
70
|
+
#
|
|
71
|
+
# production: # <- environment config, i.e. defaults, NOT worker config
|
|
72
|
+
# <<: *default
|
|
73
|
+
# default_forks: 2
|
|
74
|
+
# default_threads: 10
|
|
75
|
+
# default_gc_limit: 5000
|
|
76
|
+
# default_ttr: 300
|
|
77
|
+
# workers: # <- worker config, i.e. overrides, NOT environment config
|
|
78
|
+
# imports: # <- worker "group" name
|
|
79
|
+
# forks: 4 # Overrides default_forks
|
|
80
|
+
# threads: 1 # Overrides default_threads
|
|
81
|
+
# gc_limit: 500 # Overrides default_gc_limit
|
|
82
|
+
# # ttr: 60 # Use default from production, i.e. 300 because not set
|
|
83
|
+
# queues:
|
|
84
|
+
# - imports
|
|
85
|
+
# - data_processing
|
|
86
|
+
#
|
|
65
87
|
def self.load_yaml(path, env = 'development', worker_name = nil)
|
|
66
|
-
yaml = YAML.load_file(path)
|
|
67
|
-
|
|
88
|
+
yaml = YAML.load_file(path, aliases: true)
|
|
89
|
+
# env_defaults = top-level environment config (development:, production:, etc.)
|
|
90
|
+
env_defaults = yaml[env.to_s] || yaml[env.to_sym]
|
|
68
91
|
|
|
69
|
-
raise ArgumentError, "Environment '#{env}' not found in #{path}" unless
|
|
92
|
+
raise ArgumentError, "Environment '#{env}' not found in #{path}" unless env_defaults
|
|
70
93
|
|
|
71
|
-
workers =
|
|
94
|
+
workers = env_defaults['workers']
|
|
72
95
|
raise ArgumentError, "No 'workers:' section found in #{path} for environment '#{env}'" unless workers
|
|
73
96
|
|
|
74
97
|
# Auto-select single worker or validate worker_name
|
|
@@ -84,24 +107,28 @@ module Postburner
|
|
|
84
107
|
end
|
|
85
108
|
end
|
|
86
109
|
|
|
110
|
+
# worker_config = specific worker configuration (workers: imports:)
|
|
87
111
|
worker_config = workers[worker_name]
|
|
88
112
|
|
|
89
|
-
# Convert queue array to hash format
|
|
113
|
+
# Convert queue array to hash format (queues no longer have per-queue config)
|
|
90
114
|
queue_list = worker_config['queues'] || []
|
|
91
115
|
queues_hash = {}
|
|
92
116
|
queue_list.each do |queue_name|
|
|
93
|
-
queues_hash[queue_name] = {}
|
|
117
|
+
queues_hash[queue_name] = {} # Empty hash - queues run in worker pool
|
|
94
118
|
end
|
|
95
119
|
|
|
120
|
+
# Cascade: worker-level overrides env-level defaults
|
|
121
|
+
# Worker uses: forks, threads, gc_limit, ttr, priority (NO default_ prefix)
|
|
122
|
+
# Env uses: default_forks, default_threads, etc. (WITH default_ prefix)
|
|
96
123
|
options = {
|
|
97
|
-
beanstalk_url:
|
|
124
|
+
beanstalk_url: env_defaults['beanstalk_url'],
|
|
98
125
|
queues: queues_hash,
|
|
99
|
-
default_queue: worker_config['default_queue'] ||
|
|
100
|
-
default_priority: worker_config['
|
|
101
|
-
default_ttr: worker_config['
|
|
102
|
-
default_threads: worker_config['
|
|
103
|
-
default_forks: worker_config['
|
|
104
|
-
default_gc_limit: worker_config['
|
|
126
|
+
default_queue: worker_config['default_queue'] || env_defaults['default_queue'],
|
|
127
|
+
default_priority: worker_config['priority'] || env_defaults['default_priority'],
|
|
128
|
+
default_ttr: worker_config['ttr'] || env_defaults['default_ttr'],
|
|
129
|
+
default_threads: worker_config['threads'] || env_defaults['default_threads'],
|
|
130
|
+
default_forks: worker_config['forks'] || env_defaults['default_forks'],
|
|
131
|
+
default_gc_limit: worker_config['gc_limit'] || env_defaults['default_gc_limit']
|
|
105
132
|
}
|
|
106
133
|
|
|
107
134
|
new(options)
|