garcun 0.0.2
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 +7 -0
- data/.gitattributes +17 -0
- data/.gitignore +197 -0
- data/.rspec +2 -0
- data/Gemfile +22 -0
- data/LICENSE +201 -0
- data/README.md +521 -0
- data/Rakefile +47 -0
- data/garcun.gemspec +83 -0
- data/lib/garcon.rb +290 -0
- data/lib/garcon/chef/chef_helpers.rb +343 -0
- data/lib/garcon/chef/coerce/coercer.rb +134 -0
- data/lib/garcon/chef/coerce/coercions/boolean_definitions.rb +34 -0
- data/lib/garcon/chef/coerce/coercions/date_definitions.rb +32 -0
- data/lib/garcon/chef/coerce/coercions/date_time_definitions.rb +32 -0
- data/lib/garcon/chef/coerce/coercions/fixnum_definitions.rb +34 -0
- data/lib/garcon/chef/coerce/coercions/float_definitions.rb +32 -0
- data/lib/garcon/chef/coerce/coercions/hash_definitions.rb +29 -0
- data/lib/garcon/chef/coerce/coercions/integer_definitions.rb +31 -0
- data/lib/garcon/chef/coerce/coercions/string_definitions.rb +45 -0
- data/lib/garcon/chef/coerce/coercions/time_definitions.rb +32 -0
- data/lib/garcon/chef/handler/devreporter.rb +127 -0
- data/lib/garcon/chef/log.rb +64 -0
- data/lib/garcon/chef/node.rb +100 -0
- data/lib/garcon/chef/provider/civilize.rb +209 -0
- data/lib/garcon/chef/provider/development.rb +159 -0
- data/lib/garcon/chef/provider/download.rb +420 -0
- data/lib/garcon/chef/provider/house_keeping.rb +265 -0
- data/lib/garcon/chef/provider/node_cache.rb +31 -0
- data/lib/garcon/chef/provider/partial.rb +183 -0
- data/lib/garcon/chef/provider/recovery.rb +80 -0
- data/lib/garcon/chef/provider/zip_file.rb +271 -0
- data/lib/garcon/chef/resource/attribute.rb +52 -0
- data/lib/garcon/chef/resource/base_dsl.rb +174 -0
- data/lib/garcon/chef/resource/blender.rb +140 -0
- data/lib/garcon/chef/resource/lazy_eval.rb +66 -0
- data/lib/garcon/chef/resource/resource_name.rb +109 -0
- data/lib/garcon/chef/secret_bag.rb +204 -0
- data/lib/garcon/chef/validations.rb +76 -0
- data/lib/garcon/chef_inclusions.rb +151 -0
- data/lib/garcon/configuration.rb +138 -0
- data/lib/garcon/core_ext.rb +39 -0
- data/lib/garcon/core_ext/array.rb +27 -0
- data/lib/garcon/core_ext/binding.rb +64 -0
- data/lib/garcon/core_ext/boolean.rb +66 -0
- data/lib/garcon/core_ext/duration.rb +271 -0
- data/lib/garcon/core_ext/enumerable.rb +34 -0
- data/lib/garcon/core_ext/file.rb +127 -0
- data/lib/garcon/core_ext/filetest.rb +62 -0
- data/lib/garcon/core_ext/hash.rb +279 -0
- data/lib/garcon/core_ext/kernel.rb +159 -0
- data/lib/garcon/core_ext/lazy.rb +222 -0
- data/lib/garcon/core_ext/method_access.rb +243 -0
- data/lib/garcon/core_ext/module.rb +92 -0
- data/lib/garcon/core_ext/nil.rb +53 -0
- data/lib/garcon/core_ext/numeric.rb +44 -0
- data/lib/garcon/core_ext/object.rb +342 -0
- data/lib/garcon/core_ext/pathname.rb +152 -0
- data/lib/garcon/core_ext/process.rb +41 -0
- data/lib/garcon/core_ext/random.rb +497 -0
- data/lib/garcon/core_ext/string.rb +312 -0
- data/lib/garcon/core_ext/struct.rb +49 -0
- data/lib/garcon/core_ext/symbol.rb +170 -0
- data/lib/garcon/core_ext/time.rb +234 -0
- data/lib/garcon/exceptions.rb +101 -0
- data/lib/garcon/inflections.rb +237 -0
- data/lib/garcon/inflections/defaults.rb +79 -0
- data/lib/garcon/inflections/inflections.rb +182 -0
- data/lib/garcon/inflections/rules_collection.rb +37 -0
- data/lib/garcon/secret.rb +271 -0
- data/lib/garcon/stash/format.rb +114 -0
- data/lib/garcon/stash/journal.rb +226 -0
- data/lib/garcon/stash/queue.rb +83 -0
- data/lib/garcon/stash/serializer.rb +86 -0
- data/lib/garcon/stash/store.rb +435 -0
- data/lib/garcon/task.rb +31 -0
- data/lib/garcon/task/atomic.rb +151 -0
- data/lib/garcon/task/atomic_boolean.rb +127 -0
- data/lib/garcon/task/condition.rb +99 -0
- data/lib/garcon/task/copy_on_notify_observer_set.rb +154 -0
- data/lib/garcon/task/copy_on_write_observer_set.rb +153 -0
- data/lib/garcon/task/count_down_latch.rb +92 -0
- data/lib/garcon/task/delay.rb +196 -0
- data/lib/garcon/task/dereferenceable.rb +144 -0
- data/lib/garcon/task/event.rb +119 -0
- data/lib/garcon/task/executor.rb +275 -0
- data/lib/garcon/task/executor_options.rb +59 -0
- data/lib/garcon/task/future.rb +107 -0
- data/lib/garcon/task/immediate_executor.rb +84 -0
- data/lib/garcon/task/ivar.rb +171 -0
- data/lib/garcon/task/lazy_reference.rb +74 -0
- data/lib/garcon/task/monotonic_time.rb +69 -0
- data/lib/garcon/task/obligation.rb +256 -0
- data/lib/garcon/task/observable.rb +101 -0
- data/lib/garcon/task/priority_queue.rb +234 -0
- data/lib/garcon/task/processor_count.rb +128 -0
- data/lib/garcon/task/read_write_lock.rb +304 -0
- data/lib/garcon/task/safe_task_executor.rb +58 -0
- data/lib/garcon/task/single_thread_executor.rb +97 -0
- data/lib/garcon/task/thread_pool/cached.rb +71 -0
- data/lib/garcon/task/thread_pool/executor.rb +294 -0
- data/lib/garcon/task/thread_pool/fixed.rb +61 -0
- data/lib/garcon/task/thread_pool/worker.rb +90 -0
- data/lib/garcon/task/timer.rb +44 -0
- data/lib/garcon/task/timer_set.rb +194 -0
- data/lib/garcon/task/timer_task.rb +377 -0
- data/lib/garcon/task/waitable_list.rb +58 -0
- data/lib/garcon/utility/ansi.rb +199 -0
- data/lib/garcon/utility/at_random.rb +77 -0
- data/lib/garcon/utility/crypto.rb +292 -0
- data/lib/garcon/utility/equalizer.rb +146 -0
- data/lib/garcon/utility/faker/extensions/array.rb +22 -0
- data/lib/garcon/utility/faker/extensions/symbol.rb +9 -0
- data/lib/garcon/utility/faker/faker.rb +164 -0
- data/lib/garcon/utility/faker/faker/company.rb +17 -0
- data/lib/garcon/utility/faker/faker/hacker.rb +30 -0
- data/lib/garcon/utility/faker/faker/version.rb +3 -0
- data/lib/garcon/utility/faker/locales/en-US.yml +83 -0
- data/lib/garcon/utility/faker/locales/en.yml +21 -0
- data/lib/garcon/utility/file_helper.rb +170 -0
- data/lib/garcon/utility/hookers.rb +178 -0
- data/lib/garcon/utility/interpolation.rb +90 -0
- data/lib/garcon/utility/memstash.rb +364 -0
- data/lib/garcon/utility/misc.rb +54 -0
- data/lib/garcon/utility/msg_from_god.rb +62 -0
- data/lib/garcon/utility/retry.rb +238 -0
- data/lib/garcon/utility/timeout.rb +58 -0
- data/lib/garcon/utility/uber/builder.rb +91 -0
- data/lib/garcon/utility/uber/callable.rb +7 -0
- data/lib/garcon/utility/uber/delegates.rb +13 -0
- data/lib/garcon/utility/uber/inheritable_attr.rb +37 -0
- data/lib/garcon/utility/uber/options.rb +101 -0
- data/lib/garcon/utility/uber/uber_version.rb +3 -0
- data/lib/garcon/utility/uber/version.rb +33 -0
- data/lib/garcon/utility/url_helper.rb +100 -0
- data/lib/garcon/utils.rb +29 -0
- data/lib/garcon/version.rb +62 -0
- data/lib/garcun.rb +24 -0
- metadata +680 -0
|
@@ -0,0 +1,435 @@
|
|
|
1
|
+
# encoding: UTF-8
|
|
2
|
+
#
|
|
3
|
+
# Author: Stefano Harding <riddopic@gmail.com>
|
|
4
|
+
#
|
|
5
|
+
# Copyright (C) 2014-2015 Stefano Harding
|
|
6
|
+
#
|
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
# you may not use this file except in compliance with the License.
|
|
9
|
+
# You may obtain a copy of the License at
|
|
10
|
+
#
|
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
#
|
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
# See the License for the specific language governing permissions and
|
|
17
|
+
# limitations under the License.
|
|
18
|
+
#
|
|
19
|
+
|
|
20
|
+
# _____ ______ ____ _____ __ __ __ ____ __ __ __ ___
|
|
21
|
+
# / ___/| T / T/ ___/| T T / ] / T / ]| T T / _]
|
|
22
|
+
# ( \_ | |Y o ( \_ | l | / / Y o | / / | l | / [_
|
|
23
|
+
# \__ Tl_j l_j| |\__ T| _ | / / | |/ / | _ |Y _]
|
|
24
|
+
# / \ | | | | _ |/ \ || | | / \_ | _ / \_ | | || [_
|
|
25
|
+
# \ | | | | | |\ || | | \ || | \ || | || T
|
|
26
|
+
# \___j l__j l__j__j \___jl__j__j \____jl__j__j\____jl__j__jl_____j
|
|
27
|
+
#
|
|
28
|
+
# __ __ ____ _____ __ __ _____ ______ ___ ____ ___
|
|
29
|
+
# | T T / T/ ___/| T T / ___/| T / \ | \ / _]
|
|
30
|
+
# | l |Y o ( \_ | l | ( \_ | |Y Y| D ) / [_
|
|
31
|
+
# | _ || |\__ T| _ | \__ Tl_j l_j| O || / Y _]
|
|
32
|
+
# | | || _ |/ \ || | | / \ | | | | || \ | [_
|
|
33
|
+
# | | || | |\ || | | \ | | | l !| . Y| T
|
|
34
|
+
# l__j__jl__j__j \___jl__j__j \___j l__j \___/ l__j\_jl_____j
|
|
35
|
+
#
|
|
36
|
+
|
|
37
|
+
require 'thread'
|
|
38
|
+
require 'zlib'
|
|
39
|
+
require_relative 'serializer'
|
|
40
|
+
require_relative 'format'
|
|
41
|
+
require_relative 'queue'
|
|
42
|
+
require_relative 'journal'
|
|
43
|
+
|
|
44
|
+
module Garcon
|
|
45
|
+
module Stash
|
|
46
|
+
# Stash::Store contains the public api for Stash. It includes
|
|
47
|
+
# Enumerable for functional goodies like map, each, reduce and friends.
|
|
48
|
+
#
|
|
49
|
+
# @api public
|
|
50
|
+
class Store
|
|
51
|
+
include Enumerable
|
|
52
|
+
|
|
53
|
+
# Set default value, can be a callable
|
|
54
|
+
attr_writer :default
|
|
55
|
+
|
|
56
|
+
# Create a new Stash::Store. The second argument is the default value
|
|
57
|
+
# to store when accessing a previously unset key, this follows the
|
|
58
|
+
# Hash standard.
|
|
59
|
+
#
|
|
60
|
+
# @param [String] file
|
|
61
|
+
# The path to the Stash Store file.
|
|
62
|
+
#
|
|
63
|
+
# @param [Hash] opts
|
|
64
|
+
# Options hash for creating a new stash.
|
|
65
|
+
#
|
|
66
|
+
# @option opts [Class] :serializer
|
|
67
|
+
# Serializer class
|
|
68
|
+
#
|
|
69
|
+
# @option opts [Class] :format
|
|
70
|
+
# Format class
|
|
71
|
+
#
|
|
72
|
+
# @option opts [Object] :default
|
|
73
|
+
# Default value
|
|
74
|
+
#
|
|
75
|
+
# @yield [key] a block that will return the default value to store.
|
|
76
|
+
#
|
|
77
|
+
# @yieldparam [String] key the key to be stored.
|
|
78
|
+
#
|
|
79
|
+
def initialize(file, opts = {}, &block)
|
|
80
|
+
opts = {
|
|
81
|
+
serializer: opts.fetch(:serializer, Serializer::Default),
|
|
82
|
+
format: opts.fetch(:format, Format),
|
|
83
|
+
default: opts.fetch(:default, nil)
|
|
84
|
+
}
|
|
85
|
+
@format = (opts[:format]).new
|
|
86
|
+
@serializer = (opts[:serializer]).new
|
|
87
|
+
@table = Hash.new(&method(:hash_default))
|
|
88
|
+
@journal = Journal.new(file, @format, @serializer) do |record|
|
|
89
|
+
if !record
|
|
90
|
+
@table.clear
|
|
91
|
+
elsif record.size == 1
|
|
92
|
+
@table.delete(record.first)
|
|
93
|
+
else
|
|
94
|
+
@table[record.first] = @serializer.load(record.last)
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
@default = block ? block : opts[:default]
|
|
99
|
+
@mutex = Mutex.new
|
|
100
|
+
@@stashs_mutex.synchronize { @@stashs << self }
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# Stash store file name.
|
|
104
|
+
#
|
|
105
|
+
# @return [String]
|
|
106
|
+
# stash store file name
|
|
107
|
+
#
|
|
108
|
+
def file
|
|
109
|
+
@journal.file
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# Return default value belonging to key.
|
|
113
|
+
#
|
|
114
|
+
# @param [Object] key
|
|
115
|
+
# the default value to retrieve.
|
|
116
|
+
#
|
|
117
|
+
# @return [Object]
|
|
118
|
+
# value the default value
|
|
119
|
+
#
|
|
120
|
+
def default(key = nil)
|
|
121
|
+
@table.default(@serializer.key_for(key))
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# Retrieve a value at key from the stash. If the default value was
|
|
125
|
+
# specified when this stash was created, that value will be set and
|
|
126
|
+
# returned. Aliased as `#get`.
|
|
127
|
+
#
|
|
128
|
+
# @param [Object] key
|
|
129
|
+
# the value to retrieve from the stash
|
|
130
|
+
#
|
|
131
|
+
# @return [Object]
|
|
132
|
+
# the value
|
|
133
|
+
#
|
|
134
|
+
def [](key)
|
|
135
|
+
@table[@serializer.key_for(key)]
|
|
136
|
+
end
|
|
137
|
+
alias_method :get, '[]'
|
|
138
|
+
|
|
139
|
+
# Set a key in the stash to be written at some future date. If the data
|
|
140
|
+
# needs to be persisted immediately, call `#store.set(key, value, true)`.
|
|
141
|
+
#
|
|
142
|
+
# @param [Object] key
|
|
143
|
+
# the key of the storage slot in the stash
|
|
144
|
+
#
|
|
145
|
+
# @param [Object] value
|
|
146
|
+
# the value to store
|
|
147
|
+
#
|
|
148
|
+
# @return [Object]
|
|
149
|
+
# the value
|
|
150
|
+
#
|
|
151
|
+
def []=(key, value)
|
|
152
|
+
key = @serializer.key_for(key)
|
|
153
|
+
@journal << [key, value]
|
|
154
|
+
@table[key] = value
|
|
155
|
+
end
|
|
156
|
+
alias_method :set, '[]='
|
|
157
|
+
|
|
158
|
+
# Flushes data immediately to disk.
|
|
159
|
+
#
|
|
160
|
+
# @param [Object] key
|
|
161
|
+
# the key of the storage slot in the stash
|
|
162
|
+
#
|
|
163
|
+
# @param [Object] value
|
|
164
|
+
# the value to store
|
|
165
|
+
#
|
|
166
|
+
# @return [Object]
|
|
167
|
+
# the value
|
|
168
|
+
#
|
|
169
|
+
def set!(key, value)
|
|
170
|
+
set(key, value)
|
|
171
|
+
flush
|
|
172
|
+
value
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
# Delete a key from the stash.
|
|
176
|
+
#
|
|
177
|
+
# @param [Object] key
|
|
178
|
+
# the key of the storage slot in the stash
|
|
179
|
+
#
|
|
180
|
+
# @return [Object]
|
|
181
|
+
# the value
|
|
182
|
+
#
|
|
183
|
+
def delete(key)
|
|
184
|
+
key = @serializer.key_for(key)
|
|
185
|
+
@journal << [key]
|
|
186
|
+
@table.delete(key)
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
# Immediately delete the key on disk.
|
|
190
|
+
#
|
|
191
|
+
# @param [Object] key
|
|
192
|
+
# the key of the storage slot in the stash
|
|
193
|
+
#
|
|
194
|
+
# @return [Object]
|
|
195
|
+
# the value
|
|
196
|
+
#
|
|
197
|
+
def delete!(key)
|
|
198
|
+
value = delete(key)
|
|
199
|
+
flush
|
|
200
|
+
value
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
# Update stash with hash (fast batch update).
|
|
204
|
+
#
|
|
205
|
+
# @param [Hash] hash
|
|
206
|
+
# the key/value hash
|
|
207
|
+
#
|
|
208
|
+
# @return [Stash] self
|
|
209
|
+
#
|
|
210
|
+
def update(hash)
|
|
211
|
+
shash = {}
|
|
212
|
+
hash.each { |key, value| shash[@serializer.key_for(key)] = value }
|
|
213
|
+
@journal << shash
|
|
214
|
+
@table.update(shash)
|
|
215
|
+
self
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
# Updata stash and flush data to disk.
|
|
219
|
+
#
|
|
220
|
+
# @param [Hash] hash
|
|
221
|
+
# the key/value hash
|
|
222
|
+
#
|
|
223
|
+
# @return [Stash] self
|
|
224
|
+
#
|
|
225
|
+
def update!(hash)
|
|
226
|
+
update(hash)
|
|
227
|
+
@journal.flush
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
# Does this stash have this key?
|
|
231
|
+
#
|
|
232
|
+
#
|
|
233
|
+
# @param [Object] key
|
|
234
|
+
# the key to check if the stash has it
|
|
235
|
+
#
|
|
236
|
+
# @return [Boolean]
|
|
237
|
+
#
|
|
238
|
+
def has_key?(key)
|
|
239
|
+
@table.has_key?(@serializer.key_for(key))
|
|
240
|
+
end
|
|
241
|
+
alias_method :key?, :has_key?
|
|
242
|
+
alias_method :include?, :has_key?
|
|
243
|
+
alias_method :member?, :has_key?
|
|
244
|
+
|
|
245
|
+
# Does this stash have this value?
|
|
246
|
+
#
|
|
247
|
+
# @param [Object] value
|
|
248
|
+
# the value to check if the Stash has it
|
|
249
|
+
#
|
|
250
|
+
# @return [Boolean]
|
|
251
|
+
#
|
|
252
|
+
def has_value?(value)
|
|
253
|
+
@table.has_value?(value)
|
|
254
|
+
end
|
|
255
|
+
alias_method :value?, :has_value?
|
|
256
|
+
|
|
257
|
+
# Return the number of stored items.
|
|
258
|
+
#
|
|
259
|
+
# @return [Fixnum]
|
|
260
|
+
#
|
|
261
|
+
def size
|
|
262
|
+
@table.size
|
|
263
|
+
end
|
|
264
|
+
alias_method :length, :size
|
|
265
|
+
|
|
266
|
+
# Utility method that will return the size of the stash in bytes, useful
|
|
267
|
+
# for determining when to compact.
|
|
268
|
+
#
|
|
269
|
+
# @return [Fixnum]
|
|
270
|
+
#
|
|
271
|
+
def bytesize
|
|
272
|
+
@journal.bytesize
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
# Counter of how many records are in the journal.
|
|
276
|
+
#
|
|
277
|
+
# @return [Fixnum]
|
|
278
|
+
#
|
|
279
|
+
def logsize
|
|
280
|
+
@journal.size
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
# Return true if stash is empty.
|
|
284
|
+
#
|
|
285
|
+
# @return [Boolean]
|
|
286
|
+
#
|
|
287
|
+
def empty?
|
|
288
|
+
@table.empty?
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
# Iterate over the key, value pairs in the stash.
|
|
292
|
+
#
|
|
293
|
+
# @yield [key, value] block
|
|
294
|
+
# the iterator for each key value pair
|
|
295
|
+
#
|
|
296
|
+
# @yieldparam key the key.
|
|
297
|
+
#
|
|
298
|
+
# @yieldparam value the value from the stash.
|
|
299
|
+
#
|
|
300
|
+
def each(&block)
|
|
301
|
+
@table.each(&block)
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
# Return the keys in the stash.
|
|
305
|
+
#
|
|
306
|
+
# @return [Array<String>]
|
|
307
|
+
#
|
|
308
|
+
def keys
|
|
309
|
+
@table.keys
|
|
310
|
+
end
|
|
311
|
+
|
|
312
|
+
# Flush all changes to disk.
|
|
313
|
+
#
|
|
314
|
+
# @return [Stash] self
|
|
315
|
+
#
|
|
316
|
+
def flush
|
|
317
|
+
@journal.flush
|
|
318
|
+
self
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
# Sync the stash with what is on disk, by first flushing changes, and then
|
|
322
|
+
# loading the new records if necessary.
|
|
323
|
+
#
|
|
324
|
+
# @return [Stash] self
|
|
325
|
+
#
|
|
326
|
+
def load
|
|
327
|
+
@journal.load
|
|
328
|
+
self
|
|
329
|
+
end
|
|
330
|
+
alias_method :sunrise, :load
|
|
331
|
+
|
|
332
|
+
# Lock the stash for an exclusive commit across processes and threads.
|
|
333
|
+
# @note This method performs an expensive locking over process boundaries.
|
|
334
|
+
# If you want to synchronize only between threads, use `#synchronize`.
|
|
335
|
+
# @see #synchronize
|
|
336
|
+
#
|
|
337
|
+
# @yield a block where every change to the stash is synced
|
|
338
|
+
# @yieldparam [Stash] stash
|
|
339
|
+
# @return result of the block
|
|
340
|
+
#
|
|
341
|
+
def lock
|
|
342
|
+
synchronize { @journal.lock { yield self } }
|
|
343
|
+
end
|
|
344
|
+
|
|
345
|
+
# Synchronize access to the stash from multiple threads.
|
|
346
|
+
# @note Stash is not thread safe, if you want to access it from multiple
|
|
347
|
+
# threads, all accesses have to be in the #synchronize block.
|
|
348
|
+
# @see #lock
|
|
349
|
+
#
|
|
350
|
+
# @yield a block where every change to the stash is synced
|
|
351
|
+
#
|
|
352
|
+
# @yieldparam [Stash] stash
|
|
353
|
+
#
|
|
354
|
+
# @return result of the block
|
|
355
|
+
#
|
|
356
|
+
def synchronize
|
|
357
|
+
@mutex.synchronize { yield self }
|
|
358
|
+
end
|
|
359
|
+
|
|
360
|
+
# Remove all keys and values from the stash.
|
|
361
|
+
#
|
|
362
|
+
# @return [Stash] self
|
|
363
|
+
#
|
|
364
|
+
def clear
|
|
365
|
+
@table.clear
|
|
366
|
+
@journal.clear
|
|
367
|
+
self
|
|
368
|
+
end
|
|
369
|
+
|
|
370
|
+
# Compact the stash to remove stale commits and reduce the file size.
|
|
371
|
+
#
|
|
372
|
+
# @return [Stash] self
|
|
373
|
+
#
|
|
374
|
+
def compact
|
|
375
|
+
@journal.compact { @table }
|
|
376
|
+
self
|
|
377
|
+
end
|
|
378
|
+
|
|
379
|
+
# Close the stash for reading and writing.
|
|
380
|
+
#
|
|
381
|
+
# @return nil
|
|
382
|
+
#
|
|
383
|
+
def close
|
|
384
|
+
@journal.close
|
|
385
|
+
@@stashs_mutex.synchronize { @@stashs.delete(self) }
|
|
386
|
+
nil
|
|
387
|
+
end
|
|
388
|
+
|
|
389
|
+
# Check to see if we've already closed the stash.
|
|
390
|
+
#
|
|
391
|
+
# @return [Boolean]
|
|
392
|
+
#
|
|
393
|
+
def closed?
|
|
394
|
+
@journal.closed?
|
|
395
|
+
end
|
|
396
|
+
|
|
397
|
+
private # P R O P R I E T À P R I V A T A Vietato L'accesso
|
|
398
|
+
|
|
399
|
+
# @private
|
|
400
|
+
@@stashs = []
|
|
401
|
+
|
|
402
|
+
# @private
|
|
403
|
+
@@stashs_mutex = Mutex.new
|
|
404
|
+
|
|
405
|
+
# A handler that will ensure that stashs are closed and synced when the
|
|
406
|
+
# current process exits.
|
|
407
|
+
#
|
|
408
|
+
# @private
|
|
409
|
+
def self.exit_handler
|
|
410
|
+
loop do
|
|
411
|
+
stash = @@stashs_mutex.synchronize { @@stashs.shift }
|
|
412
|
+
break unless stash
|
|
413
|
+
warn "Stash #{stash.file} was not closed, state might be inconsistent"
|
|
414
|
+
begin
|
|
415
|
+
stash.close
|
|
416
|
+
rescue Exception => e
|
|
417
|
+
warn "Failed to close stash store: #{e.message}"
|
|
418
|
+
end
|
|
419
|
+
end
|
|
420
|
+
end
|
|
421
|
+
|
|
422
|
+
at_exit { Garcon::Stash::Store.exit_handler }
|
|
423
|
+
|
|
424
|
+
# The block used in @table for new records.
|
|
425
|
+
#
|
|
426
|
+
def hash_default(_, key)
|
|
427
|
+
if @default != nil
|
|
428
|
+
value = @default.respond_to?(:call) ? @default.call(key) : @default
|
|
429
|
+
@journal << [key, value]
|
|
430
|
+
@table[key] = value
|
|
431
|
+
end
|
|
432
|
+
end
|
|
433
|
+
end
|
|
434
|
+
end
|
|
435
|
+
end
|
data/lib/garcon/task.rb
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# encoding: UTF-8
|
|
2
|
+
#
|
|
3
|
+
# Author: Stefano Harding <riddopic@gmail.com>
|
|
4
|
+
# License: Apache License, Version 2.0
|
|
5
|
+
# Copyright: (C) 2014-2015 Stefano Harding
|
|
6
|
+
#
|
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
# you may not use this file except in compliance with the License.
|
|
9
|
+
# You may obtain a copy of the License at
|
|
10
|
+
#
|
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
#
|
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
# See the License for the specific language governing permissions and
|
|
17
|
+
# limitations under the License.
|
|
18
|
+
#
|
|
19
|
+
|
|
20
|
+
require_relative 'task/atomic_boolean'
|
|
21
|
+
require_relative 'task/condition'
|
|
22
|
+
require_relative 'task/count_down_latch'
|
|
23
|
+
require_relative 'task/event'
|
|
24
|
+
require_relative 'task/future'
|
|
25
|
+
require_relative 'task/lazy_reference'
|
|
26
|
+
require_relative 'task/read_write_lock'
|
|
27
|
+
require_relative 'task/observable'
|
|
28
|
+
require_relative 'task/processor_count'
|
|
29
|
+
require_relative 'task/thread_pool/cached'
|
|
30
|
+
require_relative 'task/thread_pool/fixed'
|
|
31
|
+
require_relative 'task/timer_set'
|