ruboty-moneta 0.1.2 → 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
  SHA1:
3
- metadata.gz: c7ee242db65f9474acc5c251cf6fae62bbc95313
4
- data.tar.gz: 167863df2a8fa5673941c58be5a33f2ff6e10169
3
+ metadata.gz: deede0a29a8c8b4a33310ef97a27a915707f6f29
4
+ data.tar.gz: cee6611da47b099948ad515ad28dcdebfc183922
5
5
  SHA512:
6
- metadata.gz: a470c5b28465122987b1cb07842cc0283d13c2d1ed6e6c791fc225b7779c4e6332d18a60fe2dd74da815b24aead39c06ffc3b01a27cb8dcc3ba701ebc1c1e1d3
7
- data.tar.gz: dd6f005125be95a374fece62ad220230a43d70d3b2237e941a2d517f4266375cd099e6c2ea8a310760a50cda7373ee46fd1508f0d6276ced63d0e46a0b1f1e66
6
+ metadata.gz: 506f23e3c07404b42ba864c973f361ae6d47d05733d3a36416717cdc86c484a8104b767a007ecd59c7958e22e5ede2de543b83593438ce9557758f07a26d97c6
7
+ data.tar.gz: 04710fbb9577cd664d8577587ef97a52eee0a0b132224df0ba0000401279e71a3e5f44f0634434b1cab69e7a3876239b4c8f9666471afe45234caed0f65da6f9
data/README.md CHANGED
@@ -18,7 +18,7 @@ gem 'ruboty-moneta'
18
18
  please setting [backend_type](https://github.com/minad/moneta#supported-backends) to MONETA_BACKEND
19
19
 
20
20
 
21
- ```
21
+ ```shell
22
22
  MONETA_BACKEND="{backend_type}"
23
23
  ```
24
24
 
@@ -35,31 +35,45 @@ Sqlite3のfileパラメータを指定したいときは下記のようになり
35
35
  # MONETA_#{Backend}_#{Options Hash}
36
36
  MONETA_SQLITE_FILE = "{file path}"
37
37
  ```
38
+ ### Setting replicate
39
+
40
+ 複数のbackendに同時に書き込みます。
41
+ MONETA_BACKENDで設定されたbackendのデータをmasterとして扱います。
42
+ ストアの移行作業時などに使ってください
43
+
44
+ ```
45
+ MONETA_BACKEND_SLAVE={"backend" or ["backend1", "backend2"]}
46
+
47
+ # example
48
+
49
+ # Replicate in YAML
50
+ MONETA_BACKEND_SLAVE="YAML"
51
+
52
+ # Replicate in YAML and File
53
+ MONETA_BACKEND_SLAVE=["YAML", "File"]
54
+
55
+ ```
38
56
 
39
57
  ### example
40
58
 
41
- #### BackendType == memory
59
+ #### use memory
42
60
 
43
61
  ```ruby
44
62
  gem 'ruboty-moneta'
45
63
  ```
46
64
 
47
- #### BackendType == File
48
-
49
- ##### Gemfile
65
+ #### use File
50
66
 
51
67
  ```ruby
52
68
  gem 'ruboty-moneta'
53
69
  ```
54
70
 
55
- ##### dotenv
56
-
57
- ```shell:.dotenv
71
+ ```shell
58
72
  MONETA_BACKEND="File"
59
73
  MONETA_FILE_DIR="./db"
60
74
  ```
61
75
 
62
- #### BackendType == sqlite
76
+ #### use Sqlite
63
77
 
64
78
  ```ruby
65
79
  gem 'ruboty-moneta'
@@ -79,7 +93,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
79
93
 
80
94
  ## Contributing
81
95
 
82
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ruboty-moneta. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
96
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rike422/ruboty-moneta. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
83
97
 
84
98
 
85
99
  ## License
@@ -6,7 +6,8 @@ module Ruboty
6
6
  KEY = "brain"
7
7
 
8
8
  attr_reader :thread
9
- env :MONETA_BACKEND, "use moneta db backend"
9
+ env :MONETA_BACKEND, "use moneta backend (default: memory)"
10
+ env :MONETA_BACKEND_SLAVE, "use cloning data to slaves (defalut: nil)", optional: true
10
11
  env :MONETA_SAVE_INTERVAL, "Interval sec to push data to moneta (default: 5)", optional: true
11
12
  env :MONETA_NAMESPACE, "Moneta name space (default: ruboty)", optional: true
12
13
 
@@ -23,42 +24,59 @@ module Ruboty
23
24
  # Override.
24
25
  def validate!
25
26
  super
26
- Ruboty.die("#{self.class.usage}") unless backend
27
+ Ruboty.die("#{self.class.usage}") unless master_backend
27
28
  end
28
29
 
29
30
  private
30
31
 
31
32
  def push
32
- store[KEY] = Marshal.dump(data)
33
+ master[key] = data
33
34
  end
34
35
 
35
36
  def pull
36
- if str = store[KEY]
37
- Marshal.load(str)
37
+ if str = master[key]
38
+ str
38
39
  end
39
40
  rescue TypeError
40
41
  end
41
42
 
43
+ def replicate
44
+ slaves.each do |slave|
45
+ slave[key] = data
46
+ end
47
+ end
48
+
49
+ def wait
50
+ sleep(interval)
51
+ end
52
+
42
53
  def sync
43
54
  loop do
44
55
  wait
45
56
  push
57
+ replicate
46
58
  end
47
59
  end
48
60
 
49
- def backend
50
- @backend ||= (ENV["MONETA_BACKEND"] || "Memory").to_sym
61
+ def interval
62
+ (ENV["MONETA_SAVE_INTERVAL"] || 5).to_i
51
63
  end
52
64
 
53
- def wait
54
- sleep(interval)
65
+ def master_backend
66
+ (ENV["MONETA_BACKEND"] || "Memory").to_sym
55
67
  end
56
68
 
57
- def store
58
- @store||= ::Moneta.new(backend, moneta_option)
69
+ def slave_backend
70
+ eval(ENV["MONETA_BACKEND_SLAVE"] || "")
71
+ rescue NameError
72
+ ENV["MONETA_BACKEND_SLAVE"]
59
73
  end
60
74
 
61
- def moneta_option
75
+ def namespace
76
+ ENV["MONETA_NAMESPACE"] || "ruboty"
77
+ end
78
+
79
+ def fetch_option(backend)
62
80
  prefix = backend.to_s.underscore.upcase
63
81
  ENV.reduce({}) do |option, env|
64
82
  match = env[0].to_s.match(/MONETA_#{prefix}_([A-Z_]+)$/)
@@ -68,12 +86,25 @@ module Ruboty
68
86
  end
69
87
  end
70
88
 
71
- def namespace
72
- ENV["MONETA_NAMESPACE"] || "ruboty"
89
+ def master
90
+ @master ||= ::Moneta.new(master_backend, fetch_option(master_backend))
73
91
  end
74
92
 
75
- def interval
76
- (ENV["MONETA_SAVE_INTERVAL"] || 5).to_i
93
+ def slaves
94
+ @slaves ||=
95
+ if slave_backend.is_a?(String)
96
+ [::Moneta.new(slave_backend.to_sym, fetch_option(slave_backend))]
97
+ elsif slave_backend.is_a?(Array)
98
+ slave_backend.map do |slave_name|
99
+ ::Moneta.new(slave_name.to_sym, fetch_option(slave_name))
100
+ end
101
+ else
102
+ []
103
+ end
104
+ end
105
+
106
+ def key
107
+ @key ||= "#{namespace}:#{KEY}"
77
108
  end
78
109
  end
79
110
  end
@@ -1,5 +1,5 @@
1
1
  module Ruboty
2
2
  module Moneta
3
- VERSION = "0.1.2"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruboty-moneta
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - akira.takahashi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-11-09 00:00:00.000000000 Z
11
+ date: 2015-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruboty