rest-ftp-daemon 0.214.0 → 0.220.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,7 @@
1
1
  module RestFtpDaemon
2
2
  class WorkerPool
3
+ include LoggerHelper
4
+ attr_reader :logger
3
5
 
4
6
  attr_reader :wid
5
7
 
@@ -7,28 +9,24 @@ module RestFtpDaemon
7
9
  include ::NewRelic::Agent::Instrumentation::ControllerInstrumentation
8
10
  end
9
11
 
10
- def initialize number_threads
12
+ def initialize
11
13
  # Logger
12
14
  @logger = RestFtpDaemon::LoggerPool.instance.get :workers
13
-
14
- # Check parameters
15
- raise "at least one worker is needed to continue (#{number_threads} is less than one)" if number_threads < 1
15
+ # log_info "WorkerPool: log_info"
16
+ # log_error "WorkerPool: log_error"
17
+ # log_error "WorkerPool: log_error + lines", ['line1', 'line2']
16
18
 
17
19
  # Prepare status hash and vars
18
20
  @statuses = {}
19
21
  @workers = {}
20
22
  @conchita = nil
21
23
  @mutex = Mutex.new
22
- @counter = 0
23
- @timeout = (Settings.transfer.timeout rescue nil) || DEFAULT_WORKER_TIMEOUT
24
24
 
25
- # Create worker threads
26
- info "WorkerPool creating worker threads [#{number_threads}] timeout [#{@timeout}]s"
27
- create_worker_threads number_threads
25
+ # Identifiers generator
26
+ @last_id = 0
28
27
 
29
- # Create conchita thread
30
- info "WorkerPool creating conchita thread"
31
- create_conchita_thread
28
+ # Create worker threads
29
+ create_threads
32
30
  end
33
31
 
34
32
  def worker_variables
@@ -47,119 +45,65 @@ module RestFtpDaemon
47
45
 
48
46
  private
49
47
 
50
- def create_worker_threads n
51
- # FIXME counter instead of upto ?
52
- n.times do
53
- # Increment counter
54
- @mutex.synchronize do
55
- @counter +=1
56
- end
48
+ def generate_id
49
+ @mutex.synchronize do
50
+ @last_id += 1
51
+ end
52
+ "w#{@last_id}"
53
+ end
54
+
55
+ def create_threads
56
+ # Read configuration
57
+ number_threads = (Settings.workers || APP_WORKERS)
58
+
59
+ if number_threads < 1
60
+ log_error "create_threads: one worker is the minimum possible number (#{number_threads} configured)"
61
+ raise InvalidWorkerNumber
62
+ end
57
63
 
58
- # Create a dedicated thread for this worker
59
- wid = "w#{@counter}"
64
+ # Create workers
65
+ log_info "WorkerPool creating #{number_threads}x JobWorker, 1x ConchitaWorker"
66
+
67
+ # Start worker threads
68
+ number_threads.times do
69
+ wid = generate_id
60
70
  @workers[wid] = create_worker_thread wid
61
71
  end
72
+
73
+ # Start conchita thread
74
+ @conchita = create_conchita_thread
75
+
76
+ rescue Exception => ex
77
+ log_error "UNHDNALED EXCEPTION: #{ex.message}", ex.backtrace
78
+
62
79
  end
63
80
 
64
81
  def create_worker_thread wid
65
82
  Thread.new wid do
66
- # Set thread context
67
- Thread.current.thread_variable_set :wid, wid
68
- Thread.current.thread_variable_set :started_at, Time.now
69
- worker_status :starting
70
-
71
- # Start working
72
- loop do
73
- begin
74
- work
75
- rescue Exception => ex
76
- puts "WORKER UNEXPECTED CRASH: #{ex.message}", lines: ex.backtrace
77
- sleep 1
78
- end
83
+ begin
84
+ worker = JobWorker.new wid
85
+ log_info "JobWorker [#{wid}]: #{worker}"
86
+ rescue Exception => ex
87
+ log_error "EXCEPTION: #{ex.message}"
79
88
  end
80
-
81
- # We should never get here
82
89
  end
83
90
  end
84
91
 
85
92
  def create_conchita_thread
86
93
  Thread.new do
87
94
  begin
88
- @conchita = Conchita.new
89
- rescue Exception => e
90
- info "CONCHITA EXCEPTION: #{e.inspect}"
95
+ worker = ConchitaWorker.new :conchita
96
+ log_info "ConchitaWorker: #{worker}"
97
+ rescue Exception => ex
98
+ log_error "EXCEPTION: #{ex.message}"
91
99
  end
92
100
  end
93
101
  end
94
102
 
95
- def work
96
- # Wait for a job to come into the queue
97
- worker_status :waiting
98
- info "waiting for a job"
99
- job = $queue.pop
100
-
101
- # Prepare the job for processing
102
- worker_status :working
103
- worker_jid job.id
104
- info "working"
105
- job.wid = Thread.current.thread_variable_get :wid
106
-
107
- # Processs this job protected by a timeout
108
- Timeout::timeout(@timeout, RestFtpDaemon::JobTimeout) do
109
- job.process
110
- end
111
-
112
- # Processing done
113
- worker_status :finished
114
- info "finished"
115
- worker_jid nil
116
- job.wid = nil
117
-
118
- # Increment total processed jobs count
119
- $queue.counter_inc :jobs_processed
120
-
121
- rescue RestFtpDaemon::JobTimeout => ex
122
- info "JOB TIMED OUT", lines: ex.backtrace
123
- worker_status :timeout
124
- job.oops_you_stop_now ex unless job.nil?
125
- sleep 1
126
-
127
- rescue Exception => ex
128
- info "UNHDNALED EXCEPTION: #{ex.message}", lines: ex.backtrace
129
- worker_status :crashed
130
- job.oops_after_crash ex unless job.nil?
131
- sleep 1
132
-
133
- else
134
- # Clean job status
135
- worker_status :free
136
- job.wid = nil
137
-
138
- end
139
-
140
103
  protected
141
104
 
142
- def info message, context = {}
143
- return if @logger.nil?
144
-
145
- # Forward to logger
146
- @logger.info_with_id message,
147
- wid: Thread.current.thread_variable_get(:wid),
148
- jid: Thread.current.thread_variable_get(:jid),
149
- origin: self.class.to_s
150
- end
151
-
152
- def worker_status status
153
- Thread.current.thread_variable_set :status, status
154
- Thread.current.thread_variable_set :updted_at, Time.now
155
- end
156
-
157
- def worker_jid jid
158
- Thread.current.thread_variable_set :jid, jid
159
- Thread.current.thread_variable_set :updted_at, Time.now
160
- end
161
-
162
105
  if Settings.newrelic_enabled?
106
+ add_transaction_tracer :create_conchita_thread, :category => :task
163
107
  add_transaction_tracer :create_worker_thread, :category => :task
164
108
  add_transaction_tracer :work, :category => :task
165
109
  end
@@ -15,10 +15,6 @@ defaults: &defaults
15
15
  overwrite: false
16
16
  timeout: 1800
17
17
 
18
- # newrelic:
19
- # appname: rftpd-testing
20
- # license: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
21
-
22
18
  conchita:
23
19
  timer: 60
24
20
  # garbage_collector: true
@@ -28,6 +24,7 @@ defaults: &defaults
28
24
 
29
25
  debug:
30
26
  ftp: false
27
+ # license: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
31
28
 
32
29
  logs:
33
30
  thin: "/var/log/rftpd-environment-thin.log"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rest-ftp-daemon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.214.0
4
+ version: 0.220.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bruno MEDICI
@@ -229,15 +229,14 @@ files:
229
229
  - lib/rest-ftp-daemon/api/job_presenter.rb
230
230
  - lib/rest-ftp-daemon/api/jobs.rb
231
231
  - lib/rest-ftp-daemon/api/root.rb
232
- - lib/rest-ftp-daemon/api/routes.rb
233
232
  - lib/rest-ftp-daemon/api/status.rb
234
- - lib/rest-ftp-daemon/conchita.rb
235
233
  - lib/rest-ftp-daemon/constants.rb
236
234
  - lib/rest-ftp-daemon/exceptions.rb
237
235
  - lib/rest-ftp-daemon/helpers.rb
238
236
  - lib/rest-ftp-daemon/job.rb
239
237
  - lib/rest-ftp-daemon/job_queue.rb
240
238
  - lib/rest-ftp-daemon/logger.rb
239
+ - lib/rest-ftp-daemon/logger_helper.rb
241
240
  - lib/rest-ftp-daemon/logger_pool.rb
242
241
  - lib/rest-ftp-daemon/notification.rb
243
242
  - lib/rest-ftp-daemon/paginate.rb
@@ -252,6 +251,9 @@ files:
252
251
  - lib/rest-ftp-daemon/views/dashboard_table.haml
253
252
  - lib/rest-ftp-daemon/views/dashboard_tokens.haml
254
253
  - lib/rest-ftp-daemon/views/dashboard_workers.haml
254
+ - lib/rest-ftp-daemon/worker.rb
255
+ - lib/rest-ftp-daemon/worker_conchita.rb
256
+ - lib/rest-ftp-daemon/worker_job.rb
255
257
  - lib/rest-ftp-daemon/worker_pool.rb
256
258
  - rest-ftp-daemon.gemspec
257
259
  - rest-ftp-daemon.yml.sample
@@ -1,16 +0,0 @@
1
- module RestFtpDaemon
2
- module API
3
- class Root < Grape::API
4
-
5
-
6
- ####### GET /routes
7
-
8
- get '/routes' do
9
- info "GET /routes"
10
- status 200
11
- return RestFtpDaemon::API::Root::routes
12
- end
13
-
14
- end
15
- end
16
- end
@@ -1,65 +0,0 @@
1
- module RestFtpDaemon
2
- class Conchita
3
-
4
- def initialize
5
- # Logger
6
- @logger = RestFtpDaemon::LoggerPool.instance.get :workers
7
-
8
- # Conchita configuration
9
- @conchita = Settings.conchita
10
- if @conchita.nil?
11
- return info "conchita: missing conchita.* configuration"
12
- elsif @conchita[:timer].nil?
13
- return info "conchita: missing conchita.timer value"
14
- end
15
-
16
- # Start main loop
17
- info "initialized #{@conchita.inspect}"
18
- cleanup
19
- end
20
-
21
- protected
22
-
23
- def maxage status
24
- @conchita["clean_#{status.to_s}"] || 0
25
- end
26
-
27
- def cleanup
28
- loop do
29
- #info "cleanup"
30
- # info "conchita_loop: cleanup "
31
- $queue.expire JOB_STATUS_FINISHED, maxage(JOB_STATUS_FINISHED)
32
- $queue.expire JOB_STATUS_FAILED, maxage(JOB_STATUS_FAILED)
33
- $queue.expire JOB_STATUS_QUEUED, maxage(JOB_STATUS_QUEUED)
34
-
35
- # Force garbage collector
36
- GC.start if @conchita["garbage_collector"]
37
-
38
- # Sleep for a few seconds
39
- sleep @conchita[:timer]
40
- end
41
- end
42
-
43
- # def conchita_gc
44
- # # Read config state
45
- # proceed = @conchita["clean_garbage"] || false
46
- # #info "conchita_clean status[#{status.to_s}] \t maxage[#{maxage}] s"
47
- # return unless proceed
48
-
49
- # # Trig Ruby's garbage collector
50
- # info "conchita_gc forced garbage collecting"
51
- # GC.start
52
- # end
53
-
54
- def info message, lines = []
55
- return if @logger.nil?
56
-
57
- # Forward to logger
58
- @logger.info_with_id message,
59
- wid: :conchita,
60
- lines: lines,
61
- origin: self.class.to_s
62
- end
63
-
64
- end
65
- end