filesystem_queue 0.1.1 → 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: 51d0fe57ac440673ae98c9fa2a782ddfe6552450bf984dd44c4edc0d0f972e2f
4
- data.tar.gz: e61ded49f3eb2919f0da55ce20627a5d3a732c22c71bf747b64def5f42f0e2a4
3
+ metadata.gz: 2e960414c06a8d0a9fe7d7fc0b8b1ed90cf604d7648ab74c64f924648a137e94
4
+ data.tar.gz: 06f0a8e59cbb4c64a2de4af00360b317bb13e8a136f3d1647ff802b8b7617939
5
5
  SHA512:
6
- metadata.gz: 7ecb0a4780632dcedcbe33593460c9b827c4bc1402b967fe9cd010ca3cbb2ab97f4e7bcdcbe33f707caf7bf8a90d964c65f8e773cf806f2a2e3436e203398c85
7
- data.tar.gz: 73b113cb15f2cd5eb285565ceb82c62b8d3b0f6180c49c75c374228becd4ba0f1877dc49794cc01b7ca0e3b4442b43614d229e21f093e43739e4c7ab84da2eed
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,8 @@
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
3
6
  ## [0.1.1] - 2024-09-13
4
7
  - Added unit tests
5
8
  - Fixed Github Actions
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.1)
4
+ filesystem_queue (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -14,9 +14,11 @@
14
14
  - **Persistence**: Jobs are stored on the filesystem, ensuring they are not lost between application restarts.
15
15
  - **Minimal Overhead**: No need for external dependencies or services.
16
16
  - **Ease of Use**: Simple API for enqueuing and dequeuing jobs.
17
- - **Improved Performance**: Enhances filesystem performance by using an index file that keeps track of files by the time they are written in the queue.
18
- - **Tailored for Local Use**: This system is designed for local use with a single consumer (at the moment).
19
- - **Job Management**: Completed and failed jobs are tracked in separate folders for easy management.
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.
20
22
 
21
23
  ## Installation
22
24
 
@@ -117,6 +119,9 @@ puts 'Queue is empty'
117
119
  # Check on failed jobs
118
120
  puts "Queue size: #{queue.size}"
119
121
  puts "Failed jobs: #{queue.failed_size}"
122
+
123
+ # Cleanup all files and directories created by the queue
124
+ queue.cleanup
120
125
  ```
121
126
 
122
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,25 +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 "\
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"
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'
17
17
 
18
- spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
19
19
 
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"
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'
23
23
 
24
24
  # Specify which files should be added to the gem when it is released.
25
25
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
@@ -28,7 +28,7 @@ Gem::Specification.new do |spec|
28
28
  (File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
29
29
  end
30
30
  end
31
- spec.bindir = "exe"
31
+ spec.bindir = 'exe'
32
32
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
33
- spec.require_paths = ["lib"]
33
+ spec.require_paths = ['lib']
34
34
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FilesystemQueue
4
- VERSION = "0.1.1"
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,27 +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 = extract_job_file_from_index
35
- return nil unless job_file && File.exist?(job_file)
34
+ return nil if @index.empty?
35
+
36
+ job_file = @index.shift
37
+ return nil unless File.exist?(job_file)
36
38
 
37
39
  job_data = JSON.parse(File.read(job_file), symbolize_names: true)
38
40
  [job_file, job_data]
@@ -47,33 +49,30 @@ module FilesystemQueue
47
49
  end
48
50
 
49
51
  def size
50
- File.readlines(@index_file).size
52
+ @index.size
51
53
  end
52
54
 
53
55
  def failed_size
54
- 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)
55
65
  end
56
66
 
57
67
  private
58
68
 
59
- def move_job(job_file, target_dir)
60
- FileUtils.mv(job_file, target_dir)
61
- rescue StandardError => e
62
- puts "Failed to move job file: #{e.message}"
69
+ def rebuild_index
70
+ Dir.glob(File.join(@jobs_dir, '*.json')).sort
63
71
  end
64
72
 
65
- def extract_job_file_from_index
66
- job_file = nil
67
- File.open(@index_file, "r+") do |f|
68
- lines = f.each_line.to_a
69
- return nil if lines.empty?
70
-
71
- job_file = lines.shift.strip
72
- f.rewind
73
- f.write(lines.join)
74
- f.truncate(f.pos)
75
- end
76
- job_file
73
+ def move_job(job_file, target_dir)
74
+ FileUtils.mv(job_file, target_dir)
75
+ @index.delete(job_file)
77
76
  end
78
77
  end
79
78
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: filesystem_queue
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
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
14
  using the local filesystem.It allows you to enqueue and dequeue jobs, and keeps