Skip to content
This repository has been archived by the owner on Feb 27, 2021. It is now read-only.

No thread mode #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ Maximum number of worker threads.

Default: number of logical cores

#### `--no-use_threads`

Do not use threads, process each message using raw pub/sub event
callback.

Default: use threads

#### `--project_id=PROJECT_ID`, `--credentials=KEYFILE_PATH`

Credentials of Google Cloud Platform. Please see [the document](https://github.com/GoogleCloudPlatform/google-cloud-ruby/blob/master/AUTHENTICATION.md) for details.
Expand Down
4 changes: 4 additions & 0 deletions exe/activejob-google_cloud_pubsub-worker
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ worker_args = parser.define_by_keywords(
}
)

parser.on '--[no-]threads' do |o|
worker_args[:use_threads] = o
end

pubsub_args = parser.define_by_keywords(
{},
Google::Cloud::Pubsub.method(:new),
Expand Down
45 changes: 29 additions & 16 deletions lib/activejob-google_cloud_pubsub/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,53 @@ class Worker

using PubsubExtension

def initialize(queue: 'default', min_threads: 0, max_threads: Concurrent.processor_count, pubsub: Google::Cloud::Pubsub.new, logger: Logger.new($stdout))
def initialize(queue: 'default', min_threads: 0, max_threads: Concurrent.processor_count, use_threads: true, pubsub: Google::Cloud::Pubsub.new, logger: Logger.new($stdout))
@queue_name = queue
@min_threads = min_threads
@max_threads = max_threads
@use_threads = use_threads
@pubsub = pubsub
@logger = logger
end

def run
pool = Concurrent::ThreadPoolExecutor.new(min_threads: @min_threads, max_threads: @max_threads, max_queue: -1)
if @use_threads
pool = Concurrent::ThreadPoolExecutor.new(min_threads: @min_threads, max_threads: @max_threads, max_queue: -1)
else
@logger&.warn "Running without threads"
end

@pubsub.subscription_for(@queue_name).listen {|message|
@logger&.info "Message(#{message.message_id}) was received."

begin
Concurrent::Promise.execute(args: message, executor: pool) {|msg|
process_or_delay msg
}.rescue {|e|
@logger&.error e
}
rescue Concurrent::RejectedExecutionError
Concurrent::Promise.execute(args: message) {|msg|
msg.delay! 10.seconds.to_i

@logger&.info "Message(#{msg.message_id}) was rescheduled after 10 seconds because the thread pool is full."
}.rescue {|e|
@logger&.error e
}
if @use_threads
run_concurrent message, pool
else
process message
end
}.start

sleep
end

def run_concurrent(message, pool)
begin
Concurrent::Promise.execute(args: message, executor: pool) {|msg|
process_or_delay msg
}.rescue {|e|
@logger&.error e
}
rescue Concurrent::RejectedExecutionError
Concurrent::Promise.execute(args: message) {|msg|
msg.delay! 10.seconds.to_i

@logger&.info "Message(#{msg.message_id}) was rescheduled after 10 seconds because the thread pool is full."
}.rescue {|e|
@logger&.error e
}
end
end

def ensure_subscription
@pubsub.subscription_for @queue_name

Expand Down