rails-cloud-tasks 0.0.2 โ†’ 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 924a936920255b90f05426a1a02d8fcb7c0b688f05ecf23f47d5574cfd830100
4
- data.tar.gz: dac09c6a8c32a41ce9b8849b57892cac630f54b9592faf88400d248ac32d4661
3
+ metadata.gz: 563b23e52524d8a824730b37b9dfe35daa1fcba85bed52050c8581c677dc0ff7
4
+ data.tar.gz: e0eb08e7c1d7bfb0271c3154b5e9673e2955e3e30bdbece1e4758faa22544785
5
5
  SHA512:
6
- metadata.gz: b4bb22ec37af949357f944d2e5dc5f80c955fd65426aadc35f1ac4d7bc53cfdc8d5703813553f824e2024c137087a3d891f16d2a7859eb6a26fef771da7a894f
7
- data.tar.gz: 4ca848172ca6473f08348f0b5ff6d9f9ddad8ff2a9b95ab9b2f8e77d933c5d6a1ebd4c29b1eac6afa43ecd3e665913fb9864d22bc8b451a1ee946b1279be6cb4
6
+ metadata.gz: 2aa519587a1662e8dae394e8a100b043a64aee09fdb4c2d679a8d5731fed4da010600780a65a6aff0cc29ff36f6e7373fda85073cb9a82fdedbca9f0bcf6ca89
7
+ data.tar.gz: 4b188e8d270ba58d06030924e7119c544feddf7ccb8813b1ccafdb79dd48975f536a5be18ce995782cca5244cfcf75cc873336d093980120792e14f24378489d
@@ -23,7 +23,7 @@ jobs:
23
23
  run: gem install bundler
24
24
 
25
25
  - name: Cache dependencies
26
- uses: actions/cache@v1
26
+ uses: actions/cache@v2
27
27
  with:
28
28
  path: vendor/bundle
29
29
  key: ${{ runner.os }}-gem-${{ hashFiles('**/rails-cloud-tasks.gemspec') }}
@@ -48,7 +48,7 @@ jobs:
48
48
  run: gem install bundler
49
49
 
50
50
  - name: Cache dependencies
51
- uses: actions/cache@v1
51
+ uses: actions/cache@v2
52
52
  with:
53
53
  path: vendor/bundle
54
54
  key: ${{ runner.os }}-gem-${{ hashFiles('**/rails-cloud-tasks.gemspec') }}
@@ -73,7 +73,7 @@ jobs:
73
73
  run: gem install bundler
74
74
 
75
75
  - name: Cache dependencies
76
- uses: actions/cache@v1
76
+ uses: actions/cache@v2
77
77
  with:
78
78
  path: vendor/bundle
79
79
  key: ${{ runner.os }}-gem-${{ hashFiles('**/rails-cloud-tasks.gemspec') }}
@@ -12,6 +12,7 @@ jobs:
12
12
  pre_release: ${{ steps.versioning.outputs.pre_release }}
13
13
  upgraded: ${{ steps.versioning.outputs.upgraded }}
14
14
  package_version: ${{ steps.versioning.outputs.package_version }}
15
+ release_message: ${{ steps.versioning.outputs.release_message }}
15
16
  steps:
16
17
  - uses: actions/checkout@v2
17
18
 
@@ -24,13 +25,15 @@ jobs:
24
25
  run: gem install bundler
25
26
 
26
27
  - name: Cache dependencies
27
- uses: actions/cache@v1
28
+ uses: actions/cache@v2
28
29
  with:
29
30
  path: vendor/bundle
30
31
  key: ${{ runner.os }}-gem-${{ hashFiles('**/rails-cloud-tasks.gemspec') }}-2.7
31
32
 
32
33
  - name: Install dependencies
33
- run: bundle install
34
+ run: |
35
+ bundle config set without 'development test'
36
+ bundle install
34
37
 
35
38
  - name: Fetching Tags
36
39
  run: git fetch -t
@@ -45,6 +48,8 @@ jobs:
45
48
  echo "::set-output name=upgraded::"$upgraded
46
49
  pre_release=$([[ $package_version =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] && echo "false" || echo "true")
47
50
  echo "::set-output name=pre_release::"$pre_release
51
+ release_message=$(git log HEAD ^$(git describe --tags `git rev-list --tags --max-count=1`) --pretty=format:'* %C(green)%h%C(reset) %s')
52
+ echo "::set-output name=release_message::"$release_message
48
53
 
49
54
  release:
50
55
  runs-on: ubuntu-latest
@@ -62,13 +67,15 @@ jobs:
62
67
  run: gem install bundler
63
68
 
64
69
  - name: Cache dependencies
65
- uses: actions/cache@v1
70
+ uses: actions/cache@v2
66
71
  with:
67
72
  path: vendor/bundle
68
73
  key: ${{ runner.os }}-gem-${{ hashFiles('**/rails-cloud-tasks.gemspec') }}-2.7
69
74
 
70
75
  - name: Install dependencies
71
- run: bundle install
76
+ run: |
77
+ bundle config set without 'development test'
78
+ bundle install
72
79
 
73
80
  - name: Create Release
74
81
  uses: actions/create-release@v1
@@ -78,8 +85,7 @@ jobs:
78
85
  tag_name: ${{ needs.checks.outputs.package_version }}
79
86
  release_name: Release ${{ needs.checks.outputs.package_version }}
80
87
  body: |
81
- Auto-released by bot.
82
- See commit changes.
88
+ ${{ needs.checks.outputs.release_message }}
83
89
  draft: false
84
90
  prerelease: ${{ needs.checks.outputs.pre_release }}
85
91
 
data/README.md CHANGED
@@ -64,6 +64,14 @@ Check out the available configs and its usage description:
64
64
  | tasks_path | The path to run tasks | ๐„‚ | | '/tasks' |
65
65
 
66
66
 
67
+ - Configure ActiveJob queue_adapter
68
+
69
+ ```ruby
70
+ # ./config/application.rb
71
+
72
+ config.active_job.queue_adapter = RailsCloudTasks.queue_adapter
73
+ ```
74
+
67
75
  - Add a Job class:
68
76
  ```ruby
69
77
  # ./app/jobs/application_job.rb
@@ -97,23 +105,29 @@ We have support to Google Cloud Schedule. It's based on Cloud tasks, the jobs ar
97
105
  Check out the follow sample of config file:
98
106
  ```yaml
99
107
  # config/scheduler.yml
100
- - name: Users::SyncJob
108
+ - name: UsersSyncJob
101
109
  schedule: 0 8 * * *
102
110
  description: Sync user data
103
111
  time_zone: "America/Los_Angeles"
112
+ class_name: Users::SyncJob
104
113
  args:
105
- arg1: 100
106
- arg2: 200
114
+ - this_first: argument
115
+ is_a: hash
116
+ - - this second argument
117
+ - is an array
118
+ - this third argument is a string
107
119
  ```
108
120
 
109
- | attribute | description | required |
110
- |------------- |---------------------------------------------------------------- |---------- |
111
- | name | The Job class namespace | โœ“ |
112
- | schedule | The frequency to run your job. It should be a unix-cron format | โœ“ |
113
- | description | What this job does | โœ“ |
114
- | time_zone | Choose which one timezone your job must run | โœ“ |
115
- | args | What are the job's arguments | โœ“ |
121
+ | attribute | description | required |
122
+ |------------- |---------------------------------------------------------------------------------- |---------- |
123
+ | name | Any descriptive name, following [Tasks naming restrictions][1] | โœ“ |
124
+ | schedule | The frequency to run your job. It should be a unix-cron format | โœ“ |
125
+ | description | What this job does | โœ“ |
126
+ | time_zone | Choose which one timezone your job must run | โœ“ |
127
+ | args | Arguments to the job execution. Important: if present, this must be an array of items. Check out the example above | ๐„‚ |
128
+ | class_name | The Job class name (including namespace) | โœ“ |
116
129
 
130
+ [1]: https://cloud.google.com/tasks/docs/reference/rpc/google.cloud.tasks.v2
117
131
 
118
132
 
119
133
 
@@ -12,6 +12,7 @@ module RailsCloudTasks
12
12
  autoload :AppEngine
13
13
  autoload :Configuration
14
14
  autoload :Version
15
+ autoload :Instrumentation
15
16
 
16
17
  module Rack
17
18
  extend ActiveSupport::Autoload
@@ -39,4 +40,21 @@ module RailsCloudTasks
39
40
  end
40
41
  end
41
42
  end
43
+
44
+ @queue_adapter = nil
45
+
46
+ def queue_adapter
47
+ @@queue_adapter
48
+ end
49
+
50
+ def self.queue_adapter
51
+ @queue_adapter ||= Adapter.new
52
+ rescue StandardError => e
53
+ raise e unless Rails.env.development?
54
+
55
+ logger.warn('unable to setup adapter, falling back to :inline')
56
+ logger.warn(e)
57
+
58
+ :inline
59
+ end
42
60
  end
@@ -0,0 +1,19 @@
1
+ module RailsCloudTasks
2
+ module Instrumentation
3
+ extend ActiveSupport::Autoload
4
+
5
+ autoload :Default
6
+ autoload :Factory
7
+ autoload :NewRelic
8
+
9
+ module_function
10
+
11
+ def agent
12
+ @agent ||= RailsCloudTasks::Instrumentation::Factory.agent_class.new
13
+ end
14
+
15
+ def transaction_name!(*opts)
16
+ agent.transaction_name!(opts)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,7 @@
1
+ module RailsCloudTasks
2
+ module Instrumentation
3
+ class Default
4
+ def transaction_name!(*opts); end
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ module RailsCloudTasks
2
+ module Instrumentation
3
+ class Factory
4
+ def self.agent_class
5
+ return NewRelic if defined?(::NewRelic)
6
+
7
+ Default
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ module RailsCloudTasks
2
+ module Instrumentation
3
+ class NewRelic
4
+ def transaction_name!(opts)
5
+ agent.set_transaction_name(*opts)
6
+ end
7
+
8
+ def agent
9
+ @agent ||= ::NewRelic::Agent
10
+ end
11
+ end
12
+ end
13
+ end
@@ -8,9 +8,14 @@ module RailsCloudTasks
8
8
  def call(env)
9
9
  job_class = extract_job_class(env)
10
10
 
11
+ RailsCloudTasks::Instrumentation.transaction_name!(
12
+ "RailsCloudTasks/#{job_class}/perform_now"
13
+ )
14
+
11
15
  request = ::Rack::Request.new(env)
12
16
  job_args = extract_args(request)
13
- job_class.perform_now(job_args)
17
+
18
+ job_class.perform_now(*job_args)
14
19
 
15
20
  response(200, {})
16
21
  rescue Rack::InvalidPayloadError => e
@@ -28,7 +33,7 @@ module RailsCloudTasks
28
33
 
29
34
  def extract_args(request)
30
35
  body = request.body.read
31
- JSON.parse(body)
36
+ JSON.parse(body) || []
32
37
  rescue JSON::ParserError, KeyError
33
38
  raise Rack::InvalidPayloadError
34
39
  end
@@ -9,8 +9,11 @@ module RailsCloudTasks
9
9
  request = ::Rack::Request.new(env)
10
10
  job = extract_job(request)
11
11
 
12
- ActiveJob::Base.execute(job)
12
+ RailsCloudTasks::Instrumentation.transaction_name!(
13
+ "RailsCloudTasks/#{job['job_class']}/perform_now"
14
+ )
13
15
 
16
+ ActiveJob::Base.execute(job)
14
17
  response(200, {})
15
18
  rescue Rack::InvalidPayloadError => e
16
19
  response(400, { error: e.cause.message })
@@ -4,28 +4,28 @@ module RailsCloudTasks
4
4
  :scheduler_file_path, :scheduler_prefix_name,
5
5
  :service_account_email, to: 'RailsCloudTasks.config'
6
6
 
7
- attr_reader :client, :credentials
7
+ attr_reader :client, :credentials, :logger
8
8
 
9
9
  def initialize(
10
10
  client: Google::Cloud::Scheduler.cloud_scheduler,
11
- credentials: RailsCloudTasks::Credentials.new
11
+ credentials: RailsCloudTasks::Credentials.new,
12
+ logger: RailsCloudTasks.logger
12
13
  )
13
14
  client.configure do |config|
14
15
  config.credentials = credentials.generate(service_account_email)
15
16
  end
16
17
  @client = client
18
+ @logger = logger
17
19
  end
18
20
 
19
21
  # Create & Update scheduler job on Google Cloud
20
22
  # TODO: Support to delete scheduled jobs
21
23
  def upsert
24
+ result = { success: [], failure: [] }
22
25
  scheduler_jobs.each do |job|
23
- begin
24
- client.create_job parent: location_path, job: job
25
- rescue Google::Cloud::AlreadyExistsError
26
- client.update_job job: job
27
- end
26
+ upsert_job(job) ? (result[:success] << job[:name]) : (result[:failure] << job[:name])
28
27
  end
28
+ log_output(result)
29
29
  end
30
30
 
31
31
  protected
@@ -45,7 +45,7 @@ module RailsCloudTasks
45
45
  description: job[:description],
46
46
  time_zone: job[:time_zone],
47
47
  http_target: {
48
- uri: "#{host}#{tasks_path}/#{job[:name]}",
48
+ uri: "#{host}#{tasks_path}/#{job[:class_name]}",
49
49
  http_method: 'POST',
50
50
  body: job[:args].to_json
51
51
  }.merge(auth)
@@ -58,5 +58,38 @@ module RailsCloudTasks
58
58
  rescue Errno::ENOENT
59
59
  []
60
60
  end
61
+
62
+ def log_output(result)
63
+ parse_task_name = ->(task) { task.split("#{scheduler_prefix_name}--")[1] }
64
+ success = result[:success].map(&parse_task_name)
65
+ failure = result[:failure].map(&parse_task_name)
66
+
67
+ if success.count.positive?
68
+ log("Successfuly scheduled #{success.count} tasks", '- [โœ“] ',
69
+ success)
70
+ end
71
+
72
+ log("Failed to schedule #{failure.count} tasks", '- [๐„‚] ', failure) if failure.count.positive?
73
+ end
74
+
75
+ def log(desc, prefix, tasks)
76
+ logger.info(desc)
77
+ logger.info(prefix + tasks.join("\n #{prefix}"))
78
+ end
79
+
80
+ private
81
+
82
+ def upsert_job(job)
83
+ success = true
84
+ begin
85
+ client.create_job parent: location_path, job: job
86
+ rescue Google::Cloud::AlreadyExistsError
87
+ client.update_job job: job
88
+ rescue StandardError => e
89
+ logger.error(e)
90
+ success = false
91
+ end
92
+ success
93
+ end
61
94
  end
62
95
  end
@@ -1,3 +1,3 @@
1
1
  module RailsCloudTasks
2
- VERSION = '0.0.2'.freeze
2
+ VERSION = '0.0.5'.freeze
3
3
  end
@@ -27,7 +27,7 @@ Gem::Specification.new do |spec|
27
27
  spec.add_dependency 'google-apis-iamcredentials_v1'
28
28
  spec.add_dependency 'google-cloud-scheduler', '>= 2'
29
29
  spec.add_dependency 'google-cloud-tasks', '>= 2'
30
- spec.add_development_dependency 'rails', '>= 4'
30
+ spec.add_dependency 'rails', '>= 4'
31
31
 
32
32
  spec.add_development_dependency 'bundler'
33
33
  spec.add_development_dependency 'pry'
data/renovate.json ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "extends": [
3
+ "config:base"
4
+ ]
5
+ }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-cloud-tasks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Guilherme Araรบjo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-02-11 00:00:00.000000000 Z
11
+ date: 2021-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -87,7 +87,7 @@ dependencies:
87
87
  - - ">="
88
88
  - !ruby/object:Gem::Version
89
89
  version: '4'
90
- type: :development
90
+ type: :runtime
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
@@ -284,6 +284,10 @@ files:
284
284
  - lib/rails_cloud_tasks/app_engine.rb
285
285
  - lib/rails_cloud_tasks/configuration.rb
286
286
  - lib/rails_cloud_tasks/credentials.rb
287
+ - lib/rails_cloud_tasks/instrumentation.rb
288
+ - lib/rails_cloud_tasks/instrumentation/default.rb
289
+ - lib/rails_cloud_tasks/instrumentation/factory.rb
290
+ - lib/rails_cloud_tasks/instrumentation/new_relic.rb
287
291
  - lib/rails_cloud_tasks/rack/errors.rb
288
292
  - lib/rails_cloud_tasks/rack/jobs.rb
289
293
  - lib/rails_cloud_tasks/rack/tasks.rb
@@ -292,6 +296,7 @@ files:
292
296
  - lib/railtie.rb
293
297
  - lib/tasks/scheduler.rake
294
298
  - rails-cloud-tasks.gemspec
299
+ - renovate.json
295
300
  homepage: http://github.com/flamingo-run/rails-cloud-tasks
296
301
  licenses:
297
302
  - Apache License 2.0