litequeue 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e33bb82bf866416420a67cdbc4d5106a85d378ff9d0739ea8dbdbb7022f7902d
4
- data.tar.gz: d8d721f96fb1a533fcc1446b6d193de6eba46a575b8114b7fdf537958b8e079f
3
+ metadata.gz: 975ed284b758c73f3ac99d5874c05fea38eb8c25bb36221b56f3e778b3f8fc08
4
+ data.tar.gz: fcb2a7662243851c7dcc62ea6346b0a1cbd591330c2d0883737609ff7996c260
5
5
  SHA512:
6
- metadata.gz: 5b421f5e2375f74806ccf2564ff642698c25b9a59f26d92190fa138808d1a08e9ccbca295c8925604a0bb1b7a4383db8f46e0a97b1ae0f4c925633b356791280
7
- data.tar.gz: c57def82958f2878d7a42ea65a45af8a5a4599cd3b83a2d0481db6a8d02f8663bffed4362adbb1af732f86e948a70ed7e7e2341269b96ea8f436c8c5df06f67e
6
+ metadata.gz: 17cb8fc124f2b14ff43f10e2281af2d33e60b684a2dcea9c735e15798efa3790976cda508d8e9d5a1ef2816b4322bf9c906b53156a65c68987cb8d1d29d82fcd
7
+ data.tar.gz: 10cea9107216cbbb56a2fe2e7647c2a5830acd74ae8840817dd6dd9aa717124588dd61cbb2d2ba51b4adcb39661c353c36ff7c10a1e6d4551feb76c560b4fbab
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.1] - 2023-08-11
4
+
5
+ - Update statements to work with SQLite versions that don't support `UNIXEPOCH('subsec')``
6
+
7
+ ## [0.2.0] - 2023-08-11
8
+
9
+ - Initial release of usable code
10
+
3
11
  ## [0.1.0] - 2023-08-09
4
12
 
5
- - Initial release
13
+ - Initial release to reserve gem name
data/Gemfile.lock CHANGED
@@ -1,15 +1,21 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- litequeue (0.1.0)
4
+ litequeue (0.2.1)
5
+ litedb (>= 0.2.1)
5
6
 
6
7
  GEM
7
8
  remote: https://rubygems.org/
8
9
  specs:
9
10
  ast (2.4.2)
11
+ docile (1.4.0)
10
12
  json (2.6.3)
11
13
  language_server-protocol (3.17.0.3)
12
14
  lint_roller (1.1.0)
15
+ litedb (0.2.1)
16
+ litescheduler (>= 0.2.0)
17
+ sqlite3 (>= 1.5.0)
18
+ litescheduler (0.2.1)
13
19
  minitest (5.19.0)
14
20
  parallel (1.23.0)
15
21
  parser (3.2.2.3)
@@ -36,6 +42,14 @@ GEM
36
42
  rubocop (>= 1.7.0, < 2.0)
37
43
  rubocop-ast (>= 0.4.0)
38
44
  ruby-progressbar (1.13.0)
45
+ simplecov (0.22.0)
46
+ docile (~> 1.1)
47
+ simplecov-html (~> 0.11)
48
+ simplecov_json_formatter (~> 0.1)
49
+ simplecov-html (0.12.3)
50
+ simplecov_json_formatter (0.1.4)
51
+ sqlite3 (1.6.3-arm64-darwin)
52
+ sqlite3 (1.6.3-x86_64-linux)
39
53
  standard (1.30.1)
40
54
  language_server-protocol (~> 3.17.0.2)
41
55
  lint_roller (~> 1.0)
@@ -52,11 +66,13 @@ GEM
52
66
 
53
67
  PLATFORMS
54
68
  arm64-darwin-21
69
+ x86_64-linux
55
70
 
56
71
  DEPENDENCIES
57
72
  litequeue!
58
73
  minitest (~> 5.0)
59
74
  rake (~> 13.0)
75
+ simplecov
60
76
  standard (~> 1.3)
61
77
 
62
78
  BUNDLED WITH
data/README.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Litequeue
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/litequeue.svg)](https://rubygems.org/gems/litequeue)
4
+ [![Gem Downloads](https://img.shields.io/gem/dt/litequeue)](https://rubygems.org/gems/litequeue)
5
+ ![Tests](https://github.com/litestack-ruby/litequeue/actions/workflows/main.yml/badge.svg)
6
+ ![Coverage](https://img.shields.io/badge/code_coverage-100%25-brightgreen)
7
+
3
8
  TODO: Delete this and the text below, and describe your gem
4
9
 
5
10
  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/litequeue`. To experiment with that code, run `bin/console` for an interactive prompt.
data/Rakefile CHANGED
@@ -9,6 +9,34 @@ Rake::TestTask.new(:test) do |t|
9
9
  t.test_files = FileList["test/**/test_*.rb"]
10
10
  end
11
11
 
12
+ desc "Update the README code coverage badge"
13
+ task :update_readme_coverage_badge do
14
+ require "json"
15
+
16
+ next unless File.exist?("coverage/.last_run.json")
17
+
18
+ last_run_coverage = JSON.load_file("coverage/.last_run.json")
19
+ line_coverage = last_run_coverage.dig("result", "line")
20
+ branch_coverage = last_run_coverage.dig("result", "branch")
21
+ average_coverage = [(branch_coverage * 1), (line_coverage * 1.5)].sum.fdiv(2.5).round
22
+ badge_color = if average_coverage >= 75
23
+ :brightgreen
24
+ else
25
+ :red
26
+ end
27
+
28
+ coverage_badge_re = /!\[Coverage\]\(https:\/\/img.shields.io\/badge\/code_coverage-(.*?)\)/
29
+ last_run_coverage_badge = "![Coverage](https://img.shields.io/badge/code_coverage-#{average_coverage}%25-#{badge_color})"
30
+
31
+ new_readme = File.read("README.md").gsub(coverage_badge_re, last_run_coverage_badge)
32
+
33
+ File.write("README.md", new_readme)
34
+
35
+ puts "Updated README code coverage badge to show #{average_coverage}% coverage."
36
+ end
37
+
38
+ task cov: %i[test update_readme_coverage_badge]
39
+
12
40
  require "standard/rake"
13
41
 
14
42
  task default: %i[test standard]
@@ -0,0 +1,11 @@
1
+ create_table_queue: >
2
+ CREATE TABLE IF NOT EXISTS queue(
3
+ id TEXT PRIMARY KEY NOT NULL ON CONFLICT REPLACE,
4
+ name TEXT NOT NULL ON CONFLICT REPLACE,
5
+ fire_at INTEGER NOT NULL ON CONFLICT REPLACE,
6
+ value TEXT,
7
+ created_at INTEGER DEFAULT(UNIXEPOCH()) NOT NULL ON CONFLICT REPLACE
8
+ ) WITHOUT ROWID;
9
+
10
+ create_index_queue_by_name: >
11
+ CREATE INDEX IF NOT EXISTS idx_queue_by_name ON queue(name, fire_at ASC);
@@ -0,0 +1,48 @@
1
+ push: >
2
+ INSERT INTO queue(id, name, fire_at, value)
3
+ VALUES (HEX(RANDOMBLOB(32)), $1, (IFNULL(UNIXEPOCH('subsec'), UNIXEPOCH()) + $2), $3)
4
+ RETURNING id, name;
5
+
6
+ pop: >
7
+ DELETE FROM queue
8
+ WHERE name != '_dead'
9
+ AND (name, fire_at, id)
10
+ IN (
11
+ SELECT name, fire_at, id FROM queue
12
+ WHERE name = IFNULL($1, 'default')
13
+ AND fire_at <= IFNULL(UNIXEPOCH('subsec'), UNIXEPOCH())
14
+ ORDER BY fire_at ASC
15
+ LIMIT IFNULL($2, 1)
16
+ )
17
+ RETURNING id, value;
18
+
19
+ repush: >
20
+ INSERT INTO queue(id, name, fire_at, value)
21
+ VALUES ($1, $2, (IFNULL(UNIXEPOCH('subsec'), UNIXEPOCH()) + $3), $4)
22
+ RETURNING name;
23
+
24
+ delete: >
25
+ DELETE FROM queue
26
+ WHERE id = $1
27
+ RETURNING value;
28
+
29
+ count: >
30
+ SELECT COUNT(*)
31
+ FROM queue
32
+ WHERE IIF($1 IS NULL, 1, name = $1);
33
+
34
+ clear: >
35
+ DELETE FROM queue
36
+ WHERE IIF($1 IS NULL, 1, name = $1)
37
+ RETURNING id;
38
+
39
+ info: >
40
+ SELECT
41
+ name,
42
+ COUNT(*) AS count,
43
+ AVG(IFNULL(UNIXEPOCH('subsec'), UNIXEPOCH()) - created_at) AS avg,
44
+ MIN(IFNULL(UNIXEPOCH('subsec'), UNIXEPOCH()) - created_at) AS min,
45
+ MAX(IFNULL(UNIXEPOCH('subsec'), UNIXEPOCH()) - created_at) AS max
46
+ FROM queue
47
+ GROUP BY name
48
+ ORDER BY count DESC;
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Litequeue
4
- VERSION = "0.1.0"
3
+ class Litequeue
4
+ VERSION = "0.2.1"
5
5
  end
data/lib/litequeue.rb CHANGED
@@ -2,7 +2,102 @@
2
2
 
3
3
  require_relative "litequeue/version"
4
4
 
5
- module Litequeue
6
- class Error < StandardError; end
7
- # Your code goes here...
5
+ require "singleton"
6
+ require "yaml"
7
+ require "litedb"
8
+
9
+ class Litequeue
10
+ include Singleton
11
+
12
+ DEFAULT_QUEUE = "default"
13
+
14
+ Configuration = Struct.new(:path, :synchronous, :mmap_size, :journal_size_limit)
15
+
16
+ def self.configuration
17
+ @configuration ||= Configuration.new(
18
+ _path = "queue.sqlite3",
19
+ _synchronous = :OFF,
20
+ _mmap_size = 32 * 1024
21
+ )
22
+ end
23
+
24
+ def self.configure
25
+ yield(configuration)
26
+ end
27
+
28
+ def self.migrations
29
+ YAML.load_file("#{__dir__}/litequeue/migrations.sql.yml")
30
+ end
31
+
32
+ def self.statements
33
+ YAML.load_file("#{__dir__}/litequeue/statements.sql.yml")
34
+ end
35
+
36
+ def initialize
37
+ configuration = self.class.configuration
38
+ file = configuration.path
39
+ options = configuration.to_h
40
+ .slice(:synchronous, :mmap_size, :journal_size_limit)
41
+ .merge(migrations: self.class.migrations,
42
+ statements: self.class.statements)
43
+
44
+ @db = Litedb::Connection.new(file, options)
45
+ # Once the instance has been initialized, don't allow the configuration to be changed
46
+ # as it won't have any effect.
47
+ configuration.freeze
48
+ end
49
+
50
+ def push(value, queue: DEFAULT_QUEUE, delay: 0)
51
+ results = @db.run_statement(:push, queue, delay, value) # [["{id}", "{name}"]]
52
+ extract_row(results)
53
+ end
54
+
55
+ def pop(queue: DEFAULT_QUEUE, limit: 1)
56
+ results = @db.run_statement(:pop, queue, limit)
57
+
58
+ return extract_row(results) if limit == 1
59
+
60
+ results
61
+ end
62
+
63
+ def repush(id, value, queue: DEFAULT_QUEUE, delay: 0)
64
+ results = @db.run_statement(:repush, id, queue, delay, value)
65
+ extract_value(results)
66
+ end
67
+
68
+ def delete(id)
69
+ results = @db.run_statement(:delete, id)
70
+ extract_value(results)
71
+ end
72
+
73
+ def count(queue: nil)
74
+ results = @db.run_statement(:count, queue)
75
+ extract_value(results)
76
+ end
77
+
78
+ def clear(queue: nil)
79
+ results = @db.run_statement(:clear, queue)
80
+ results.count
81
+ end
82
+
83
+ def empty?
84
+ count.zero?
85
+ end
86
+
87
+ private
88
+
89
+ def extract_value(results) # [["{value}"]] || []
90
+ return if results.empty?
91
+
92
+ results
93
+ .first # [[value]] -> [value]
94
+ .first # [value] -> value
95
+ end
96
+
97
+ def extract_row(results) # [[{value}, {value}]] || []
98
+ return if results.empty?
99
+
100
+ results
101
+ .first # [[value, value]] -> [value, value]
102
+ end
8
103
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: litequeue
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mohamed Hassan
@@ -9,8 +9,36 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2023-08-09 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2023-08-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: litedb
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: 0.2.1
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: 0.2.1
28
+ - !ruby/object:Gem::Dependency
29
+ name: simplecov
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
14
42
  description:
15
43
  email:
16
44
  - oldmoe@gmail.com
@@ -28,6 +56,8 @@ files:
28
56
  - README.md
29
57
  - Rakefile
30
58
  - lib/litequeue.rb
59
+ - lib/litequeue/migrations.sql.yml
60
+ - lib/litequeue/statements.sql.yml
31
61
  - lib/litequeue/version.rb
32
62
  - sig/litequeue.rbs
33
63
  homepage: https://github.com/litestack-ruby/litequeue