easy-serve 0.11 → 0.12

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/easy-serve.rb +52 -22
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1ef2c07a293530b91eacbd1630d63507acddedab
4
- data.tar.gz: 004b52c5e9f3399f60a882f6302b648e900c8afe
3
+ metadata.gz: 5c1e18a947084b2aa2f1508159eef9d102f90d0f
4
+ data.tar.gz: ae49fefd0aa53336007d2745d0f5471970950aac
5
5
  SHA512:
6
- metadata.gz: 6ab412543120bca229865bff3b1f4a66bca28ec4bef4336feacd5c0c392a04a7a9672a6452c93539d49f6344ae8e642c3ed46b92c9773b81a2f0f1dbfa423f88
7
- data.tar.gz: ec3d64c1e104eae2f79ed87fb918e992d3bf25ead58374a55cf38da332421b7e8c84c16e2007ca7e8913bd0ad50c5887ad5b47cabc6347bc967c617f72ce60fe
6
+ metadata.gz: b4d08db27f7c8240213ac5934cfafa08bb595b6dbea4402e09b09102f0b84133ef018f912868349166940d9c7c74b45fc3cc3b0acccb97372c6bb4814434d87f
7
+ data.tar.gz: e1551d47c03786400d81a0e949fd94cc18e153faebc58345e3d540a40db9950b372903135b534cf040ef2471aa0a0b97b6d9b89dee4d7e6f19962b7dcef86fb8
@@ -6,8 +6,10 @@ require 'fileutils'
6
6
  require 'easy-serve/service'
7
7
 
8
8
  class EasyServe
9
- VERSION = "0.11"
10
-
9
+ VERSION = "0.12"
10
+
11
+ class ServicesExistError < RuntimeError; end
12
+
11
13
  class EasyFormatter < Logger::Formatter
12
14
  Format = "%s: %s: %s\n"
13
15
 
@@ -70,6 +72,7 @@ class EasyServe
70
72
  #
71
73
  def initialize **opts
72
74
  @services_file = opts[:services_file]
75
+ @created_services_file = false
73
76
  @interactive = opts[:interactive]
74
77
  @log = opts[:log] || self.class.null_logger
75
78
  @children = [] # pid
@@ -148,7 +151,7 @@ class EasyServe
148
151
  service.cleanup
149
152
  end
150
153
 
151
- if services_file
154
+ if @created_services_file
152
155
  begin
153
156
  FileUtils.rm services_file
154
157
  rescue Errno::ENOENT
@@ -161,32 +164,59 @@ class EasyServe
161
164
  end
162
165
 
163
166
  def start_services
164
- if @owner
165
- if services_file
166
- begin
167
- FileUtils.ln_s services_file, ".lock.#{services_file}"
168
- # Successful creation of the lock file gives this process the
169
- # right to create the services_file itself.
170
- rescue Errno::EEXIST
171
- raise Errno::EEXIST, "Services at #{services_file} already exist."
172
- end
167
+ return unless @owner
168
+
169
+ if not services_file
170
+ log.debug {"starting services without services_file"}
171
+ yield
172
+ return
173
+ end
174
+
175
+ lock_success = with_lock_file *File.split(services_file) do
176
+ # Successful creation of the lock file gives this process the
177
+ # right to check and create the services_file itself.
178
+
179
+ if File.exist? services_file
180
+ raise ServicesExistError,
181
+ "Services at #{services_file.inspect} already exist."
173
182
  end
174
183
 
175
- log.debug {"starting services"}
184
+ log.debug {"starting services stored in #{services_file.inspect}"}
176
185
  yield
177
186
 
178
- if services_file
179
- begin
180
- File.open(services_file, "w") do |f|
181
- YAML.dump(services, f)
182
- end
183
- ensure
184
- FileUtils.rm_f ".lock.#{services_file}"
185
- end
187
+ tmp = services_file + ".tmp"
188
+ File.open(tmp, "w") do |f|
189
+ YAML.dump(services, f)
186
190
  end
191
+ FileUtils.mv(tmp, services_file)
192
+ @created_services_file = true
193
+ end
194
+
195
+ unless lock_success
196
+ raise ServicesExistError,
197
+ "Services at #{services_file.inspect} are being created."
187
198
  end
188
199
  end
189
-
200
+
201
+ # Returns true if this process got the lock.
202
+ def with_lock_file dir, base
203
+ lock_file = File.join(dir, ".lock.#{base}")
204
+
205
+ begin
206
+ FileUtils.ln_s ".#{Process.pid}.#{base}", lock_file
207
+ rescue Errno::EEXIST
208
+ return false
209
+ end
210
+
211
+ begin
212
+ yield
213
+ ensure
214
+ FileUtils.rm_f lock_file
215
+ end
216
+
217
+ true
218
+ end
219
+
190
220
  def tmpdir
191
221
  @tmpdir ||= begin
192
222
  require 'tmpdir'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy-serve
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.11'
4
+ version: '0.12'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel VanderWerf
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-23 00:00:00.000000000 Z
11
+ date: 2014-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: msgpack