Google Cloud Pub/Sub adapter and worker for ActiveJob
gem 'activejob-google-cloud-pubsub'
First, change the ActiveJob backend.
Rails.application.config.active_job.queue_adapter = :google_cloud_pubsub
Write the Job class and code to use it.
class HelloJob < ApplicationJob
def perform(name)
puts "hello, #{name}!"
end
end
class HelloController < ApplicationController
def say
HelloJob.perform_later params[:name]
end
end
In order to test the worker in your local environment, it is a good idea to use the Pub/Sub emulator provided by gcloud
command. Refer to this document for the installation procedure.
When the installation is completed, execute the following command to start up the worker.
$ gcloud beta emulators pubsub start
(Switch to another terminal)
$ eval `gcloud beta emulators pubsub env-init`
$ cd path/to/your-app
$ bundle exec activejob-google-cloud-pubsub-worker --project_id=dummy
If you hit the previous action, the job will be executed. (Both the emulator and the worker stop with Ctrl+C)
When passing options to the adapter, you need to create the object instead of a symbol.
Rails.application.config.active_job.queue_adapter = ActiveJob::GoogleCloudPubsub::Adapter.new(
async: false,
logger: Rails.logger,
pubsub: Google::Cloud::Pubsub.new(
project_id: 'MY-PROJECT-ID',
credentials: 'path/to/keyfile.json',
timeout: 60
)
)
Whether to publish messages asynchronously.
Default: true
The logger that outputs a message publishing error. Specify nil
to disable logging.
Default: Logger.new($stdout)
The instance of Google::Cloud::Pubsub::Project
. Please see Google::Cloud::Pubsub.new
for details.
Default: Google::Cloud::Pubsub.new(timeout: 60)
The following command line flags can be specified. All flags are optional.
The path of the file to load before the worker starts up.
Default: ./config/environment
The name of the queue the worker handles.
Note: One worker can handle only one queue. If you use multiple queues, you need to launch multiple worker processes.
Default: default
Credentials of Google Cloud Platform. Please see the document for details.
$ bundle exec rake spec
Bug reports and pull requests are welcome on GitHub at https://github.com/ursm/activejob-google_cloud_pubsub.
The gem is available as open source under the terms of the MIT License.