pubilion 0.1.0
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 +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +16 -0
- data/.ruby-version +1 -0
- data/.solargraph.yml +10 -0
- data/.yardopts +4 -0
- data/CHANGELOG.md +3 -0
- data/LICENSE +21 -0
- data/README.md +97 -0
- data/Rakefile +12 -0
- data/compose.yml +9 -0
- data/exe/pubilion +6 -0
- data/lib/pubilion/cli/subscription.rb +26 -0
- data/lib/pubilion/cli/topic.rb +24 -0
- data/lib/pubilion/cli.rb +21 -0
- data/lib/pubilion/config.rb +61 -0
- data/lib/pubilion/errors.rb +12 -0
- data/lib/pubilion/extensions/active_job/queue_adapter.rb +39 -0
- data/lib/pubilion/extensions.rb +3 -0
- data/lib/pubilion/generators/install_generator.rb +18 -0
- data/lib/pubilion/generators/templates/pubilion.rb +19 -0
- data/lib/pubilion/railties.rb +16 -0
- data/lib/pubilion/tasks/worker.rake +10 -0
- data/lib/pubilion/version.rb +5 -0
- data/lib/pubilion/worker/message_handler.rb +53 -0
- data/lib/pubilion/worker/runner.rb +36 -0
- data/lib/pubilion/worker/subscriber.rb +70 -0
- data/lib/pubilion/worker.rb +16 -0
- data/lib/pubilion.rb +15 -0
- data/sig/pubilion.rbs +4 -0
- metadata +134 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 32330acf3d201866165a6e8b5e10b8fa458d97d49b296debbda199a88ee92270
|
|
4
|
+
data.tar.gz: aa64ea1215012ba90d25d08700c711261aaf3538aed3468587cfb226f2037197
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: a72c58e9dd5a8c397695914141480a6ee94ef3d920ac880a9217259b15e4693606861cd66c06bae9810d5a21c182b03f18d480e2105cb5fed45f3a0a1af93aed
|
|
7
|
+
data.tar.gz: 59fd7354d2fac6ae55c34d1d867f02ef9f8f9467f272dd84166307ba49645a7d7681ac725d90544bef168c8fa642aa01e4e7bd2027d91b48b8cced08a2569df2
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
AllCops:
|
|
2
|
+
TargetRubyVersion: 3.0
|
|
3
|
+
NewCops: enable
|
|
4
|
+
Exclude:
|
|
5
|
+
- "example/**/*"
|
|
6
|
+
|
|
7
|
+
Style/StringLiterals:
|
|
8
|
+
EnforcedStyle: double_quotes
|
|
9
|
+
|
|
10
|
+
Style/StringLiteralsInInterpolation:
|
|
11
|
+
EnforcedStyle: double_quotes
|
|
12
|
+
|
|
13
|
+
Metrics/BlockLength:
|
|
14
|
+
Exclude:
|
|
15
|
+
- "spec/**/*_spec.rb"
|
|
16
|
+
- "pubilion.gemspec"
|
data/.ruby-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.3.6
|
data/.solargraph.yml
ADDED
data/.yardopts
ADDED
data/CHANGELOG.md
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 LruLab
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# Pubilion
|
|
2
|
+
|
|
3
|
+
Pubilion is a Ruby gem that provides a simple way to use ActiveJob with Cloud Pub/Sub.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'pubilion'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
And execute command:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
bundle install
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
> [!NOTE]
|
|
22
|
+
> This gem is built to be used with rails.
|
|
23
|
+
> There is currently no usage in Plain Ruby.
|
|
24
|
+
|
|
25
|
+
### Configuration
|
|
26
|
+
|
|
27
|
+
1. Generate initializer file.
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
rails generate pubilion:install
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
2. Specify settings in the initializer file. See your `config/initializers/pubilion.rb` for details.
|
|
34
|
+
|
|
35
|
+
### Message Publish
|
|
36
|
+
|
|
37
|
+
1. Set queue adapter to `pub_sub`.
|
|
38
|
+
|
|
39
|
+
```ruby
|
|
40
|
+
# config/application.rb
|
|
41
|
+
config.active_job.queue_adapter = :pub_sub
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
or
|
|
45
|
+
|
|
46
|
+
```ruby
|
|
47
|
+
# app/jobs/application_job.rb (or any other job file)
|
|
48
|
+
class ApplicationJob < ActiveJob::Base
|
|
49
|
+
self.queue_adapter = :pub_sub
|
|
50
|
+
end
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
2. Create a job class.
|
|
54
|
+
|
|
55
|
+
```ruby
|
|
56
|
+
# app/jobs/hello_job.rb
|
|
57
|
+
class HelloJob < ApplicationJob
|
|
58
|
+
# `queue_as` is recognized as a Topic name.
|
|
59
|
+
queue_as :topic_name
|
|
60
|
+
|
|
61
|
+
def perform(*args)
|
|
62
|
+
puts "Hello, World!"
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
3. Call `perform_later`.
|
|
68
|
+
|
|
69
|
+
```ruby
|
|
70
|
+
HelloJob.perform_later
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Message Subscribe
|
|
74
|
+
|
|
75
|
+
> [!NOTE]
|
|
76
|
+
> Currently, the worker is support only single subscription.
|
|
77
|
+
> If you want to use multiple subscriptions, you need to create worker per subscription.
|
|
78
|
+
|
|
79
|
+
1. Run the worker.
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
bundle exec rake pubilion:worker:start
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Development
|
|
86
|
+
|
|
87
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
88
|
+
|
|
89
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
90
|
+
|
|
91
|
+
## Contributing
|
|
92
|
+
|
|
93
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/LruLab/pubilion.
|
|
94
|
+
|
|
95
|
+
## License
|
|
96
|
+
|
|
97
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/compose.yml
ADDED
data/exe/pubilion
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "thor"
|
|
4
|
+
|
|
5
|
+
module Pubilion
|
|
6
|
+
class CLI < Thor
|
|
7
|
+
class Subscription < Thor
|
|
8
|
+
desc "create TOPIC_NAME SUBSCRIPTION_NAME", "Create a new subscription"
|
|
9
|
+
option :topic, required: true, desc: "The topic name"
|
|
10
|
+
def create(subscription_name)
|
|
11
|
+
topic = client.find_topic(options[:topic])
|
|
12
|
+
subscription = topic.subscribe(subscription_name)
|
|
13
|
+
puts "Subscription #{subscription.name} created."
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
def client
|
|
19
|
+
@client ||= Google::Cloud::PubSub.new(
|
|
20
|
+
project_id: Pubilion::Config.project_id,
|
|
21
|
+
credentials: Pubilion::Config.credentials
|
|
22
|
+
)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozern_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "thor"
|
|
4
|
+
|
|
5
|
+
module Pubilion
|
|
6
|
+
class CLI < Thor
|
|
7
|
+
class Topic < Thor
|
|
8
|
+
desc "create TOPIC_NAME", "Create a new topic"
|
|
9
|
+
def create(topic_name)
|
|
10
|
+
topic = client.create_topic(topic_name)
|
|
11
|
+
puts "Topic #{topic.name} created."
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
private
|
|
15
|
+
|
|
16
|
+
def client
|
|
17
|
+
@client ||= Google::Cloud::PubSub.new(
|
|
18
|
+
project_id: Pubilion::Config.project_id,
|
|
19
|
+
credentials: Pubilion::Config.credentials
|
|
20
|
+
)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
data/lib/pubilion/cli.rb
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "thor"
|
|
4
|
+
require "pubilion/cli/topic"
|
|
5
|
+
require "pubilion/cli/subscription"
|
|
6
|
+
|
|
7
|
+
module Pubilion
|
|
8
|
+
# Command Line Interface for Pubilion
|
|
9
|
+
class CLI < Thor
|
|
10
|
+
desc "subscription SUBCOMMAND ...ARGS", "Manage subscriptions"
|
|
11
|
+
subcommand "subscription", Pubilion::CLI::Subscription
|
|
12
|
+
|
|
13
|
+
desc "topic SUBCOMMAND ...ARGS", "Manage topics"
|
|
14
|
+
subcommand "topic", Pubilion::CLI::Topic
|
|
15
|
+
|
|
16
|
+
desc "version", "Print the version"
|
|
17
|
+
def version
|
|
18
|
+
puts Pubilion::VERSION
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pubilion
|
|
4
|
+
# Configuration for Pubilion
|
|
5
|
+
class Config
|
|
6
|
+
class << self
|
|
7
|
+
attr_writer :project_id, :credentials, :topic, :subscription
|
|
8
|
+
|
|
9
|
+
# Configure Pubilion.
|
|
10
|
+
#
|
|
11
|
+
# @yield [config] Configuration object
|
|
12
|
+
# @example
|
|
13
|
+
# Pubilion::Config.configure do |config|
|
|
14
|
+
# config.project_id = "my-project"
|
|
15
|
+
# config.credentials = "/path/to/credentials.json"
|
|
16
|
+
# config.topic = "my-topic"
|
|
17
|
+
# config.subscription = "my-subscription"
|
|
18
|
+
# end
|
|
19
|
+
# @return [void]
|
|
20
|
+
def configure
|
|
21
|
+
yield self
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Google Cloud Project ID.
|
|
25
|
+
# If it is not set, it will try to get from environment variable +PUBSUB_PROJECT_ID+.
|
|
26
|
+
# If it still unsettled, implicitly set by Google Cloud SDK.
|
|
27
|
+
#
|
|
28
|
+
# @return [String, nil]
|
|
29
|
+
def project_id
|
|
30
|
+
@project_id ||= ENV.fetch("PUBSUB_PROJECT_ID", nil)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Credentials file path for access to Cloud Pub/Sub.
|
|
34
|
+
# If not set, it will try to get from environment variable +GOOGLE_APPLICATION_CREDENTIALS+.
|
|
35
|
+
# If not set and +GOOGLE_APPLICATION_CREDENTIALS+ is not set, it will implicitly set by Google Cloud SDK.
|
|
36
|
+
#
|
|
37
|
+
# @return [String, nil]
|
|
38
|
+
def credentials
|
|
39
|
+
@credentials ||= ENV.fetch("GOOGLE_APPLICATION_CREDENTIALS", nil)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Topic name of subscription.
|
|
43
|
+
# If not set, it will try to get from +PUBSUB_TOPIC+.
|
|
44
|
+
# This configuration is required for +Pubilion::Worker+.
|
|
45
|
+
#
|
|
46
|
+
# @return [String, nil]
|
|
47
|
+
def topic
|
|
48
|
+
@topic ||= ENV.fetch("PUBSUB_TOPIC", nil)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Subscription name.
|
|
52
|
+
# If not set, it will try to get from +PUBSUB_SUBSCRIPTION+.
|
|
53
|
+
# This configuration is required for +Pubilion::Worker+.
|
|
54
|
+
#
|
|
55
|
+
# @return [String, nil]
|
|
56
|
+
def subscription
|
|
57
|
+
@subscription ||= ENV.fetch("PUBSUB_SUBSCRIPTION", nil)
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pubilion
|
|
4
|
+
# Custom error classes for Pubilion
|
|
5
|
+
class Error < StandardError; end
|
|
6
|
+
|
|
7
|
+
# Raised when a feature is not supported by Pubilion
|
|
8
|
+
class NotSupported < Error; end
|
|
9
|
+
|
|
10
|
+
# Raised when a topic is not found in message publishing or subscribing
|
|
11
|
+
class TopicNotFound < Error; end
|
|
12
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ActiveJob
|
|
4
|
+
module QueueAdapters
|
|
5
|
+
# Cloud Pub/Sub Queue Adapter for ActiveJob
|
|
6
|
+
class PubSubAdapter
|
|
7
|
+
require "google/cloud/pubsub"
|
|
8
|
+
|
|
9
|
+
# Enqueue a job to Cloud Pub/Sub.
|
|
10
|
+
# @param job [ActiveJob::Base] the job to enqueue.
|
|
11
|
+
def enqueue(job)
|
|
12
|
+
topic = client.find_topic(job.queue_name)
|
|
13
|
+
raise Pubilion::TopicNotFound, "Topic not found: #{job.queue_name}" if topic.nil?
|
|
14
|
+
|
|
15
|
+
topic.publish(job.serialize.to_json)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# THIS METHOD IS NOT SUPPORTED.
|
|
19
|
+
# Always raise Pubilion::NotSupported.
|
|
20
|
+
# Because PubSub does not support job scheduling.
|
|
21
|
+
# If you need to schedule jobs, use a Cloud Tasks or other QueueAdapter.
|
|
22
|
+
def enqueue_at(_job, _timestamp)
|
|
23
|
+
raise Pubilion::NotSupported, <<~MSG
|
|
24
|
+
PubSub does not support job scheduling.
|
|
25
|
+
If you need to schedule jobs, use a Cloud Tasks or other QueueAdapter.
|
|
26
|
+
MSG
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
def client
|
|
32
|
+
@client ||= Google::Cloud::PubSub.new(
|
|
33
|
+
project_id: Pubilion::Config.project_id,
|
|
34
|
+
credentials: Pubilion::Config.credentials
|
|
35
|
+
)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rails/generators/base"
|
|
4
|
+
|
|
5
|
+
module Pubilion
|
|
6
|
+
module Generators
|
|
7
|
+
# Install generator for Pubilion.
|
|
8
|
+
class InstallGenerator < Rails::Generators::Base
|
|
9
|
+
source_root File.expand_path("templates", __dir__)
|
|
10
|
+
|
|
11
|
+
desc "Copy initializer file to your application."
|
|
12
|
+
|
|
13
|
+
def copy_initializer_file
|
|
14
|
+
template "pubilion.rb", "config/initializers/pubilion.rb"
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
Pubilion::Config.configure do |config|
|
|
4
|
+
# Set Google Cloud Project ID
|
|
5
|
+
# Can be specified by the environment variable `PUBSUB_PROJECT_ID`
|
|
6
|
+
# config.project_id = "your-project-id"
|
|
7
|
+
|
|
8
|
+
# Set Google Cloud Pub/Sub topic name to subscribe
|
|
9
|
+
# Can be specified by the environment variable `PUBSUB_TOPIC`
|
|
10
|
+
# config.topic = "your-topic-name"
|
|
11
|
+
|
|
12
|
+
# Set Google Cloud Pub/Sub subscription name
|
|
13
|
+
# Can be specified by the environment variable `PUBSUB_SUBSCRIPTION`
|
|
14
|
+
# config.subscription = "your-subscription-name"
|
|
15
|
+
|
|
16
|
+
# Set Google Cloud Pub/Sub credentials file path
|
|
17
|
+
# Can be specified by the environment variable `GOOGLE_APPLICATION_CREDENTIALS`
|
|
18
|
+
# config.credentials = "/path/to/credentials.json"
|
|
19
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pubilion
|
|
4
|
+
# Railtie for Pubilion.
|
|
5
|
+
#
|
|
6
|
+
# This class is used for loading rake tasks.
|
|
7
|
+
# When this gem is loaded in a Rails application, tasks in +lib/tasks+ are loaded automatically.
|
|
8
|
+
class Railtie < Rails::Railtie
|
|
9
|
+
railtie_name :pubilion
|
|
10
|
+
|
|
11
|
+
rake_tasks do
|
|
12
|
+
path = File.expand_path(__dir__)
|
|
13
|
+
Dir.glob("#{path}/tasks/**/*.rake").each { |f| load f }
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pubilion
|
|
4
|
+
class Worker
|
|
5
|
+
# Message handler for Pubilion worker
|
|
6
|
+
class MessageHandler
|
|
7
|
+
require "active_support/inflector"
|
|
8
|
+
|
|
9
|
+
# Handle incoming message
|
|
10
|
+
def on_message(message)
|
|
11
|
+
autoreload do
|
|
12
|
+
job = deserialize(message.data)
|
|
13
|
+
|
|
14
|
+
if job.nil?
|
|
15
|
+
message.nack!
|
|
16
|
+
return
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
job.perform_now
|
|
20
|
+
message.ack!
|
|
21
|
+
end
|
|
22
|
+
rescue StandardError
|
|
23
|
+
message.nack!
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Handle error
|
|
27
|
+
def on_error(error)
|
|
28
|
+
# TODO: Implement error handling
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def deserialize(payload)
|
|
34
|
+
job_data = JSON.parse(payload)
|
|
35
|
+
klass = job_data["job_class"]&.safe_constantize
|
|
36
|
+
return nil if klass.nil?
|
|
37
|
+
|
|
38
|
+
instance = klass.new
|
|
39
|
+
instance.deserialize(job_data)
|
|
40
|
+
|
|
41
|
+
instance
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def autoreload(&block)
|
|
45
|
+
if defined?(Rails)
|
|
46
|
+
Rails.application.reloader.wrap(&block)
|
|
47
|
+
else
|
|
48
|
+
block.call
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pubilion
|
|
4
|
+
class Worker
|
|
5
|
+
# Runner for Pubilion worker
|
|
6
|
+
class Runner
|
|
7
|
+
WAIT_INTERVAL = 1
|
|
8
|
+
EXIT_SIGNALS = %w[SIGTERM SIGINT].freeze
|
|
9
|
+
|
|
10
|
+
attr_reader :subscriber
|
|
11
|
+
|
|
12
|
+
def initialize(config)
|
|
13
|
+
@handler = Pubilion::Worker::MessageHandler.new
|
|
14
|
+
@subscriber = Pubilion::Worker::Subscriber.new(config, @handler)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Start worker and wait for exits.
|
|
18
|
+
def run
|
|
19
|
+
subscriber.run
|
|
20
|
+
|
|
21
|
+
wait_for_exits
|
|
22
|
+
ensure
|
|
23
|
+
subscriber.shutdown
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
private
|
|
27
|
+
|
|
28
|
+
def wait_for_exits(signals = EXIT_SIGNALS)
|
|
29
|
+
exit_requested = false
|
|
30
|
+
signals.each { |sig| Signal.trap(sig) { exit_requested = true } }
|
|
31
|
+
|
|
32
|
+
sleep WAIT_INTERVAL until exit_requested || subscriber.stopped?
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pubilion
|
|
4
|
+
class Worker
|
|
5
|
+
# Subscriber on Pubilion
|
|
6
|
+
class Subscriber
|
|
7
|
+
require "google/cloud/pubsub"
|
|
8
|
+
|
|
9
|
+
SHUTDOWN_WAIT = 20
|
|
10
|
+
|
|
11
|
+
attr_reader :config, :handler
|
|
12
|
+
|
|
13
|
+
def initialize(config, handler)
|
|
14
|
+
@config = config
|
|
15
|
+
@handler = handler
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Start streaming pull subscribe.
|
|
19
|
+
def run
|
|
20
|
+
@subscriber = subscription.listen do |message|
|
|
21
|
+
handler.on_message(message)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
@subscriber.on_error do |error|
|
|
25
|
+
handler.on_error(error)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
@subscriber.start
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Stop streaming pull subscribe.
|
|
32
|
+
def shutdown
|
|
33
|
+
@subscriber&.stop&.wait!(SHUTDOWN_WAIT)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Check if subscriber is alive.
|
|
37
|
+
def alive?
|
|
38
|
+
@subscriber.started? && !@subscriber.stopped?
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Check if subscriber is stopped.
|
|
42
|
+
def stopped?
|
|
43
|
+
@subscriber.stopped?
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
private
|
|
47
|
+
|
|
48
|
+
def client
|
|
49
|
+
@client ||= Google::Cloud::PubSub.new(
|
|
50
|
+
project_id: config.project_id,
|
|
51
|
+
credentials: config.credentials
|
|
52
|
+
)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def topic
|
|
56
|
+
@topic ||= client.find_topic(config.topic)
|
|
57
|
+
raise Pubilion::TopicNotFound, "Topic not found: #{config.topic}" if @topic.nil?
|
|
58
|
+
|
|
59
|
+
@topic
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def subscription
|
|
63
|
+
@subscription ||= topic.find_subscription(config.subscription)
|
|
64
|
+
raise Pubilion::SubscriptionNotFound, "Subscription not found: #{config.subscription}" if @subscription.nil?
|
|
65
|
+
|
|
66
|
+
@subscription
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "pubilion/worker/message_handler"
|
|
4
|
+
require "pubilion/worker/subscriber"
|
|
5
|
+
require "pubilion/worker/runner"
|
|
6
|
+
|
|
7
|
+
module Pubilion
|
|
8
|
+
# Subscribe worker on Pubilion
|
|
9
|
+
class Worker
|
|
10
|
+
class << self
|
|
11
|
+
def run
|
|
12
|
+
Pubilion::Worker::Runner.new(Pubilion::Config).run
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
data/lib/pubilion.rb
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "pubilion/version"
|
|
4
|
+
require "pubilion/errors"
|
|
5
|
+
require "pubilion/config"
|
|
6
|
+
|
|
7
|
+
require "pubilion/extensions"
|
|
8
|
+
require "pubilion/worker"
|
|
9
|
+
require "pubilion/cli"
|
|
10
|
+
|
|
11
|
+
require "pubilion/railties" if defined?(Rails::Railtie)
|
|
12
|
+
require "pubilion/generators/install_generator" if defined?(Rails::Generators)
|
|
13
|
+
|
|
14
|
+
# Ruby library for performing job with Google Cloud Pub/Sub.
|
|
15
|
+
module Pubilion; end
|
data/sig/pubilion.rbs
ADDED
metadata
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: pubilion
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- KL-Lru
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2024-11-10 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: activejob
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '6.1'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ">="
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '6.1'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: activesupport
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ">="
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '6.1'
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ">="
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '6.1'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: google-cloud-pubsub
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '2.0'
|
|
48
|
+
type: :runtime
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '2.0'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: thor
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '1.3'
|
|
62
|
+
type: :runtime
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '1.3'
|
|
69
|
+
description: ActiveJob Backend for Google Cloud Pub/Sub
|
|
70
|
+
email:
|
|
71
|
+
- 26233827+KL-Lru@users.noreply.github.com
|
|
72
|
+
executables:
|
|
73
|
+
- pubilion
|
|
74
|
+
extensions: []
|
|
75
|
+
extra_rdoc_files: []
|
|
76
|
+
files:
|
|
77
|
+
- ".rspec"
|
|
78
|
+
- ".rubocop.yml"
|
|
79
|
+
- ".ruby-version"
|
|
80
|
+
- ".solargraph.yml"
|
|
81
|
+
- ".yardopts"
|
|
82
|
+
- CHANGELOG.md
|
|
83
|
+
- LICENSE
|
|
84
|
+
- README.md
|
|
85
|
+
- Rakefile
|
|
86
|
+
- compose.yml
|
|
87
|
+
- exe/pubilion
|
|
88
|
+
- lib/pubilion.rb
|
|
89
|
+
- lib/pubilion/cli.rb
|
|
90
|
+
- lib/pubilion/cli/subscription.rb
|
|
91
|
+
- lib/pubilion/cli/topic.rb
|
|
92
|
+
- lib/pubilion/config.rb
|
|
93
|
+
- lib/pubilion/errors.rb
|
|
94
|
+
- lib/pubilion/extensions.rb
|
|
95
|
+
- lib/pubilion/extensions/active_job/queue_adapter.rb
|
|
96
|
+
- lib/pubilion/generators/install_generator.rb
|
|
97
|
+
- lib/pubilion/generators/templates/pubilion.rb
|
|
98
|
+
- lib/pubilion/railties.rb
|
|
99
|
+
- lib/pubilion/tasks/worker.rake
|
|
100
|
+
- lib/pubilion/version.rb
|
|
101
|
+
- lib/pubilion/worker.rb
|
|
102
|
+
- lib/pubilion/worker/message_handler.rb
|
|
103
|
+
- lib/pubilion/worker/runner.rb
|
|
104
|
+
- lib/pubilion/worker/subscriber.rb
|
|
105
|
+
- sig/pubilion.rbs
|
|
106
|
+
homepage: https://github.com/LruLab/pubilion
|
|
107
|
+
licenses:
|
|
108
|
+
- MIT
|
|
109
|
+
metadata:
|
|
110
|
+
homepage_uri: https://github.com/LruLab/pubilion
|
|
111
|
+
source_code_uri: https://github.com/LruLab/pubilion
|
|
112
|
+
documantation_uri: https://lrulab.github.io/pubilion
|
|
113
|
+
changelog_uri: https://lrulab.github.io/pubilion/file.CHANGELOG.html
|
|
114
|
+
rubygems_mfa_required: 'true'
|
|
115
|
+
post_install_message:
|
|
116
|
+
rdoc_options: []
|
|
117
|
+
require_paths:
|
|
118
|
+
- lib
|
|
119
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
120
|
+
requirements:
|
|
121
|
+
- - ">="
|
|
122
|
+
- !ruby/object:Gem::Version
|
|
123
|
+
version: 3.0.0
|
|
124
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
125
|
+
requirements:
|
|
126
|
+
- - ">="
|
|
127
|
+
- !ruby/object:Gem::Version
|
|
128
|
+
version: '0'
|
|
129
|
+
requirements: []
|
|
130
|
+
rubygems_version: 3.5.23
|
|
131
|
+
signing_key:
|
|
132
|
+
specification_version: 4
|
|
133
|
+
summary: ActiveJob Backend for Google Cloud Pub/Sub
|
|
134
|
+
test_files: []
|