octokiq 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f28e37cf9c233c23e2bd805a66f09499ab4f7076e8a6275576aa119c3f16f374
4
- data.tar.gz: 12a6f510c04659f8bb636bf3e1f687efb798c8a365ef227c4b0a5ba3d2082848
3
+ metadata.gz: 44d0f352223a198828049c695b9f9eb6794c9e59e61c78437a725d04c9d89817
4
+ data.tar.gz: 69a40dc857ffc594a7d73ab3e439aed6f2e03d58923ebd76ee3336c759aa7069
5
5
  SHA512:
6
- metadata.gz: 343e6dbfb7161aa9dbf0aee8006176073f814d838d2387127e99a498cba5dabc42b7df1b632687486382e4caebd509b3296020492eb4943c1bf01f71df990282
7
- data.tar.gz: 92f38623fec01098154151acf6d13af9210cd117185871d2372cf25d246d704cf4202d5f985233f039caa70ba88b1207c2ff8488eb87a7b64db13acc713e8d61
6
+ metadata.gz: 5be9cc5ff099a09634c0af35e1d1764233f25f8d16f353d7e1c078a3c976dfb6cfd330779146c0e8b209f5a7ce2149e0900c7d0dde2ed5e49bb9fa04d482e5e7
7
+ data.tar.gz: 364d73063e5133ba872ee90454704212c66462ed4907c5a2f87662b68f12cb318a26e1d9d02a84ba79e03feca1fedb2e92478a4a0c48bc29b926087c688a1b95
@@ -12,4 +12,8 @@ Initial release 🚀
12
12
  ## 0.1.2 - 2020-12-30
13
13
 
14
14
  - Fix the ractor server loop bug
15
- - Add configuration `force_thread_mode`
15
+ - Add configuration `force_thread_mode`
16
+
17
+ ## 0.1.3 - 2020-12-31
18
+
19
+ - Seperate `server` and `client` concerns
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- octokiq (0.1.0)
4
+ octokiq (0.1.3)
5
5
  redis (~> 4.2)
6
6
 
7
7
  GEM
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'bundler/setup'
4
4
  require 'octokiq'
5
+ require 'optparse'
5
6
 
6
7
  OptionParser.new do |opts|
7
8
  opts.banner = 'Usage: bundle exec octokiq [options]'
@@ -1,16 +1,12 @@
1
- require 'redis'
2
-
3
- require 'etc'
4
- require 'logger'
5
- require 'json'
6
- require 'optparse'
7
-
8
1
  require 'octokiq/version'
9
- require 'octokiq/configuration'
2
+ require 'octokiq/logger'
10
3
  require 'octokiq/connection'
11
- require 'octokiq/worker'
4
+ require 'octokiq/consts'
5
+ require 'octokiq/configuration'
12
6
  require 'octokiq/processor'
7
+ require 'octokiq/client'
13
8
  require 'octokiq/server'
9
+ require 'octokiq/worker'
14
10
  require 'octokiq/cmd'
15
11
 
16
12
  module Octokiq
@@ -25,14 +21,6 @@ module Octokiq
25
21
  @configuration ||= Configuration.new
26
22
  end
27
23
 
28
- def server_connection
29
- @server_connection ||= Connection.new
30
- end
31
-
32
- def client_connection
33
- @client_connection ||= Connection.new
34
- end
35
-
36
24
  def logger
37
25
  Logger.new($stdout)
38
26
  end
@@ -0,0 +1,31 @@
1
+ module Octokiq
2
+ module Client
3
+ extend self
4
+
5
+ class Configuration
6
+ attr_accessor :default_queue
7
+
8
+ def initialize
9
+ @default_queue = Consts::DEFAULT_QUEUE
10
+ end
11
+ end
12
+
13
+ def push(*args)
14
+ connection.push(*args)
15
+ end
16
+
17
+ def configure
18
+ yield(configuration)
19
+ end
20
+
21
+ def configuration
22
+ @configuration ||= Configuration.new
23
+ end
24
+
25
+ private
26
+
27
+ def connection
28
+ @connection ||= Connection.new
29
+ end
30
+ end
31
+ end
@@ -2,36 +2,15 @@ module Octokiq
2
2
  class CMD
3
3
  def run
4
4
  puts logo
5
- server.start
5
+ Server.start
6
6
  rescue Interrupt
7
7
  Octokiq.logger.info 'Bye!'
8
8
  end
9
9
 
10
10
  private
11
11
 
12
- def server
13
- @server ||= Server.new
14
- end
15
-
16
12
  def logo
17
- <<-EOF
18
- _,gggggg,_
19
- ,d8P""d8P"Y8b, I8 ,dPYb,
20
- ,d8' Y8 "8b,dP I8 IP'`Yb
21
- d8' `Ybaaad88P' 88888888 I8 8I gg
22
- 8P `""""Y8 I8 I8 8bgg, ""
23
- 8b d8 ,gggg, I8 ,ggggg, I8 dP" "8 gg ,gggg,gg
24
- Y8, ,8P dP" "Yb I8 dP" "Y8ggg I8d8bggP" 88 dP" "Y8I
25
- `Y8, ,8P' i8' ,I8, i8' ,8I I8P' "Yb, 88 i8' ,8I
26
- `Y8b,,__,,d8P' ,d8,_ _ ,d88b, ,d8, ,d8' ,d8 `Yb,_,88,_,d8, ,d8b
27
- `"Y8888P"' P""Y8888PP 8P""Y8 P"Y8888P" 88P Y88P""Y8P"Y8888P"88d
28
- I8P
29
- I8'
30
- I8
31
- I8
32
- I8
33
- I8
34
- EOF
13
+ "Hello! Welcome to Octokiq!"
35
14
  end
36
15
  end
37
16
  end
@@ -1,17 +1,9 @@
1
1
  module Octokiq
2
2
  class Configuration
3
- attr_accessor :processors_count,
4
- :queue_prefix,
5
- :default_queue,
6
- :queues,
7
- :force_thread_mode
3
+ attr_accessor :queue_prefix
8
4
 
9
5
  def initialize
10
- @processors_count = Etc.nprocessors
11
- @queue_prefix = 'octokiq'
12
- @default_queue = 'default'
13
- @queues = [@default_queue]
14
- @force_thread_mode = false
6
+ @queue_prefix = Consts::QUEUE_PREFIX
15
7
  end
16
8
  end
17
9
  end
@@ -1,3 +1,6 @@
1
+ require 'redis'
2
+ require 'json'
3
+
1
4
  module Octokiq
2
5
  class Connection
3
6
  attr_accessor :redis
@@ -0,0 +1,8 @@
1
+ module Octokiq
2
+ module Consts
3
+ DEFAULT_QUEUE = 'default'.freeze
4
+ QUEUE_PREFIX = 'octokiq'.freeze
5
+ ITEM_CLASS_KEY = 'class'.freeze
6
+ ITEM_ARGS_KEY = 'args'.freeze
7
+ end
8
+ end
@@ -1,3 +1,5 @@
1
+ require 'logger'
2
+
1
3
  module Octokiq
2
4
  class Logger < ::Logger
3
5
  end
@@ -2,8 +2,8 @@ module Octokiq
2
2
  class Processor
3
3
  attr_accessor :klass, :args
4
4
  def initialize(job)
5
- klass_name = job.fetch(Worker::CLASS_KEY)
6
- @args = job.fetch(Worker::ARGS_KEY)
5
+ klass_name = job.fetch(Consts::ITEM_CLASS_KEY)
6
+ @args = job.fetch(Consts::ITEM_ARGS_KEY)
7
7
  @klass = Object.const_get(klass_name)
8
8
  end
9
9
 
@@ -1,46 +1,84 @@
1
+ require 'etc'
2
+
1
3
  module Octokiq
2
- class Server
4
+ module Server
5
+ extend self
6
+
7
+ class Configuration
8
+ attr_accessor :concurrency,
9
+ :queues,
10
+ :thread_mode
11
+
12
+ def initialize
13
+ @concurrency = Etc.nprocessors
14
+ @queues = [Consts::DEFAULT_QUEUE]
15
+ @thread_mode = false
16
+ end
17
+ end
18
+
19
+ def configure
20
+ yield(configuration)
21
+ end
22
+
23
+ def configuration
24
+ @configuration ||= Configuration.new
25
+ end
26
+
3
27
  def start
4
- handle_pipe
5
- handle_tube
28
+ configuration.thread_mode ? handle_thread_jobs : ractor_workers
6
29
 
7
30
  loop do
8
- job = Octokiq.server_connection.fetch(queues)
9
- Octokiq.configuration.force_thread_mode ? tube << job : pipe << job
31
+ job = connection.fetch(queues)
32
+ pipe << job
10
33
  end
11
34
  end
12
35
 
13
36
  private
14
37
 
15
- def handle_pipe
16
- (1..Octokiq.configuration.processors_count).map do
17
- Ractor.new(pipe, tube) do |pipe, tube|
18
- loop do
19
- job = pipe.take
20
- begin
21
- Processor.new(job).run
22
- rescue Ractor::IsolationError => e
23
- Octokiq.logger.warn 'Ractor::IsolationError'
24
- tube << job
25
- end
38
+ def connection
39
+ @connection ||= Connection.new
40
+ end
41
+
42
+ def ractor_workers
43
+ @ractor_workers ||= (1..concurrency).map do
44
+ Ractor.new(pipe) do |pipe|
45
+ while job = pipe.take
46
+ Ractor.yield Processor.new(job).run
26
47
  end
27
48
  end
28
49
  end
29
50
  end
30
51
 
31
- def handle_tube
52
+ def handle_thread_jobs
53
+ semaphore = Mutex.new
54
+ counter = 0
32
55
  Thread.new do
33
- loop do
34
- job = tube.take
56
+ while counter < concurrency && job = pipe.take
57
+ semaphore.synchronize { counter += 1 }
35
58
  Thread.new do
36
59
  Processor.new(job).run
60
+ semaphore.synchronize { counter -= 1 }
37
61
  end
38
62
  end
39
63
  end
40
64
  end
41
65
 
66
+ def thread_workers
67
+ @thread_workers = []
68
+ Thread.new do
69
+ while job = pipe.take
70
+ Processor.new(job).run
71
+ end
72
+ end
73
+ @thread_workers
74
+ end
75
+
76
+ def concurrency
77
+ @concurrency ||= configuration.concurrency
78
+ end
79
+
42
80
  def queues
43
- Octokiq.configuration.queues
81
+ configuration.queues
44
82
  end
45
83
 
46
84
  def pipe
@@ -50,13 +88,5 @@ module Octokiq
50
88
  end
51
89
  end
52
90
  end
53
-
54
- def tube
55
- @tube ||= Ractor.new do
56
- loop do
57
- Ractor.yield Ractor.receive
58
- end
59
- end
60
- end
61
91
  end
62
92
  end
@@ -1,3 +1,3 @@
1
1
  module Octokiq
2
- VERSION = '0.1.2'.freeze
2
+ VERSION = '0.1.3'.freeze
3
3
  end
@@ -1,14 +1,11 @@
1
1
  module Octokiq
2
2
  module Worker
3
- CLASS_KEY = 'class'.freeze
4
- ARGS_KEY = 'args'.freeze
5
-
6
3
  class Configuration
7
4
  OPTIONS = [:queue].freeze
8
5
  attr_accessor(*OPTIONS)
9
6
 
10
7
  def initialize
11
- @queue = Octokiq.configuration.default_queue
8
+ @queue = Client.configuration.default_queue
12
9
  end
13
10
  end
14
11
 
@@ -18,7 +15,7 @@ module Octokiq
18
15
 
19
16
  module ClassMethods
20
17
  def perform_async(*args)
21
- client_push(CLASS_KEY => self, ARGS_KEY => args)
18
+ _push(Consts::ITEM_CLASS_KEY => self, Consts::ITEM_ARGS_KEY => args)
22
19
  end
23
20
 
24
21
  def configuration
@@ -33,8 +30,8 @@ module Octokiq
33
30
 
34
31
  private
35
32
 
36
- def client_push(item)
37
- Octokiq.client_connection.push(configuration.queue, item)
33
+ def _push(item)
34
+ Client.push(configuration.queue, item)
38
35
  end
39
36
  end
40
37
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: octokiq
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bran Liang
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-12-30 00:00:00.000000000 Z
11
+ date: 2020-12-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
@@ -45,9 +45,11 @@ files:
45
45
  - bin/setup
46
46
  - exe/octokiq
47
47
  - lib/octokiq.rb
48
+ - lib/octokiq/client.rb
48
49
  - lib/octokiq/cmd.rb
49
50
  - lib/octokiq/configuration.rb
50
51
  - lib/octokiq/connection.rb
52
+ - lib/octokiq/consts.rb
51
53
  - lib/octokiq/logger.rb
52
54
  - lib/octokiq/processor.rb
53
55
  - lib/octokiq/server.rb