Skip to content

Commit

Permalink
Merge pull request #72 from SonicGarden/dev
Browse files Browse the repository at this point in the history
[review] Add ActiveJob extension
  • Loading branch information
aki77 authored Jul 9, 2024
2 parents f99c710 + 92d46a8 commit be8ec12
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
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

0 comments on commit be8ec12

Please sign in to comment.