active_job_log 2.0.0 → 2.1.0

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: dba3c2ac8737c2eb3ca017c3076303b46007ce3b1d410377437f613d8840edbb
4
- data.tar.gz: d1cb759e84634d425728e83d859e29ea9796386d1303340e9fc29060ec1fd81e
3
+ metadata.gz: f2a246cc523e487ca1aa839f1ecacffad9cd6459b8c66d7879e44808453d7e9b
4
+ data.tar.gz: d5042bfd7f44b07e521e978aa98fdda7d9ed5b3a27949cd85cf91a038dccb84b
5
5
  SHA512:
6
- metadata.gz: 32a50d61161aead9971cd4e688260f57491955c83cf0076d59990b5df98396b4a3ad4632a9894f3044bc4386ddd3dbae7f50c1a6c5222b4b902a2bbc1e9c1d48
7
- data.tar.gz: 7d32934735dd7218ea77237473e0e91872a6e129e32701b779089340cd5f019ffdec1864c9b7f7d590a52e89d0863401ebfd9e1fc7c059efac5fbf1ac1222c59
6
+ metadata.gz: add4582bcf93c72bee457d5d7c612ec9a801e187e34c6df9df657d7a0682b1a7fc17f28a4f9d3cdf1bdfa9a040c580f735c195fd03691c49fdf97a26338ab9b4
7
+ data.tar.gz: c6aef40bb57db3c35164531e1863f49ab3c93a6788b21dc7ed0aef3b6728de963e79180773cad69667440255fd3260deac89f935f13f4a7160f4d1d87b94d2ad
data/CHANGELOG.md CHANGED
@@ -2,6 +2,12 @@
2
2
  All notable changes to this project will be documented in this file.
3
3
  This project adheres to [Semantic Versioning](http://semver.org/).
4
4
 
5
+ ### v2.1.0
6
+
7
+ #### Added
8
+
9
+ * Avoid logging using `disable_job_logs` class method.
10
+
5
11
  ### v2.0.0
6
12
 
7
13
  #### Changed
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- active_job_log (2.0.0)
4
+ active_job_log (2.1.0)
5
5
  enumerize (>= 1.0)
6
6
  rails (>= 6.0)
7
7
 
data/README.md CHANGED
@@ -99,6 +99,20 @@ queued pending finished failed
99
99
 
100
100
  - `executions`: number of times this job has been executed (which increments on every retry, like after an exception.
101
101
 
102
+ ### Disable logging
103
+
104
+ If you want to avoid logging a specific job you have to add `disable_job_logs` config option on that job. For example:
105
+
106
+ ```ruby
107
+ class MyJob < ActiveJob::Base
108
+ disable_job_logs
109
+
110
+ def perform(param1, param2)
111
+ # ...
112
+ end
113
+ end
114
+ ```
115
+
102
116
  ### Important
103
117
 
104
118
  If your job calls the `rescue_from` method, you will need to call the `fail_job` method explicitly to log the job completion. For example:
@@ -3,6 +3,8 @@ module ActiveJobLog
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  included do
6
+ class_attribute :disabled_log
7
+
6
8
  before_enqueue { |job| enqueue_job(job) }
7
9
  before_perform { |job| execute_job(job) }
8
10
  after_perform { |job| finish_job(job) }
@@ -11,31 +13,48 @@ module ActiveJobLog
11
13
  fail_job(exception)
12
14
  raise exception
13
15
  end
16
+ end
14
17
 
15
- def enqueue_job(job)
16
- Job.update_job!(job.job_id, :queued, init_params(job))
18
+ class_methods do
19
+ def disable_job_logs
20
+ self.disabled_log = true
17
21
  end
22
+ end
18
23
 
19
- def execute_job(job)
20
- Job.update_job!(job.job_id, :pending, init_params(job))
21
- end
24
+ def enqueue_job(job)
25
+ update_job!(job.job_id, :queued, init_params(job))
26
+ end
22
27
 
23
- def finish_job(job)
24
- Job.update_job!(job.job_id, :finished)
25
- end
28
+ def execute_job(job)
29
+ update_job!(job.job_id, :pending, init_params(job))
30
+ end
26
31
 
27
- def fail_job(exception)
28
- Job.update_job!(job_id, :failed, error: exception.message, stack_trace: exception.backtrace)
29
- end
32
+ def finish_job(job)
33
+ update_job!(job.job_id, :finished)
34
+ end
30
35
 
31
- def init_params(job)
32
- {
33
- job_class: self.class.name,
34
- params: job.arguments,
35
- executions: job.try(:executions),
36
- queue_name: job.queue_name
37
- }
38
- end
36
+ def fail_job(exception)
37
+ update_job!(
38
+ job_id,
39
+ :failed,
40
+ error: exception.message,
41
+ stack_trace: exception.backtrace
42
+ )
43
+ end
44
+
45
+ def update_job!(job_id, status, params = {})
46
+ return if self.class.disabled_log
47
+
48
+ Job.update_job!(job_id, status, params)
49
+ end
50
+
51
+ def init_params(job)
52
+ {
53
+ job_class: self.class.name,
54
+ params: job.arguments,
55
+ executions: job.try(:executions),
56
+ queue_name: job.queue_name
57
+ }
39
58
  end
40
59
  end
41
60
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveJobLog
2
- VERSION = '2.0.0'
2
+ VERSION = '2.1.0'
3
3
  end
@@ -1,15 +1,9 @@
1
1
  require 'rails_helper'
2
2
 
3
3
  RSpec.describe ActiveJobLog::LogExt do
4
- def remove_job_class
5
- Object.send(:remove_const, :TestJob)
6
- rescue
7
- nil
8
- end
9
-
10
- def perform_now
11
- TestJob.perform_now(*job_params)
12
- rescue
4
+ def perform_now(job_class)
5
+ job_class.perform_now(*job_params)
6
+ rescue StandardError
13
7
  nil
14
8
  end
15
9
 
@@ -17,19 +11,21 @@ RSpec.describe ActiveJobLog::LogExt do
17
11
  %w{p1 p2}
18
12
  end
19
13
 
20
- before { remove_job_class }
21
-
22
14
  it { expect(ActiveJobLog::Job.count).to eq(0) }
23
15
 
24
16
  context "with successful job" do
25
- before do
26
- class TestJob < ActiveJob::Base
17
+ let(:job_class) do
18
+ klass = Class.new(ApplicationJob) do
27
19
  def perform(param1, param2)
28
20
  "success with #{param1} and #{param2}"
29
21
  end
30
22
  end
31
23
 
32
- perform_now
24
+ stub_const('TestJob', klass)
25
+ end
26
+
27
+ before do
28
+ perform_now(job_class)
33
29
  @job = ActiveJobLog::Job.last
34
30
  end
35
31
 
@@ -51,14 +47,18 @@ RSpec.describe ActiveJobLog::LogExt do
51
47
  end
52
48
 
53
49
  context "with failed job" do
54
- before do
55
- class TestJob < ActiveJob::Base
50
+ let(:job_class) do
51
+ klass = Class.new(ApplicationJob) do
56
52
  def perform(_param1, _param2)
57
53
  raise "error"
58
54
  end
59
55
  end
60
56
 
61
- perform_now
57
+ stub_const('TestJob', klass)
58
+ end
59
+
60
+ before do
61
+ perform_now(job_class)
62
62
  @job = ActiveJobLog::Job.last
63
63
  end
64
64
 
@@ -78,4 +78,37 @@ RSpec.describe ActiveJobLog::LogExt do
78
78
  it { expect(@job.total_duration).not_to be_nil }
79
79
  it { expect(@job.executions).to eq(1) }
80
80
  end
81
+
82
+ context "with disabled jobs" do
83
+ let(:disabled_job_class) do
84
+ klass = Class.new(ApplicationJob) do
85
+ disable_job_logs
86
+
87
+ def perform(_param1, _param2)
88
+ # ...
89
+ end
90
+ end
91
+
92
+ stub_const('DisabledTestJob', klass)
93
+ end
94
+
95
+ let(:enabled_job_class) do
96
+ klass = Class.new(ApplicationJob) do
97
+ def perform(_param1, _param2)
98
+ # ...
99
+ end
100
+ end
101
+
102
+ stub_const('EnabledTestJob', klass)
103
+ end
104
+
105
+ before do
106
+ perform_now(disabled_job_class)
107
+ perform_now(enabled_job_class)
108
+ @job = ActiveJobLog::Job.last
109
+ end
110
+
111
+ it { expect(ActiveJobLog::Job.count).to eq(1) }
112
+ it { expect(@job.job_class).to eq('EnabledTestJob') }
113
+ end
81
114
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_job_log
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Platanus
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2021-06-22 00:00:00.000000000 Z
12
+ date: 2021-07-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails