From 50191be1cf2a590fd3ab9b11443af1a05986c72e Mon Sep 17 00:00:00 2001 From: Michoel Samuels Date: Sat, 30 Jun 2018 23:10:43 +0300 Subject: [PATCH 1/7] Fix spelling --- lib/filequeue.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/filequeue.rb b/lib/filequeue.rb index 70715e6..9d75bf2 100644 --- a/lib/filequeue.rb +++ b/lib/filequeue.rb @@ -69,7 +69,7 @@ def lock(file) Thread.pass end - (raise FileLockError, "Queue file appears to be permanently lockecd") unless lock_acquired + (raise FileLockError, "Queue file appears to be permanently locked") unless lock_acquired end FileLockError = Class.new(Timeout::Error) From 257814e0d6b84e5390262b5e894f49b10b611645 Mon Sep 17 00:00:00 2001 From: Michoel Samuels Date: Sat, 30 Jun 2018 23:11:12 +0300 Subject: [PATCH 2/7] Add docs to readme --- README.md | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 304ecdd..b8b0c72 100644 --- a/README.md +++ b/README.md @@ -11,16 +11,28 @@ Originally written by [daddz](http://www.github.com/daddz) and found in [this gi ## Usage - queue = FileQueue.new 'queue_party.txt' - - queue.push "an item" - => true - - queue.pop - => "an item" +```ruby +queue = FileQueue.new 'queue_party.txt' + +queue.push "an item" +# => true +queue.pop +# => "an item" +``` + See `spec/filequeue_spec.rb` for more usage details +## Docs + +`FileQueue` has the following class methods: + +* `push` (alias `<<`) +* `pop` +* `length` +* `empty?` +* `clear` + ## Continuous Integration [![Build Status](http://travis-ci.org/maxogden/filequeue.png)](http://travis-ci.org/maxogden/filequeue) From 4d7ec49dd4ac792db6879c9fdeec4772bb327e49 Mon Sep 17 00:00:00 2001 From: Michoel Samuels Date: Sat, 30 Jun 2018 23:11:36 +0300 Subject: [PATCH 3/7] Remove duplicate CI badge --- README.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/README.md b/README.md index b8b0c72..34b01f9 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,3 @@ See `spec/filequeue_spec.rb` for more usage details * `length` * `empty?` * `clear` - -## Continuous Integration - -[![Build Status](http://travis-ci.org/maxogden/filequeue.png)](http://travis-ci.org/maxogden/filequeue) From 07a27949dc0d8a4ea0eca3670975d94dfd806f0d Mon Sep 17 00:00:00 2001 From: Michoel Samuels Date: Sat, 30 Jun 2018 23:24:49 +0300 Subject: [PATCH 4/7] Switch file locking to the Filelock gem --- Gemfile | 2 ++ Gemfile.lock | 4 +++- lib/filequeue.rb | 21 +++++++++------------ 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/Gemfile b/Gemfile index f30c9c4..5913643 100644 --- a/Gemfile +++ b/Gemfile @@ -2,3 +2,5 @@ source 'https://rubygems.org' # Specify your gem's dependencies in filequeue.gemspec gemspec + +gem 'filelock' diff --git a/Gemfile.lock b/Gemfile.lock index fc3999b..c9afbec 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -7,6 +7,7 @@ GEM remote: https://rubygems.org/ specs: diff-lcs (1.2.5) + filelock (1.1.1) rake (10.4.2) rspec (3.4.0) rspec-core (~> 3.4.0) @@ -26,9 +27,10 @@ PLATFORMS ruby DEPENDENCIES + filelock filequeue! rake rspec (~> 3.3) BUNDLED WITH - 1.10.6 + 1.16.1 diff --git a/lib/filequeue.rb b/lib/filequeue.rb index 9d75bf2..2e72b47 100644 --- a/lib/filequeue.rb +++ b/lib/filequeue.rb @@ -1,4 +1,5 @@ require 'timeout' +require 'filelock' class FileQueue attr_accessor :file_name, :delimiter @@ -52,25 +53,21 @@ def clear def safe_open(mode) File.open(@file_name, mode) do |file| - lock file + + lock file do yield file + end + end end # Locks the queue file for exclusive access. # - # Raises `FileLockError` if unable to acquire a lock. - # - # Return is undefined. + # Raises `Filelock::WaitTimeout` if unable to acquire a lock. + # Default timeout is 1 day def lock(file) - tries = 1000 - until tries == 0 || lock_acquired = file.flock(File::LOCK_NB|File::LOCK_EX) - tries -= 1 - Thread.pass + Filelock file do + yield file end - - (raise FileLockError, "Queue file appears to be permanently locked") unless lock_acquired end - - FileLockError = Class.new(Timeout::Error) end From 44830bd438b1b3d79fe571cbe05732e03e8bc454 Mon Sep 17 00:00:00 2001 From: Michoel Samuels Date: Sat, 30 Jun 2018 23:32:37 +0300 Subject: [PATCH 5/7] Tweak readme --- README.md | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 34b01f9..d1bd8c3 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ ## FileQueue [![Build Status](https://travis-ci.org/pezra/filequeue.svg)](https://travis-ci.org/pezra/filequeue) -...is a simple file based queue written in Ruby that uses the Ruby `File` class in standard library to push and pop items into a queue. It's not web scale but is nice for lightweight async queuing apps. - -Originally written by [daddz](http://www.github.com/daddz) and found in [this gist](https://gist.github.com/352509). Thanks, daddz! +A simple file based queue written in Ruby that uses the battle-tested [`Filelock`](https://github.com/sheerun/filelock) gem to push and pop items into a queue. It's not web scale but is nice for lightweight async queuing apps. ## Install - gem install filequeue +```ruby +gem install filequeue +``` ## Usage @@ -32,3 +32,8 @@ See `spec/filequeue_spec.rb` for more usage details * `length` * `empty?` * `clear` + +## Authorship + +* Origially written by [daddz](http://www.github.com/daddz) and found in [this gist](https://gist.github.com/352509). +* Ported to a Rubygem by [@pezra](https://github.com/pezra). \ No newline at end of file From a923313d266dcba71f5159bf228732164903e6de Mon Sep 17 00:00:00 2001 From: Michoel Samuels Date: Sat, 30 Jun 2018 23:33:43 +0300 Subject: [PATCH 6/7] Cleanup spaces in readme --- README.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index d1bd8c3..72f426d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,5 @@ +# FileQueue [![Build Status](https://travis-ci.org/pezra/filequeue.svg)](https://travis-ci.org/pezra/filequeue) - -## FileQueue [![Build Status](https://travis-ci.org/pezra/filequeue.svg)](https://travis-ci.org/pezra/filequeue) A simple file based queue written in Ruby that uses the battle-tested [`Filelock`](https://github.com/sheerun/filelock) gem to push and pop items into a queue. It's not web scale but is nice for lightweight async queuing apps. ## Install @@ -15,10 +14,10 @@ gem install filequeue queue = FileQueue.new 'queue_party.txt' queue.push "an item" -# => true - +# => true + queue.pop -# => "an item" +# => "an item" ``` See `spec/filequeue_spec.rb` for more usage details From 24af619480371c30ec29c0aa2343da15dd778eaa Mon Sep 17 00:00:00 2001 From: Michoel Samuels Date: Sat, 30 Jun 2018 23:41:47 +0300 Subject: [PATCH 7/7] Add the new dependency in README --- README.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 72f426d..38f80c9 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,16 @@ See `spec/filequeue_spec.rb` for more usage details * `empty?` * `clear` +## Dependencies + +Locking is delegated to the excellent [`Filelock`](https://github.com/sheerun/filelock) gem. +All of the `Filelock`'s [caveats](https://github.com/sheerun/filelock#faq) apply. + +## Limitations + +* NFS is [not supported](https://github.com/sheerun/filelock#faq) + ## Authorship * Origially written by [daddz](http://www.github.com/daddz) and found in [this gist](https://gist.github.com/352509). -* Ported to a Rubygem by [@pezra](https://github.com/pezra). \ No newline at end of file +* Ported to a Rubygem by [@pezra](https://github.com/pezra).