filesystem_queue 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b9e3ab5ddf19ea8ef07cf949fa4e273c5498c3c71922318be16619f788a648dc
4
- data.tar.gz: 51f0f96c5299d04c3777fff7f20fbfa2ec476923464f19fed4e8a11b53f4abbc
3
+ metadata.gz: 2e960414c06a8d0a9fe7d7fc0b8b1ed90cf604d7648ab74c64f924648a137e94
4
+ data.tar.gz: 06f0a8e59cbb4c64a2de4af00360b317bb13e8a136f3d1647ff802b8b7617939
5
5
  SHA512:
6
- metadata.gz: b1999fc8c23de8854d08aadc53993dc1fb128ad8dfd1a682cea44fb7315c5939ac75341d05c8ea2b9eac41820c51e422193924c422ece9dd9c01f109f0caf20e
7
- data.tar.gz: 191ad7aa123dcd9cdde4056f820190eeff45204e36fda24ca3c97d0029751b1b63b06f44832e485915903c0bb3a81afe12040330b44ca8897f35f1929155d15b
6
+ metadata.gz: 6a9d6f4d5c55313ed7af2bc85245c019f97a66a691a4511c67ed78a53e74fa91f6574e513ebaf1e288aaeeb74cca74bbb53de289c294ff4ad781c496a9e4edf1
7
+ data.tar.gz: a71491c4ac7e6f46b8834ef9ca7f1f7534216d6036c4e98877c2f0ae34a050202699fb471b9886a51f6fb61fa3359aa297980f4debc74e93db90108adbf8f756
data/.rubocop.yml CHANGED
@@ -3,7 +3,6 @@ AllCops:
3
3
 
4
4
  Style/StringLiterals:
5
5
  Enabled: true
6
- EnforcedStyle: double_quotes
7
6
 
8
7
  Style/StringLiteralsInInterpolation:
9
8
  Enabled: true
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.0] - 2024-09-16
4
+ - Replaced old index for an in-memory index for job tracking
5
+ - Added `cleanup` method to delete files and directories created by the queue
6
+ ## [0.1.1] - 2024-09-13
7
+ - Added unit tests
8
+ - Fixed Github Actions
9
+ - Minor refactors
3
10
  ## [0.1.0] - 2024-09-12
4
-
5
11
  - Initial release
data/Gemfile CHANGED
@@ -1,12 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- source "https://rubygems.org"
3
+ source 'https://rubygems.org'
4
4
 
5
5
  # Specify your gem's dependencies in filesystem_queue.gemspec
6
6
  gemspec
7
7
 
8
- gem "rake", "~> 13.0"
8
+ gem 'rake', '~> 13.0'
9
9
 
10
- gem "minitest", "~> 5.0"
10
+ gem 'minitest', '~> 5.0'
11
11
 
12
- gem "rubocop", "~> 1.21"
12
+ gem 'rubocop', '~> 1.21'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- filesystem_queue (0.1.0)
4
+ filesystem_queue (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -35,6 +35,7 @@ GEM
35
35
 
36
36
  PLATFORMS
37
37
  x86_64-darwin-19
38
+ x86_64-linux
38
39
 
39
40
  DEPENDENCIES
40
41
  filesystem_queue!
data/README.md CHANGED
@@ -1,18 +1,34 @@
1
1
  # FilesystemQueue
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/filesystem_queue.svg)](https://badge.fury.io/rb/filesystem_queue)
4
+
3
5
  `FileSystemQueue` is a persistent queue system based on the local filesystem. It allows you to enqueue and dequeue jobs, and keeps track of completed and failed jobs.
4
6
 
5
- ## Installation
6
7
 
7
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
8
+ ## What is this for?
9
+
10
+ `FileSystemQueue` is designed for applications that need a simple, embedded queue system with minimal footprint. It consists of just a single class and utilizes your local filesystem for storage. This makes it an excellent choice for lightweight applications or development environments where setting up a full-fledged queue service is overkill. If you are looking to scale up your queue into a service architecture, Ruby offers other solutions like Resque (Redis-based), or various suites from cloud providers such as AWS-SQS.
11
+
12
+ ### Features
13
+
14
+ - **Persistence**: Jobs are stored on the filesystem, ensuring they are not lost between application restarts.
15
+ - **Minimal Overhead**: No need for external dependencies or services.
16
+ - **Ease of Use**: Simple API for enqueuing and dequeuing jobs.
17
+ - **In-Memory Index**: Uses an in-memory index for tracking jobs, improving performance by reducing file I/O operations.
18
+ - **Cleanup Method**: Provides a method to delete all files and directories created by the queue.
19
+ - **Queue Size Methods**: Includes methods to get the size of the queue and the number of failed jobs.
20
+ - **Tailored for Local Use**: This system is optimized for local use with a single consumer. If you are looking for a multiple consumer approach you should be looking for larger distributed solutions such as Resque or other distributed Queue systems.
21
+ - **Job Management**: Once a job is dequeued, it's your responsabilty to flag it as `Completed` or `Failed`. Make sure you have fault tolerant logic in your worker to ensure this step. Completed and failed jobs are tracked in separate folders for easy management.
22
+
23
+ ## Installation
8
24
 
9
25
  Install the gem and add to the application's Gemfile by executing:
10
26
 
11
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
27
+ $ bundle add filesystem_queue
12
28
 
13
29
  If bundler is not being used to manage dependencies, install the gem by executing:
14
30
 
15
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
31
+ $ gem install filesystem_queue
16
32
 
17
33
  ## Methods
18
34
 
@@ -103,6 +119,9 @@ puts 'Queue is empty'
103
119
  # Check on failed jobs
104
120
  puts "Queue size: #{queue.size}"
105
121
  puts "Failed jobs: #{queue.failed_size}"
122
+
123
+ # Cleanup all files and directories created by the queue
124
+ queue.cleanup
106
125
  ```
107
126
 
108
127
  ## Error Handling
data/Rakefile CHANGED
@@ -1,15 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "bundler/gem_tasks"
4
- require "rake/testtask"
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
5
 
6
6
  Rake::TestTask.new(:test) do |t|
7
- t.libs << "test"
8
- t.libs << "lib"
9
- t.test_files = FileList["test/**/test_*.rb"]
7
+ t.libs << 'test'
8
+ t.libs << 'lib'
9
+ t.test_files = FileList['test/**/test_*.rb']
10
10
  end
11
11
 
12
- require "rubocop/rake_task"
12
+ require 'rubocop/rake_task'
13
13
 
14
14
  RuboCop::RakeTask.new
15
15
 
@@ -1,24 +1,25 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "lib/filesystem_queue/version"
3
+ require_relative 'lib/filesystem_queue/version'
4
4
 
5
5
  Gem::Specification.new do |spec|
6
- spec.name = "filesystem_queue"
6
+ spec.name = 'filesystem_queue'
7
7
  spec.version = FilesystemQueue::VERSION
8
- spec.authors = ["Carlos Westman"]
9
- spec.email = ["carloswestman@gmail.com"]
8
+ spec.authors = ['Carlos Westman']
9
+ spec.email = ['carloswestman@gmail.com']
10
10
 
11
- spec.summary = "A persistent queue system based on the local filesystem"
12
- spec.description = "FilesystemQueue is a Ruby gem that provides a persistent queue system using the local filesystem. It allows you to enqueue and dequeue jobs, and keeps track of completed and failed jobs."
13
- spec.homepage = "https://github.com/carloswestman/filesystem_queue"
14
- spec.license = "MIT"
15
- spec.required_ruby_version = ">= 2.6.0"
11
+ spec.summary = 'A persistent queue system based on the local filesystem'
12
+ spec.description = 'FilesystemQueue is a Ruby gem that provides a persistent queue system '\
13
+ 'using the local filesystem.It allows you to enqueue and dequeue jobs, and keeps track of completed and failed jobs.'
14
+ spec.homepage = 'https://github.com/carloswestman/filesystem_queue'
15
+ spec.license = 'MIT'
16
+ spec.required_ruby_version = '>= 2.6.0'
16
17
 
17
- spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
18
19
 
19
- spec.metadata["homepage_uri"] = spec.homepage
20
- spec.metadata["source_code_uri"] = spec.homepage
21
- spec.metadata["changelog_uri"] = "https://github.com/carloswestman/filesystem_queue/blob/main/CHANGELOG.md"
20
+ spec.metadata['homepage_uri'] = spec.homepage
21
+ spec.metadata['source_code_uri'] = spec.homepage
22
+ spec.metadata['changelog_uri'] = 'https://github.com/carloswestman/filesystem_queue/blob/main/CHANGELOG.md'
22
23
 
23
24
  # Specify which files should be added to the gem when it is released.
24
25
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
@@ -27,7 +28,7 @@ Gem::Specification.new do |spec|
27
28
  (File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
28
29
  end
29
30
  end
30
- spec.bindir = "exe"
31
+ spec.bindir = 'exe'
31
32
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
- spec.require_paths = ["lib"]
33
+ spec.require_paths = ['lib']
33
34
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FilesystemQueue
4
- VERSION = "0.1.0"
4
+ VERSION = '0.2.0'
5
5
  end
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "json"
4
- require "fileutils"
5
- require_relative "filesystem_queue/version"
3
+ require 'json'
4
+ require 'fileutils'
5
+ require_relative 'filesystem_queue/version'
6
6
 
7
7
  module FilesystemQueue
8
8
  class Error < StandardError; end
@@ -12,37 +12,29 @@ module FilesystemQueue
12
12
  class Queue
13
13
  def initialize(queue_dir)
14
14
  @queue_dir = queue_dir
15
- @jobs_dir = File.join(@queue_dir, "jobs")
16
- @completed_dir = File.join(@queue_dir, "completed")
17
- @failed_dir = File.join(@queue_dir, "failed")
18
- @index_file = File.join(@queue_dir, "index.txt")
15
+ @jobs_dir = File.join(@queue_dir, 'jobs')
16
+ @completed_dir = File.join(@queue_dir, 'completed')
17
+ @failed_dir = File.join(@queue_dir, 'failed')
19
18
 
20
19
  [@jobs_dir, @completed_dir, @failed_dir].each do |dir|
21
20
  FileUtils.mkdir_p(dir) unless Dir.exist?(dir)
22
21
  end
23
- FileUtils.touch(@index_file) unless File.exist?(@index_file)
22
+
23
+ @index = rebuild_index
24
24
  end
25
25
 
26
26
  def enqueue(job)
27
27
  timestamp = Time.now.to_f.to_s
28
28
  job_file = File.join(@jobs_dir, "job_#{timestamp}.json")
29
29
  File.write(job_file, job.to_json)
30
- File.open(@index_file, "a") { |f| f.puts(job_file) }
30
+ @index << job_file
31
31
  end
32
32
 
33
33
  def dequeue
34
- job_file = nil
35
- File.open(@index_file, "r+") do |f|
36
- lines = f.each_line.to_a
37
- return nil if lines.empty?
38
-
39
- job_file = lines.shift.strip
40
- f.rewind
41
- f.write(lines.join)
42
- f.truncate(f.pos)
43
- end
34
+ return nil if @index.empty?
44
35
 
45
- return nil unless job_file && File.exist?(job_file)
36
+ job_file = @index.shift
37
+ return nil unless File.exist?(job_file)
46
38
 
47
39
  job_data = JSON.parse(File.read(job_file), symbolize_names: true)
48
40
  [job_file, job_data]
@@ -57,19 +49,30 @@ module FilesystemQueue
57
49
  end
58
50
 
59
51
  def size
60
- File.readlines(@index_file).size
52
+ @index.size
61
53
  end
62
54
 
63
55
  def failed_size
64
- Dir[File.join(@failed_dir, "*")].count { |file| File.file?(file) }
56
+ Dir[File.join(@failed_dir, '*')].count { |file| File.file?(file) }
57
+ end
58
+
59
+ # CAUTION: Cleanup the queue directory, removing all files and directories
60
+ def cleanup
61
+ [@jobs_dir, @completed_dir, @failed_dir].each do |dir|
62
+ FileUtils.rm_rf(dir)
63
+ end
64
+ FileUtils.rm_rf(@queue_dir)
65
65
  end
66
66
 
67
67
  private
68
68
 
69
+ def rebuild_index
70
+ Dir.glob(File.join(@jobs_dir, '*.json')).sort
71
+ end
72
+
69
73
  def move_job(job_file, target_dir)
70
74
  FileUtils.mv(job_file, target_dir)
71
- rescue StandardError => e
72
- puts "Failed to move job file: #{e.message}"
75
+ @index.delete(job_file)
73
76
  end
74
77
  end
75
78
  end
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: filesystem_queue
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carlos Westman
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-09-13 00:00:00.000000000 Z
11
+ date: 2024-09-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: FilesystemQueue is a Ruby gem that provides a persistent queue system
14
- using the local filesystem. It allows you to enqueue and dequeue jobs, and keeps
14
+ using the local filesystem.It allows you to enqueue and dequeue jobs, and keeps
15
15
  track of completed and failed jobs.
16
16
  email:
17
17
  - carloswestman@gmail.com