Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[review] Add ActiveJob extension #72

Merged
merged 4 commits into from
Jul 9, 2024
Merged
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
27 changes: 11 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Add this line to your application's Gemfile:

```ruby
gem 'jobmon', github: 'SonicGarden/jobmon_ruby'
gem 'jobmon', github: 'SonicGarden/jobmon_ruby', branch: 'main'
```

And then execute:
Expand Down Expand Up @@ -41,25 +41,20 @@ jobmon --estimate-time 600 cron:sample_task
jobmon --estimate-time 600 --name sample --cmd "bin/rails runner scripts/sample.rb"
```

In `config/schedule.rb`:
### ActiveJobExtensionの使い方

```ruby
set :path, File.realpath('../', __dir__)
set :output, "#{path}/log/batch.log"
set :estimate_time, 180

job_type :jobmon, 'cd :path && bundle exec jobmon --estimate-time :estimate_time :task :output'
JobmonをActiveJobと組み合わせて使用することで、Railsアプリケーション内のジョブの実行を簡単に監視することができます。以下の手順に従って設定してください。

every 10.minutes do
jobmon 'cron:hoge_task'
```ruby
class SampleJob < ApplicationJob
include Jobmon::ActiveJobExtension

# rake 'jobmon:delayed_job_queue_monitor'
# rake 'jobmon:good_job_queue_monitor'
# rake 'jobmon:sidekiq_queue_monitor'
end
# Optional
jobmon_with(name: 'jobmonに表示される定期タスク名', estimate_time: 1.minute)

every 1.day, at: '00:00' do
jobmon 'cron:heavy_task', estimate_time: 600
def perform(*args)
# 実行したい処理
end
end
```

Expand Down
1 change: 1 addition & 0 deletions lib/jobmon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'jobmon/client'
require 'jobmon/engine'
require 'jobmon/configuration'
require 'jobmon/active_job_extension'

module Jobmon
class << self
Expand Down
24 changes: 24 additions & 0 deletions lib/jobmon/active_job_extension.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Jobmon
module ActiveJobExtension
extend ActiveSupport::Concern

included do
class_attribute :jobmon_config, instance_accessor: false, default: {}

around_perform do |job, block|
job_name = job.class.jobmon_config.fetch(:name) { job.class.name }
estimate_time = job.class.jobmon_config.fetch(:estimate_time) { Jobmon.configuration.estimate_time }

Jobmon::Client.new.job_monitor(job_name, estimate_time) do
block.call
end
end
end

class_methods do
def jobmon_with(config)
self.jobmon_config = config
end
end
end
end
2 changes: 1 addition & 1 deletion lib/jobmon/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Jobmon
VERSION = '1.9.0'
VERSION = '1.10.0'
end