shinq 0.1.0 → 0.2.0
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 +4 -4
- data/README.md +52 -5
- data/lib/shinq/cli.rb +8 -8
- data/lib/shinq/client.rb +16 -0
- data/shinq.gemspec +1 -1
- data/spec/integration_spec.rb +82 -13
- data/spec/shinq/configuration_spec.rb +11 -3
- data/spec/shinq_spec.rb +10 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 27fb65cd784140db7180182ee23747354ea16316
|
4
|
+
data.tar.gz: d3b16bae80c1e983ddff5a417caa23ae28f1ad0b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d9636a00d1c4d5c9a57fa5a3bdf697bb0814fb3c3caa3f61f4dc775473fb967b0c4f81980609025aaf4d475553bee80d03e6d872f3b428724938aa3f2acc7280
|
7
|
+
data.tar.gz: 36076bca1dfb6cb3c6d89f18dada6116a22b0b8123ad3e1987980cc030c0d21eec495e0326590ceb6695779d7b32e5c2ece588aa4dc5b5dc98a3bef8fcf82230
|
data/README.md
CHANGED
@@ -31,23 +31,70 @@ end
|
|
31
31
|
```
|
32
32
|
|
33
33
|
### Worker
|
34
|
+
|
35
|
+
#### Generate worker, migration file
|
36
|
+
|
37
|
+
```
|
38
|
+
$ rails generate shinq:worker worker_name title:string
|
39
|
+
create db/migrate/20141110061243_create_worker_names.rb
|
40
|
+
create app/workers/worker_name.rb
|
41
|
+
```
|
42
|
+
|
43
|
+
Generated worker file (app/workers/worker_name.rb)
|
34
44
|
```ruby
|
35
|
-
class
|
36
|
-
|
45
|
+
class WorkerName < ActiveJob::Base
|
46
|
+
queue_as :worker_names
|
37
47
|
|
38
48
|
def perform(args)
|
39
|
-
#
|
49
|
+
#do something
|
40
50
|
end
|
41
51
|
end
|
42
52
|
```
|
43
53
|
|
54
|
+
Generated migration file
|
55
|
+
```ruby
|
56
|
+
class CreateWorkerNames < ActiveRecord::Migration
|
57
|
+
def change
|
58
|
+
create_table :worker_names, {id: false, options: "ENGINE=QUEUE"} do |t|
|
59
|
+
t.string :job_id, null: false
|
60
|
+
t.string :title
|
61
|
+
t.datetime :enqueued_at, null: false
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
```
|
66
|
+
|
67
|
+
#### migrate
|
68
|
+
```
|
69
|
+
$ rake db:migrate
|
70
|
+
== 20141110061243 CreateWorkerNames: migrating ================================
|
71
|
+
-- create_table(:worker_names, {:id=>false, :options=>"ENGINE=QUEUE"})
|
72
|
+
-> 0.0260s
|
73
|
+
== 20141110061243 CreateWorkerNames: migrated (0.0261s) =======================
|
74
|
+
|
75
|
+
mysql> show create table worker_names\G
|
76
|
+
*************************** 1. row ***************************
|
77
|
+
Table: worker_names
|
78
|
+
Create Table: CREATE TABLE `worker_names` (
|
79
|
+
`job_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
|
80
|
+
`title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
|
81
|
+
`enqueued_at` datetime NOT NULL
|
82
|
+
) ENGINE=QUEUE DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
|
83
|
+
```
|
84
|
+
|
44
85
|
### Enqueue
|
45
86
|
```ruby
|
46
|
-
|
87
|
+
WorkerName.perform_later(title: 'foo')
|
47
88
|
```
|
48
89
|
|
49
90
|
### Worker execution
|
50
|
-
|
91
|
+
|
92
|
+
#### Basic execution
|
93
|
+
```
|
94
|
+
$ bundle exec shinq --worker worker_name
|
95
|
+
```
|
96
|
+
|
97
|
+
You can specify some options. see `bundle exec shinq --help`
|
51
98
|
|
52
99
|
## Contributing
|
53
100
|
|
data/lib/shinq/cli.rb
CHANGED
@@ -23,37 +23,37 @@ module Shinq
|
|
23
23
|
def parse_options(args)
|
24
24
|
opts = {}
|
25
25
|
parser = OptionParser.new do |opt|
|
26
|
-
opt.on('-d', '--daemon') do |v|
|
26
|
+
opt.on('-d', '--daemon', 'Daemonize process') do |v|
|
27
27
|
opts[:daemonize] = v
|
28
28
|
end
|
29
29
|
|
30
|
-
opt.on('--worker value') do |v|
|
30
|
+
opt.on('--worker value', 'Name of worker class') do |v|
|
31
31
|
opts[:worker_name] = v
|
32
32
|
end
|
33
33
|
|
34
|
-
opt.on('--process VALUE') do |v|
|
34
|
+
opt.on('--process VALUE', 'Number of workers') do |v|
|
35
35
|
opts[:process] = v.to_i
|
36
36
|
end
|
37
37
|
|
38
|
-
opt.on('--queue-timeout VALUE') do |v|
|
38
|
+
opt.on('--queue-timeout VALUE', 'Waiting queue time(sec). use function of queue_wait(Q4M)') do |v|
|
39
39
|
opts[:queue_timeout] = v.to_i
|
40
40
|
end
|
41
41
|
|
42
|
-
opt.on('--db-config VALUE') do |v|
|
42
|
+
opt.on('--db-config VALUE', 'Specify configuration file') do |v|
|
43
43
|
raise OptionParseError, "#{v} does not exist" unless File.exist?(v)
|
44
44
|
opts[:db_config] = YAML.load_file(v)
|
45
45
|
end
|
46
46
|
|
47
|
-
opt.on('--queue-database VALUE') do |v|
|
47
|
+
opt.on('--queue-database VALUE', 'Name of queue database') do |v|
|
48
48
|
raise OptionParseError, "#{v}'s settings does not exist" unless opts[:db_config][v]
|
49
49
|
opts[:queue_db] = v
|
50
50
|
end
|
51
51
|
|
52
|
-
opt.on('--require VALUE') do |v|
|
52
|
+
opt.on('--require VALUE', 'Add require path') do |v|
|
53
53
|
opts[:require] = v
|
54
54
|
end
|
55
55
|
|
56
|
-
opt.on('-v', '--version') do |v|
|
56
|
+
opt.on('-v', '--version', 'Print version') do |v|
|
57
57
|
puts "Shinq #{Shinq::VERSION}"
|
58
58
|
exit(0)
|
59
59
|
end
|
data/lib/shinq/client.rb
CHANGED
@@ -35,6 +35,22 @@ module Shinq
|
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
|
+
def self.queue_stats(table_name:)
|
39
|
+
quoted = SQL::Maker::Quoting.quote(table_name)
|
40
|
+
|
41
|
+
stats_query = "queue_stats(#{quoted})"
|
42
|
+
result = Shinq.connection.query("select #{stats_query}")
|
43
|
+
|
44
|
+
stats = result.first[stats_query].split(/\n/).each_with_object({}) do |s, h|
|
45
|
+
(k,v) = s.split(/:/)
|
46
|
+
h[k.to_sym] = v.to_i
|
47
|
+
end
|
48
|
+
|
49
|
+
stats.merge(
|
50
|
+
queue_count: stats[:rows_removed] - stats[:rows_written]
|
51
|
+
)
|
52
|
+
end
|
53
|
+
|
38
54
|
def self.done
|
39
55
|
Shinq.connection.query('select queue_end()')
|
40
56
|
end
|
data/shinq.gemspec
CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "shinq"
|
7
|
-
spec.version = '0.
|
7
|
+
spec.version = '0.2.0'
|
8
8
|
spec.authors = ["Ryoichi SEKIGUCHI"]
|
9
9
|
spec.email = ["ryopeko@gmail.com"]
|
10
10
|
spec.summary = %q{Worker and enqueuer for Q4M using the interface of ActiveJob.}
|
data/spec/integration_spec.rb
CHANGED
@@ -2,26 +2,95 @@ require 'spec_helper'
|
|
2
2
|
require 'shinq'
|
3
3
|
require 'shinq/client'
|
4
4
|
|
5
|
-
describe "Integration" do
|
5
|
+
describe "Integration", skip: ENV['TRAVIS'] do
|
6
|
+
let(:queue_table) { 'queue_test' }
|
7
|
+
|
6
8
|
before do
|
7
9
|
load_database_config(Shinq)
|
8
10
|
end
|
9
11
|
|
10
|
-
|
11
|
-
|
12
|
-
|
12
|
+
after do
|
13
|
+
Shinq.connection.query("delete from #{queue_table}")
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "Shinq::Client.enqueue,dequeue" do
|
17
|
+
context "valid args" do
|
18
|
+
let(:args) { {title: 'foo'} }
|
19
|
+
|
20
|
+
before do
|
21
|
+
Shinq::Client.enqueue(
|
22
|
+
table_name: queue_table,
|
23
|
+
job_id: 'jobid',
|
24
|
+
args: args
|
25
|
+
)
|
26
|
+
|
27
|
+
@queue = Shinq::Client.dequeue(table_name: queue_table)
|
28
|
+
Shinq::Client.done
|
29
|
+
end
|
13
30
|
|
14
|
-
|
15
|
-
|
16
|
-
table_name: queue_table,
|
17
|
-
job_id: 'jobid',
|
18
|
-
args: args
|
19
|
-
)
|
31
|
+
it { expect(@queue[:title]).to eq args[:title] }
|
32
|
+
end
|
20
33
|
|
21
|
-
|
22
|
-
|
34
|
+
context "invalid args" do
|
35
|
+
it {
|
36
|
+
expect {
|
37
|
+
Shinq::Client.enqueue(
|
38
|
+
table_name: queue_table,
|
39
|
+
job_id: 'jobid',
|
40
|
+
args: Array.new
|
41
|
+
)
|
42
|
+
}.to raise_error(ArgumentError)
|
43
|
+
}
|
23
44
|
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "Shinq::Client.abort" do
|
48
|
+
context "When client has a queue" do
|
49
|
+
before do
|
50
|
+
Shinq::Client.enqueue(
|
51
|
+
table_name: queue_table,
|
52
|
+
job_id: 'jobid',
|
53
|
+
args: {title: 'foo'}
|
54
|
+
)
|
55
|
+
|
56
|
+
@queue_count = Shinq.connection.query("select count(*) as c from #{queue_table}").first['c']
|
57
|
+
|
58
|
+
Shinq::Client.dequeue(table_name: queue_table)
|
59
|
+
Shinq::Client.abort
|
24
60
|
|
25
|
-
|
61
|
+
@after_queue_count = Shinq.connection.query("select count(*) as c from #{queue_table}").first['c']
|
62
|
+
end
|
63
|
+
|
64
|
+
it { expect(@after_queue_count).to be @queue_count }
|
65
|
+
end
|
66
|
+
|
67
|
+
context "When client does not have a queue" do
|
68
|
+
it {
|
69
|
+
expect {
|
70
|
+
Shinq::Client.abort
|
71
|
+
}.to raise_error (/not in owner mode/)
|
72
|
+
}
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
describe "Shinq::Client.queue_stats" do
|
77
|
+
subject(:stats) {
|
78
|
+
Shinq::Client.queue_stats(table_name: queue_table)
|
79
|
+
}
|
80
|
+
|
81
|
+
it { expect(stats).to have_key :rows_written }
|
82
|
+
it { expect(stats).to have_key :rows_removed }
|
83
|
+
it { expect(stats).to have_key :wait_immediate }
|
84
|
+
it { expect(stats).to have_key :wait_delayed }
|
85
|
+
it { expect(stats).to have_key :wait_timeout }
|
86
|
+
it { expect(stats).to have_key :restored_by_abort }
|
87
|
+
it { expect(stats).to have_key :restored_by_close }
|
88
|
+
it { expect(stats).to have_key :bytes_total }
|
89
|
+
it { expect(stats).to have_key :bytes_removed }
|
90
|
+
it { expect(stats).to have_key :queue_count }
|
91
|
+
|
92
|
+
it "queue_count expect to be equal rows_removed - rows_written" do
|
93
|
+
expect(stats[:queue_count]).to eq (stats[:rows_removed] - stats[:rows_written])
|
94
|
+
end
|
26
95
|
end
|
27
96
|
end
|
@@ -12,6 +12,7 @@ describe Shinq::Configuration do
|
|
12
12
|
it { is_expected.to respond_to(:default_db) }
|
13
13
|
it { is_expected.to respond_to(:process) }
|
14
14
|
it { is_expected.to respond_to(:queue_timeout) }
|
15
|
+
it { is_expected.to respond_to(:daemonize) }
|
15
16
|
end
|
16
17
|
|
17
18
|
describe ".new" do
|
@@ -42,9 +43,16 @@ describe Shinq::Configuration do
|
|
42
43
|
end
|
43
44
|
|
44
45
|
context "when default_db's config is present" do
|
45
|
-
let(:
|
46
|
-
|
47
|
-
|
46
|
+
let(:test_db_config) { {foo: :bar} }
|
47
|
+
let(:configuration) {
|
48
|
+
Shinq::Configuration.new(
|
49
|
+
default_db: :test,
|
50
|
+
db_config: {
|
51
|
+
test: test_db_config
|
52
|
+
})
|
53
|
+
}
|
54
|
+
|
55
|
+
it { expect(configuration.default_db_config).to be test_db_config }
|
48
56
|
end
|
49
57
|
end
|
50
58
|
|
data/spec/shinq_spec.rb
CHANGED
@@ -31,7 +31,7 @@ describe Shinq do
|
|
31
31
|
end
|
32
32
|
|
33
33
|
describe ".configuration=" do
|
34
|
-
context "when specify args" do
|
34
|
+
context "when specify args is Hash" do
|
35
35
|
let(:shinq) { shinq_class }
|
36
36
|
let(:args) { Hash.new }
|
37
37
|
|
@@ -39,6 +39,15 @@ describe Shinq do
|
|
39
39
|
expect(shinq.configuration=(args)).to eq args
|
40
40
|
end
|
41
41
|
end
|
42
|
+
|
43
|
+
context "when specify args is Shinq::Configuration" do
|
44
|
+
let(:shinq) { shinq_class }
|
45
|
+
let(:args) { Shinq::Configuration.new({}) }
|
46
|
+
|
47
|
+
it 'is expect to return specified args' do
|
48
|
+
expect(shinq.configuration=(args)).to eq args
|
49
|
+
end
|
50
|
+
end
|
42
51
|
end
|
43
52
|
|
44
53
|
describe ".connection" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: shinq
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryoichi SEKIGUCHI
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-11-
|
11
|
+
date: 2014-11-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|