hot_catch 0.1.4.1 → 0.1.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
  SHA1:
3
- metadata.gz: 9ff3f344fc7fd35c18d28fc83c8e54db9adf583f
4
- data.tar.gz: 84736194bdb94b432b2d5a436a55b064d23f552e
3
+ metadata.gz: db8e2f32d740a79b8e344f50fa3346a7c8614fee
4
+ data.tar.gz: d7a9c3d845ac1bd9109558b1fde39036215de76a
5
5
  SHA512:
6
- metadata.gz: b1feeb7eec1405f0d5ea1951f2ad586d597a408146f8a83e7d51bafc6ae7d8a4f049b57d0759ff0a1ec4fd07a11e184fb5d67c024ee6da60950a3f791988037a
7
- data.tar.gz: 1b908dbd7930cbd5d5adf094a943ae0a17e945adca1b6a359226ba7bb41db4236bcc57f88e755adc96a2f2a83cab320134f2b325de1248b19479acbdf9315127
6
+ metadata.gz: c49b96f10cb27a39805080ddc1a854ac475f1ddf27167d92ad6f81add2f94ea900dd77e371c037406121f4620074bef26b40be831dc778ce8c1124d86b0fb3f4
7
+ data.tar.gz: d9446c3a2b474e4d611209174774982998cb7245a85878b694d29204444f1780396df98b18bcec720c67b5f19a77222136fcb3180f1cfb483fcf343d69ba7104
@@ -34,7 +34,8 @@ module HotCatch
34
34
 
35
35
  def add_initialize_file_for_sender_logs
36
36
  create_file "config/initializers/hot_catch_sender_logs.rb" do
37
- "Rails.application.config.sender_logs = HotCatch::MakeHttpsRequest.new"
37
+ "Rails.application.config.sender_logs = HotCatch::MakeHttpsRequest.new\n" +
38
+ "Sidekiq::Cron::Job.create(name: 'NginxWorker - every 5min', cron: '*/5 * * * *', class: 'NginxWorker') # execute at every 5 minutes, ex: 12:05, 12:10, 12:15...etc\n"
38
39
  end
39
40
  end
40
41
 
@@ -1,7 +1,9 @@
1
1
  module HotCatch
2
2
  require 'hot_catch/hard_worker'
3
-
3
+ require 'hot_catch/nginx_worker'
4
4
  require 'hot_catch/custom_log_subscribers.rb'
5
+ require 'hot_catch/get_nginx_logs.rb'
6
+
5
7
  require 'uri'
6
8
  require 'net/https'
7
9
  require 'json'
@@ -22,8 +24,10 @@ module HotCatch
22
24
  # 5. Ошибки доступны в файле hot_catch_log_response_errors
23
25
  # ======================================================================
24
26
 
27
+ # Отвечает за отправку логов на главное приложение
25
28
  class MakeHttpsRequest
26
- def initialize(url = nil)
29
+ def initialize(url = nil, try_count = 0)
30
+ @try_count = try_count
27
31
  @url = url
28
32
  unless @url
29
33
  File.open("config/hot_catch_config.json"){ |file| @url = JSON.parse(file.read)["url"] }
@@ -32,33 +36,39 @@ module HotCatch
32
36
  end
33
37
 
34
38
  def rails_g_log(log_data, status)
35
- send_log(
36
- {main_hot_catch_log:
37
- {
38
- "log_data":log_data,
39
- "name_app":Rails.application.class.parent_name,
40
- "from_log":"Rails",
41
- "status":status
42
- }
39
+ body_log = {main_hot_catch_log:
40
+ {
41
+ "log_data":log_data,
42
+ "name_app":Rails.application.class.parent_name,
43
+ "from_log":"Rails",
44
+ "status":status
45
+ }
46
+ }
47
+ HardWorker.perform_async(body_log, @url, @try_count)
48
+ end
49
+
50
+ def nginx_g_log(log_data)
51
+ body_log = {main_hot_catch_log:
52
+ {
53
+ "log_data":log_data,
54
+ "name_app":Rails.application.class.parent_name,
55
+ "from_log":"Nginx"
43
56
  }
44
- )
57
+ }
58
+ HardWorker.perform_async(body_log, @url, @try_count)
45
59
  end
46
60
 
47
61
  def send_log(body_log)
48
- begin
49
- response = call(@url, body_log)
50
- # если на запрос пришёл ответ, то пришло уведомление об ошибке, которое логируется
51
- # Кроме того данное сообщение помещается в очередь и отправляется через какое-то время
52
- unless response.empty?
53
- str = "\n" + (?-*20) + "\n#{Time.now}\nlogs:\n#{body_log.to_s}\nresponse\n:#{response}\n" + (?-*20) + "\n"
54
- File.open("log/hot_catch_log_response_errors", 'a'){ |file| file.write str.encode('UTF-8', {
55
- :invalid => :replace,
56
- :undef => :replace,
57
- :replace => '?'
58
- }) }
59
- end
60
- rescue
61
- HardWorker.perform_in(10.seconds, body_log, @url)
62
+ response = call(@url, body_log)
63
+ # если на запрос пришёл ответ, то пришло уведомление об ошибке, которое логируется
64
+ # Кроме того данное сообщение помещается в очередь и отправляется через какое-то время
65
+ unless response.empty?
66
+ str = "\n" + (?-*20) + "\n#{Time.now}\nlogs:\n#{body_log.to_s}\nresponse\n:#{response}\n" + (?-*20) + "\n"
67
+ File.open("log/hot_catch_log_response_errors", 'a'){ |file| file.write str.encode('UTF-8', {
68
+ :invalid => :replace,
69
+ :undef => :replace,
70
+ :replace => '?'
71
+ }) }
62
72
  end
63
73
  end
64
74
 
@@ -0,0 +1,18 @@
1
+ require 'time'
2
+
3
+ def copy_nginx_logs(from_time = (Time.now() - 5 * 60), input_file = 'log/nginx.access.log')
4
+ str = ""
5
+ if File.open(input_file, 'r'){|file| str = file.readlines}
6
+ ind = nil
7
+
8
+ for i in 0 ... str.size
9
+ t = Time.strptime(str[0 - i - 1].match(/\d{2}\/\w+\/\d{4}:\d{2}:\d{2}:\d{2}/)[0], "%d/%b/%Y:%H:%M")
10
+ if from_time < t
11
+ ind = 0 - i - 1
12
+ else
13
+ break
14
+ end
15
+ end
16
+ ind ? str[ind..-1].join("") : nil
17
+ end
18
+ end
@@ -1,10 +1,26 @@
1
1
  require 'sidekiq'
2
+ # Отвечает за отправку логов на главное приложение
2
3
  class HardWorker
3
4
  include Sidekiq::Worker
4
- sidekiq_options :retry => false
5
- def perform(body_log, url)
6
- sender = HotCatch::MakeHttpsRequest.new(url)
7
- sender.send_log(body_log)
8
- sender = nil
5
+ def perform(body_log, url, try_count)
6
+ if try_count > 15
7
+ File.open("log/hot_catch_log_response_errors", 'a'){ |file| file.write body_log.encode('UTF-8', {
8
+ :invalid => :replace,
9
+ :undef => :replace,
10
+ :replace => '?'
11
+ }) }
12
+ else
13
+ sender = HotCatch::MakeHttpsRequest.new(url, try_count + 1)
14
+ sender.send_log(body_log)
15
+ sender = nil
16
+ end
17
+ end
18
+
19
+ def cancelled?
20
+ Sidekiq.redis {|c| c.exists("cancelled-#{jid}") }
21
+ end
22
+
23
+ def self.cancel!(jid)
24
+ Sidekiq.redis {|c| c.setex("cancelled-#{jid}", 86400, 1) }
9
25
  end
10
26
  end
@@ -0,0 +1,11 @@
1
+ require 'sidekiq'
2
+ # Отвечает за сбор и отправку логов Nginx
3
+ class NginxWorker
4
+ include Sidekiq::Worker
5
+ sidekiq_options :retry => false
6
+ def perform
7
+ logs = copy_nginx_logs()
8
+ sender = HotCatch::MakeHttpsRequest.new
9
+ sender.nginx_g_log(logs)
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module HotCatch
2
- VERSION = '0.1.4.1'
2
+ VERSION = '0.1.5'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hot_catch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4.1
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Davhot
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-17 00:00:00.000000000 Z
11
+ date: 2017-09-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sidekiq
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sidekiq-cron
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.6.3
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.6.3
27
41
  description: Description of Testgem.
28
42
  email:
29
43
  - david.home@mail.ru
@@ -38,8 +52,10 @@ files:
38
52
  - lib/generators/hot_catch/uninstall_generator.rb
39
53
  - lib/hot_catch.rb
40
54
  - lib/hot_catch/custom_log_subscribers.rb
55
+ - lib/hot_catch/get_nginx_logs.rb
41
56
  - lib/hot_catch/hard_worker.rb
42
57
  - lib/hot_catch/hot_catch_logger.rb
58
+ - lib/hot_catch/nginx_worker.rb
43
59
  - lib/hot_catch/version.rb
44
60
  - lib/tasks/hot_catch_tasks.rake
45
61
  homepage: https://myhomepage.com