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 +4 -4
- data/.rubocop.yml +0 -1
- data/CHANGELOG.md +7 -1
- data/Gemfile +4 -4
- data/Gemfile.lock +2 -1
- data/README.md +23 -4
- data/Rakefile +6 -6
- data/filesystem_queue.gemspec +16 -15
- data/lib/filesystem_queue/version.rb +1 -1
- data/lib/filesystem_queue.rb +27 -24
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2e960414c06a8d0a9fe7d7fc0b8b1ed90cf604d7648ab74c64f924648a137e94
|
4
|
+
data.tar.gz: 06f0a8e59cbb4c64a2de4af00360b317bb13e8a136f3d1647ff802b8b7617939
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6a9d6f4d5c55313ed7af2bc85245c019f97a66a691a4511c67ed78a53e74fa91f6574e513ebaf1e288aaeeb74cca74bbb53de289c294ff4ad781c496a9e4edf1
|
7
|
+
data.tar.gz: a71491c4ac7e6f46b8834ef9ca7f1f7534216d6036c4e98877c2f0ae34a050202699fb471b9886a51f6fb61fa3359aa297980f4debc74e93db90108adbf8f756
|
data/.rubocop.yml
CHANGED
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
|
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
|
8
|
+
gem 'rake', '~> 13.0'
|
9
9
|
|
10
|
-
gem
|
10
|
+
gem 'minitest', '~> 5.0'
|
11
11
|
|
12
|
-
gem
|
12
|
+
gem 'rubocop', '~> 1.21'
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,18 +1,34 @@
|
|
1
1
|
# FilesystemQueue
|
2
2
|
|
3
|
+
[](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
|
-
|
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
|
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
|
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
|
4
|
-
require
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rake/testtask'
|
5
5
|
|
6
6
|
Rake::TestTask.new(:test) do |t|
|
7
|
-
t.libs <<
|
8
|
-
t.libs <<
|
9
|
-
t.test_files = FileList[
|
7
|
+
t.libs << 'test'
|
8
|
+
t.libs << 'lib'
|
9
|
+
t.test_files = FileList['test/**/test_*.rb']
|
10
10
|
end
|
11
11
|
|
12
|
-
require
|
12
|
+
require 'rubocop/rake_task'
|
13
13
|
|
14
14
|
RuboCop::RakeTask.new
|
15
15
|
|
data/filesystem_queue.gemspec
CHANGED
@@ -1,24 +1,25 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative
|
3
|
+
require_relative 'lib/filesystem_queue/version'
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
|
-
spec.name =
|
6
|
+
spec.name = 'filesystem_queue'
|
7
7
|
spec.version = FilesystemQueue::VERSION
|
8
|
-
spec.authors = [
|
9
|
-
spec.email = [
|
8
|
+
spec.authors = ['Carlos Westman']
|
9
|
+
spec.email = ['carloswestman@gmail.com']
|
10
10
|
|
11
|
-
spec.summary =
|
12
|
-
spec.description =
|
13
|
-
|
14
|
-
spec.
|
15
|
-
spec.
|
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[
|
18
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
18
19
|
|
19
|
-
spec.metadata[
|
20
|
-
spec.metadata[
|
21
|
-
spec.metadata[
|
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 =
|
31
|
+
spec.bindir = 'exe'
|
31
32
|
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
32
|
-
spec.require_paths = [
|
33
|
+
spec.require_paths = ['lib']
|
33
34
|
end
|
data/lib/filesystem_queue.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require_relative
|
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,
|
16
|
-
@completed_dir = File.join(@queue_dir,
|
17
|
-
@failed_dir = File.join(@queue_dir,
|
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
|
-
|
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
|
-
|
30
|
+
@index << job_file
|
31
31
|
end
|
32
32
|
|
33
33
|
def dequeue
|
34
|
-
|
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
|
-
|
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
|
-
|
52
|
+
@index.size
|
61
53
|
end
|
62
54
|
|
63
55
|
def failed_size
|
64
|
-
Dir[File.join(@failed_dir,
|
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
|
-
|
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.
|
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-
|
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.
|
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
|