fleiss 0.1.3 → 0.2.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: 45bf4b9f2a7a9d88cbdd4feb8fb2992d6545554695c791a1f327a5a26da3e53d
4
- data.tar.gz: 4434eac634c36952365be272f5f8327462577db2cc338c97b1f242f2f59d14bf
3
+ metadata.gz: b76bfb38938511dcf5053855e5a9091ad694c0b1c1fafba3e551b5757aaa1dfe
4
+ data.tar.gz: 9ff274eeba83d0e5ffc878c4934a4c03bbc91ede2bd2eb0f7e6c026fdd3dc1bb
5
5
  SHA512:
6
- metadata.gz: d008669212582e7e5190ee0901024555c5031d0295b934d08dd91508d0b83251ea7790540c0e4d8339cae15b6aa9c6c043d5fc39eaa18bc5a696a30e23bc4a0f
7
- data.tar.gz: fc3f5555a6290abebd712e545172fea30eb4be0074da2eb86bc1cb136216af563054111d45599f2e9ddb3560a94c00256e7a8d00b3f7b69e849d39264911131d
6
+ metadata.gz: b145758e74ac991211a2677409fe31a185a094ee3304d54d2cf41937446c257dba225a01183591743486e3d91725e37ffbec747c01f3a144652693645e08d5f6
7
+ data.tar.gz: 55909e4fbf2652fe613767eca254d585ac89e07ff8d2e40c5b6e502e266a8fda6dd4f02b32c4dc1cf880cfc33f83e8433fed30e255978531bb2f3ef621bd2a83
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fleiss (0.1.3)
4
+ fleiss (0.2.0)
5
5
  activejob (>= 5.0)
6
6
  activerecord (>= 5.0)
7
7
  concurrent-ruby
@@ -25,11 +25,11 @@ GEM
25
25
  tzinfo (~> 1.1)
26
26
  arel (9.0.0)
27
27
  ast (2.4.0)
28
- concurrent-ruby (1.1.3)
28
+ concurrent-ruby (1.1.4)
29
29
  diff-lcs (1.3)
30
30
  globalid (0.4.1)
31
31
  activesupport (>= 4.2.0)
32
- i18n (1.2.0)
32
+ i18n (1.3.0)
33
33
  concurrent-ruby (~> 1.0)
34
34
  jaro_winkler (1.5.1)
35
35
  minitest (5.11.3)
@@ -83,4 +83,4 @@ DEPENDENCIES
83
83
  sqlite3
84
84
 
85
85
  BUNDLED WITH
86
- 1.16.4
86
+ 1.17.2
data/bin/fleiss CHANGED
@@ -5,6 +5,9 @@ $LOAD_PATH.push(lib_dir) unless $LOAD_PATH.include?(lib_dir)
5
5
 
6
6
  require 'fleiss/cli'
7
7
 
8
+ STDOUT.sync = true
9
+ STDERR.sync = true
10
+
8
11
  cli = Fleiss::CLI.instance
9
12
  begin
10
13
  cli.parse!
data/fleiss.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'fleiss'
3
- s.version = '0.1.3'
3
+ s.version = '0.2.0'
4
4
  s.authors = ['Black Square Media Ltd']
5
5
  s.email = ['info@blacksquaremedia.com']
6
6
  s.summary = %(Minimialist background jobs backed by ActiveJob and ActiveRecord.)
data/lib/fleiss/cli.rb CHANGED
@@ -2,7 +2,6 @@ require 'singleton'
2
2
  require 'optparse'
3
3
  require 'yaml'
4
4
  require 'erb'
5
- require 'logger'
6
5
 
7
6
  module Fleiss
8
7
  class CLI
@@ -15,7 +14,6 @@ module Fleiss
15
14
  require: [],
16
15
  concurrency: 10,
17
16
  wait_time: 1.0,
18
- logfile: STDOUT,
19
17
  }.freeze
20
18
 
21
19
  attr_reader :opts
@@ -46,14 +44,11 @@ module Fleiss
46
44
  opts[:require].each {|n| require n }
47
45
  require 'fleiss/worker'
48
46
 
49
- logdev = opts[:logfile]
50
- logdev.sync = true if logdev.respond_to?(:sync=)
51
-
47
+ ActiveJob::Base.logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(opts[:logfile])) if opts[:logfile]
52
48
  Fleiss::Worker.run \
53
49
  queues: opts[:queues],
54
50
  concurrency: opts[:concurrency],
55
- wait_time: opts[:wait_time],
56
- logger: Logger.new(logdev)
51
+ wait_time: opts[:wait_time]
57
52
  end
58
53
 
59
54
  def parser # rubocop:disable Metrics/MethodLength
data/lib/fleiss/worker.rb CHANGED
@@ -1,11 +1,10 @@
1
1
  require 'fleiss'
2
2
  require 'concurrent/executor/fixed_thread_pool'
3
3
  require 'concurrent/atomic/atomic_fixnum'
4
- require 'logger'
5
4
  require 'securerandom'
6
5
 
7
6
  class Fleiss::Worker
8
- attr_reader :queues, :uuid, :wait_time, :logger
7
+ attr_reader :queues, :uuid, :wait_time
9
8
 
10
9
  # Shortcut for new(*args).run
11
10
  def self.run(*args)
@@ -18,24 +17,22 @@ class Fleiss::Worker
18
17
  # @option [Array<String>] :queues queues to watch. Default: ["default"]
19
18
  # @option [Integer] :concurrency the number of concurrent pool. Default: 10
20
19
  # @option [Numeric] :wait_time maximum time (in seconds) to wait for jobs when retrieving next batch. Default: 1s.
21
- # @option [Logger] :logger optional logger.
22
- def initialize(queues: [Fleiss::DEFAULT_QUEUE], concurrency: 10, wait_time: 1, logger: nil)
20
+ def initialize(queues: [Fleiss::DEFAULT_QUEUE], concurrency: 10, wait_time: 1)
23
21
  @uuid = SecureRandom.uuid
24
22
  @queues = Array(queues)
25
23
  @pool = Concurrent::FixedThreadPool.new(concurrency, fallback_policy: :discard)
26
24
  @wait_time = wait_time
27
- @logger = logger || Logger.new(nil)
28
25
  end
29
26
 
30
27
  # Run starts the worker
31
28
  def run
32
- logger.info "Worker #{uuid} starting - queues: #{queues.inspect}, concurrency: #{@pool.max_length}"
29
+ log(:info) { "Worker #{uuid} starting - queues: #{queues.inspect}, concurrency: #{@pool.max_length}" }
33
30
  loop do
34
31
  run_cycle
35
32
  sleep @wait_time
36
33
  end
37
34
  rescue SignalException => e
38
- logger.info "Worker #{uuid} received #{e.message}. Shutting down..."
35
+ log(:info) { "Worker #{uuid} received #{e.message}. Shutting down..." }
39
36
  ensure
40
37
  @pool.shutdown
41
38
  @pool.wait_for_termination
@@ -43,6 +40,15 @@ class Fleiss::Worker
43
40
 
44
41
  private
45
42
 
43
+ def log(severity, &block)
44
+ logger = ActiveJob::Base.logger
45
+ if logger.respond_to?(:tagged)
46
+ logger.tagged('Fleiss') { logger.send(severity, &block) }
47
+ else
48
+ logger.send(severity, &block)
49
+ end
50
+ end
51
+
46
52
  def run_cycle
47
53
  return if @pool.shuttingdown?
48
54
 
@@ -67,7 +73,7 @@ class Fleiss::Worker
67
73
  owner = "#{uuid}/#{thread_id}"
68
74
  return unless job.start(owner)
69
75
 
70
- logger.info { "Worker #{uuid} execute job ##{job.id} (by thread #{thread_id})" }
76
+ log(:info) { "Worker #{uuid} execute job ##{job.id} (by thread #{thread_id})" }
71
77
  finished = false
72
78
  begin
73
79
  ActiveJob::Base.execute job.job_data
@@ -83,12 +89,12 @@ class Fleiss::Worker
83
89
  end
84
90
 
85
91
  def handle_exception(err, intro)
86
- lines = [
87
- "Worker #{uuid} error on #{intro}:",
88
- "#{err.class.name}: #{err.message}",
89
- err.backtrace,
90
- ].compact.flatten
91
-
92
- logger.error lines.join("\n")
92
+ log(:error) do
93
+ [
94
+ "Worker #{uuid} error on #{intro}:",
95
+ "#{err.class.name}: #{err.message}",
96
+ err.backtrace,
97
+ ].compact.flatten.join("\n")
98
+ end
93
99
  end
94
100
  end
data/lib/fleiss.rb CHANGED
@@ -1,3 +1,7 @@
1
+ require 'active_job'
2
+ require 'active_job/queue_adapters/fleiss_adapter'
3
+ require 'fleiss/backend'
4
+
1
5
  module Fleiss
2
6
  DEFAULT_QUEUE = 'default'.freeze
3
7
 
@@ -5,5 +9,3 @@ module Fleiss
5
9
  self::Backend::ActiveRecord
6
10
  end
7
11
  end
8
-
9
- require 'fleiss/backend'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fleiss
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Black Square Media Ltd
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-19 00:00:00.000000000 Z
11
+ date: 2018-12-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activejob