tanga_que_extensions 0.0.2 → 0.0.3

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
  SHA1:
3
- metadata.gz: a8d93fe2d0719cd9a52becccbdf56561aea37cfa
4
- data.tar.gz: 5286dfbf1d780a05a174692d82d5586a4f65cadd
3
+ metadata.gz: aa8f04e9cac8d8598a161af08d8d811c8a28a494
4
+ data.tar.gz: cf99fbfbe31e7755c51288d54bfb03fdec426e4d
5
5
  SHA512:
6
- metadata.gz: 9bc3be9281229b988ff949ec5c4153b1fdc0a8f79b0464ec840e5f9dd4cbcec3d2c32c8847afe9e86e1e68f7b37c6a4c9de9f9f4e9801423ab4db7c444b89ec6
7
- data.tar.gz: 9e151c7ccfabe55c47edbfc7f1a872833fa9c3f32cfaa154b3e26e99e00976ab1a1c66466bfed8c5437829467426b96ea9cf4f9d2f74dfa6a370d31f8d4184b4
6
+ metadata.gz: 66a1aa40c78a817a290cbb25067c48213ec80eaceb0ccb32121bf5deabb41e737f8bf20350c696bc8c7361b1c981f04b075aaa4bff608580c9969980f47188ea
7
+ data.tar.gz: 38e544c730883315262454b6d92cf31026bf9abda02cf0a7e7a3ec10ae728f5d84a5610ea46c61f161aff443253ea37afa2ffb6ee6471a2cae51a4bc0766931c
@@ -0,0 +1,30 @@
1
+ module Que
2
+ module BackgroundJobLogger
3
+ def _run(*)
4
+ start_time = Time.current
5
+ start_memory = Que::MemoryInfo.rss
6
+
7
+ super
8
+
9
+ end_time = Time.current
10
+ total_run_time = end_time - start_time
11
+ current_memory = Que::MemoryInfo.rss
12
+ memory_taken = current_memory - start_memory
13
+
14
+ TangaServices.logger.info(
15
+ service: 'que_jobs',
16
+ status: 'job_finished',
17
+ end_time: end_time,
18
+ total_run_time: total_run_time,
19
+ job: self.class.name,
20
+ attrs: attrs,
21
+ current_memory: current_memory,
22
+ change_in_memory: memory_taken
23
+ )
24
+ end
25
+ end
26
+
27
+ class Job
28
+ prepend Que::BackgroundJobLogger
29
+ end
30
+ end
data/lib/job.rb ADDED
@@ -0,0 +1,6 @@
1
+ class Que::Job
2
+ private
3
+ def self.class_for(job)
4
+ job.constantize
5
+ end
6
+ end
@@ -0,0 +1,9 @@
1
+ module Que::MemoryInfo
2
+ KERNEL_PAGE_SIZE = 4096
3
+ STATM_PATH = "/proc/#{Process.pid}/statm"
4
+ STATM_FOUND = File.exist?(STATM_PATH)
5
+
6
+ def self.rss
7
+ STATM_FOUND ? (File.read(STATM_PATH).split(' ')[1].to_i * KERNEL_PAGE_SIZE) / (1024 * 1024): 0
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module Que::NotifyDevOnFailures
2
+ def notify_dev_if_needed(e)
3
+ Airbrake.notify(e)
4
+ end
5
+ end
@@ -0,0 +1,38 @@
1
+ class Que::PeriodicJob < Que::Job
2
+ INTERVAL = 60
3
+ DELAY = 0
4
+ attr_reader :start_at, :end_at, :run_again_at, :time_range
5
+ def _run
6
+ args = attrs[:args].first || {}
7
+
8
+ start_at = args.delete('start_at')
9
+ end_at = args.delete('end_at')
10
+
11
+ if start_at.present? && end_at.present?
12
+ @start_at = Time.parse(start_at)
13
+ @end_at = Time.parse(end_at)
14
+ @run_again_at = @end_at + self.class::INTERVAL
15
+ else
16
+ @start_at = Time.current
17
+ @end_at = @start_at + self.class::INTERVAL
18
+ @run_again_at = @end_at
19
+ end
20
+
21
+ @time_range = @start_at...@end_at
22
+
23
+ super
24
+
25
+ args['start_at'] = @end_at
26
+ args['end_at'] = @run_again_at
27
+
28
+ Que::Migrations.transaction do
29
+ self.class.enqueue(args, run_at: @run_again_at + self.class::DELAY)
30
+ destroy
31
+ end
32
+ end
33
+
34
+ def test_job
35
+ @time_range = attrs.delete(:time_range)
36
+ run(attrs)
37
+ end
38
+ end
@@ -0,0 +1,36 @@
1
+ module Que::RecordJobStatus
2
+ def job_id
3
+ @job_id || 0
4
+ end
5
+
6
+ def run(args={})
7
+ @job_id = self.attrs['job_id']
8
+ record_job_started
9
+ super if defined?(super)
10
+ record_job_finished
11
+ rescue => e
12
+ record_job_failed(e)
13
+ notify_dev_if_needed(e)
14
+ end
15
+
16
+ def notify_dev_if_needed(e)
17
+ super if defined?(super)
18
+ end
19
+
20
+ def record_job_failed(e)
21
+ TangaServices.logger.error(service: 'que_jobs', status: 'error', error: e.to_json)
22
+ job_scope.update_all(status: 'failed', error: { message: e.message, args: e.as_json })
23
+ end
24
+
25
+ def record_job_started
26
+ QueJobStatus.create!(job_id: job_id, attrs: attrs, status: 'executing')
27
+ end
28
+
29
+ def record_job_finished
30
+ job_scope.update_all(finished_at: Time.current, status: 'finished')
31
+ end
32
+
33
+ def job_scope
34
+ QueJobStatus.where(job_id: job_id)
35
+ end
36
+ end
@@ -0,0 +1,7 @@
1
+ require 'que'
2
+ require_relative 'periodic_job'
3
+ require_relative 'memory_info'
4
+ require_relative 'background_job_logger'
5
+ require_relative 'record_job_status'
6
+ require_relative 'job'
7
+ require_relative 'notify_on_failures'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tanga_que_extensions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter van Wesep
@@ -31,6 +31,13 @@ executables: []
31
31
  extensions: []
32
32
  extra_rdoc_files: []
33
33
  files:
34
+ - lib/background_job_logger.rb
35
+ - lib/job.rb
36
+ - lib/memory_info.rb
37
+ - lib/notify_on_failures.rb
38
+ - lib/periodic_job.rb
39
+ - lib/record_job_status.rb
40
+ - lib/tanga_que_extensions.rb
34
41
  - test/test.rb
35
42
  homepage: https://www.tanga.com
36
43
  licenses: []