Skip to content

Commit c9287f0

Browse files
committed
Add optional name parameter for thread naming
1 parent 65baa0c commit c9287f0

3 files changed

Lines changed: 17 additions & 2 deletions

File tree

lib/batch_queue/batch_queue.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
class BatchQueue
2+
attr_reader :name
23
attr_reader :max_batch_size
34
attr_reader :max_interval_seconds
45

56
# starts the queue
67
# either max_batch_size or interval_milliseconds or both must be set
7-
def initialize(max_batch_size: nil, max_interval_seconds: nil, &block)
8+
def initialize(name: nil, max_batch_size: nil, max_interval_seconds: nil, &block)
89
if max_batch_size.nil? && max_interval_seconds.nil?
910
raise 'either max_batch_size or max_interval_seconds or both must be set'
1011
end
1112
@is_running = true
1213
@queue = Queue.new
1314
@block = block
15+
@name = name
1416
@max_batch_size = max_batch_size
1517
@max_interval_seconds = max_interval_seconds
1618
@mutex = Mutex.new
1719
@cond_var = ConditionVariable.new
1820
@runner = Thread.new { run }
21+
@runner.name = name if !name.nil? && @runner.respond_to?(:name=)
1922
@on_error_callback = nil
2023

2124
at_exit do

lib/batch_queue/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
class BatchQueue
2-
VERSION = "1.1.0"
2+
VERSION = "1.2.0"
33
end

test/batch_queue_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,16 @@ def test_it_handles_close
8080
assert_equal 0, bq.size
8181
assert_equal 1, processed_count
8282
end
83+
84+
def test_it_sets_thread_name
85+
thread_name = nil
86+
bq = BatchQueue.new(name: 'test-thread', max_batch_size: 1) do |arr|
87+
thread_name = Thread.current.name
88+
end
89+
90+
bq << 'test'
91+
sleep(0.2)
92+
assert_equal 'test-thread', thread_name
93+
bq.stop
94+
end
8395
end

0 commit comments

Comments
 (0)