Skip to content

Commit

Permalink
Start process_queue_thread when dead
Browse files Browse the repository at this point in the history
  • Loading branch information
bmansoob committed Dec 23, 2024
1 parent 7bfbe6e commit d44d9bb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
8 changes: 5 additions & 3 deletions lib/app_profiler/storage/google_cloud_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def upload(profile, _params = {})

def enqueue_upload(profile)
mutex.synchronize do
process_queue_thread unless @process_queue_thread&.alive?
start_process_queue_thread

@queue ||= init_queue
begin
Expand All @@ -60,8 +60,10 @@ def init_queue
@queue = SizedQueue.new(AppProfiler.upload_queue_max_length)
end

def process_queue_thread
@process_queue_thread ||= Thread.new do
def start_process_queue_thread
return if @process_queue_thread&.alive?

@process_queue_thread = Thread.new do
loop do
process_queue
sleep(AppProfiler.upload_queue_interval_secs)
Expand Down
20 changes: 18 additions & 2 deletions test/app_profiler/storage/google_cloud_storage_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,22 @@ class GoogleCloudStorageTest < AppProfiler::TestCase
assert(th.alive?)
end

test "process_queue_thread is recreated on enqueue if stopped" do
GoogleCloudStorage.enqueue_upload(profile_from_stackprof)
th = GoogleCloudStorage.instance_variable_get(:@process_queue_thread)
assert(th.alive?)

th.kill
th.join

GoogleCloudStorage.enqueue_upload(profile_from_stackprof)
assert_not_predicate(th, :alive?)

GoogleCloudStorage.send(:start_process_queue_thread)
th = GoogleCloudStorage.instance_variable_get(:@process_queue_thread)
assert(th.alive?)
end

private

def with_forward_metadata_on_upload
Expand All @@ -131,10 +147,10 @@ def with_forward_metadata_on_upload

def with_stubbed_process_queue_thread
# Stub out the thread creation, so that tests are not flaky.
GoogleCloudStorage.stubs(:process_queue_thread)
GoogleCloudStorage.stubs(:start_process_queue_thread)
yield
ensure
GoogleCloudStorage.unstub(:process_queue_thread)
GoogleCloudStorage.unstub(:start_process_queue_thread)
end

def profile_from_stackprof
Expand Down

0 comments on commit d44d9bb

Please sign in to comment.