queue_lite 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 39b66901b2cd8992a2dc7a3b68db0fef744831a6b661e882457bc893c775975f
4
+ data.tar.gz: 9fba726997f1c2707b2d84694b266e3f2f0d5f73f54a87c3edd2c20f7ade21db
5
+ SHA512:
6
+ metadata.gz: 0001ea7b4a0bc1a96b3b33a4a4ef03a9948ed673ea72ad2691ab8d54ca1f535eef57dc455aed1185f125dc1d6d6f503973e250fc50ec26d67816729307e843d0
7
+ data.tar.gz: 5504af848a6915eada00b8670f49885de8143555b7db92787b8e8bb6864728a7ac7faadff3f8602a2b924e7dc376c1b2cd089b4ecbac4287fad5380d98dc297a
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/standardrb/standard
3
+ ruby_version: 2.6
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Adam Daniels
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # QueueLite
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/queue_lite`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_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.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+
27
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/queue_lite.
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[test standard]
@@ -0,0 +1,14 @@
1
+ # frozen-string-literal: true
2
+
3
+ require "json"
4
+
5
+ module QueueLite
6
+ class Enqueue < Module
7
+ def initialize(queue)
8
+ define_method :enqueue do |*args|
9
+ queue.put(JSON.generate([self.class.to_s, *args]))
10
+ true
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,105 @@
1
+ # frozen-string-literal: true
2
+
3
+ require "sqlite3"
4
+
5
+ module QueueLite
6
+ class Queue
7
+ READY_STATUS = "ready"
8
+ LOCKED_STATUS = "locked"
9
+ FAILED_STATUS = "failed"
10
+
11
+ Task = Data.define(:id, :data) do
12
+ def initialize(data: nil, **)
13
+ super
14
+ end
15
+ end
16
+
17
+ def self.build(connection_string)
18
+ db = SQLite3::Database.new(connection_string)
19
+
20
+ new(db).tap do |instance|
21
+ instance.prepare
22
+ end
23
+ end
24
+
25
+ def initialize(db)
26
+ @db = db
27
+ end
28
+
29
+ def prepare
30
+ db.execute("PRAGMA journal_mode = 'WAL';")
31
+
32
+ db.execute(<<~SQL)
33
+ CREATE TABLE IF NOT EXISTS queue
34
+ (
35
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
36
+ data TEXT,
37
+ status TEXT
38
+ )
39
+ SQL
40
+ end
41
+
42
+ def put(data)
43
+ row = db.get_first_row(<<~SQL, [data, READY_STATUS])
44
+ INSERT INTO queue(data, status) VALUES(?, ?)
45
+ RETURNING id, data
46
+ SQL
47
+
48
+ Task.new(*row)
49
+ end
50
+
51
+ def pop
52
+ row = db.get_first_row(<<~SQL, [LOCKED_STATUS, READY_STATUS])
53
+ UPDATE queue
54
+ SET status = ?
55
+ WHERE rowid = (SELECT rowid
56
+ FROM queue
57
+ WHERE status = ?
58
+ ORDER BY id
59
+ LIMIT 1)
60
+ RETURNING id, data
61
+ SQL
62
+
63
+ return if row.nil?
64
+
65
+ Task.new(*row)
66
+ end
67
+
68
+ def done(id)
69
+ row = db.get_first_row(<<~SQL, [LOCKED_STATUS, id])
70
+ UPDATE queue
71
+ SET status = ?
72
+ WHERE id = ?
73
+ RETURNING id, data
74
+ SQL
75
+
76
+ Task.new(*row)
77
+ end
78
+
79
+ def failed(id)
80
+ row = db.get_first_row(<<~SQL, [FAILED_STATUS, id])
81
+ UPDATE queue
82
+ SET status = ?
83
+ WHERE id = ?
84
+ RETURNING id, data
85
+ SQL
86
+
87
+ Task.new(*row)
88
+ end
89
+
90
+ def get(id)
91
+ row = db.get_first_row(<<~SQL, [id])
92
+ SELECT id, data
93
+ FROM queue
94
+ WHERE id = ?
95
+ LIMIT 1
96
+ SQL
97
+
98
+ Task.new(*row)
99
+ end
100
+
101
+ private
102
+
103
+ attr_reader :db
104
+ end
105
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module QueueLite
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,41 @@
1
+ # frozen-string-literal: true
2
+
3
+ require "json"
4
+
5
+ module QueueLite
6
+ class Worker
7
+ def initialize(queue)
8
+ @queue = queue
9
+ @running = false
10
+ end
11
+
12
+ def run
13
+ @running = true
14
+ while @running
15
+ perform_once
16
+ end
17
+ end
18
+
19
+ def perform_once
20
+ task = queue.pop
21
+ return if task.nil?
22
+
23
+ klass, *args = JSON.parse(task.data)
24
+ begin
25
+ Object.const_get(klass).perform(*args).tap do
26
+ queue.done(task.id)
27
+ end
28
+ rescue
29
+ queue.failed(task.id)
30
+ end
31
+ end
32
+
33
+ def shutdown
34
+ @running = false
35
+ end
36
+
37
+ private
38
+
39
+ attr_reader :queue
40
+ end
41
+ end
data/lib/queue_lite.rb ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "queue_lite/version"
4
+
5
+ module QueueLite
6
+ class Error < StandardError; end
7
+
8
+ require_relative "queue_lite/enqueue"
9
+ require_relative "queue_lite/worker"
10
+ require_relative "queue_lite/queue"
11
+ end
@@ -0,0 +1,4 @@
1
+ module QueueLite
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: queue_lite
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Daniels
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-01-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sqlite3
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description:
28
+ email:
29
+ - adam@mediadrive.ca
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".standard.yml"
35
+ - LICENSE.txt
36
+ - README.md
37
+ - Rakefile
38
+ - lib/queue_lite.rb
39
+ - lib/queue_lite/enqueue.rb
40
+ - lib/queue_lite/queue.rb
41
+ - lib/queue_lite/version.rb
42
+ - lib/queue_lite/worker.rb
43
+ - sig/queue_lite.rbs
44
+ homepage: https://github.com/adam12/queue_lite
45
+ licenses:
46
+ - MIT
47
+ metadata:
48
+ homepage_uri: https://github.com/adam12/queue_lite
49
+ source_code_uri: https://github.com/adam12/queue_lite
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: 2.6.0
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubygems_version: 3.5.4
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: Simple queue implementation on top of SQLite
69
+ test_files: []