litestack 0.1.6 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -52,8 +52,11 @@ class Litequeue
52
52
  alias_method :"<<", :push
53
53
 
54
54
  # pop an item from the queue, optionally with a specific queue name (default queue name is 'default')
55
- def pop(queue='default')
56
- @queue.acquire {|q| q.stmts[:pop].execute!(queue)[0] }
55
+ def pop(queue='default', limit = 1)
56
+ res = @queue.acquire {|q| res = q.stmts[:pop].execute!(queue, limit) }
57
+ return res[0] if res.length == 1
58
+ return nil if res.empty?
59
+ res
57
60
  end
58
61
 
59
62
  # delete an item from the queue
@@ -62,7 +65,7 @@ class Litequeue
62
65
  # queue.delete(id) # => "somevalue"
63
66
  # queue.pop # => nil
64
67
  def delete(id, queue='default')
65
- fire_at, id = id.split("_")
68
+ fire_at, id = id.split("-")
66
69
  result = @queue.acquire{|q| q.stmts[:delete].execute!(queue, fire_at.to_i, id)[0] }
67
70
  end
68
71
 
@@ -80,6 +83,13 @@ class Litequeue
80
83
  def size
81
84
  @queue.acquire{|q| q.get_first_value("SELECT size.page_size * count.page_count FROM pragma_page_size() AS size, pragma_page_count() AS count") }
82
85
  end
86
+
87
+ def close
88
+ @queue.acquire do |q|
89
+ q.stmts.each_pair {|k, v| q.stmts[k].close }
90
+ q.close
91
+ end
92
+ end
83
93
 
84
94
  private
85
95
 
@@ -90,7 +100,7 @@ class Litequeue
90
100
  db.mmap_size = @options[:mmap_size]
91
101
  db.execute("CREATE TABLE IF NOT EXISTS _ul_queue_(queue TEXT DEFAULT('default') NOT NULL ON CONFLICT REPLACE, fire_at INTEGER DEFAULT(unixepoch()) NOT NULL ON CONFLICT REPLACE, id TEXT DEFAULT(hex(randomblob(8)) || (strftime('%f') * 100)) NOT NULL ON CONFLICT REPLACE, value TEXT, created_at INTEGER DEFAULT(unixepoch()) NOT NULL ON CONFLICT REPLACE, PRIMARY KEY(queue, fire_at ASC, id) ) WITHOUT ROWID")
92
102
  db.stmts[:push] = db.prepare("INSERT INTO _ul_queue_(queue, fire_at, value) VALUES ($1, (strftime('%s') + $2), $3) RETURNING fire_at || '-' || id")
93
- db.stmts[:pop] = db.prepare("DELETE FROM _ul_queue_ WHERE (queue, fire_at, id) = (SELECT queue, min(fire_at), id FROM _ul_queue_ WHERE queue = ifnull($1, 'default') AND fire_at <= (unixepoch()) limit 1) RETURNING fire_at || '-' || id, value")
103
+ db.stmts[:pop] = db.prepare("DELETE FROM _ul_queue_ WHERE (queue, fire_at, id) IN (SELECT queue, fire_at, id FROM _ul_queue_ WHERE queue = ifnull($1, 'default') AND fire_at <= (unixepoch()) ORDER BY fire_at ASC LIMIT ifnull($2, 1)) RETURNING fire_at || '-' || id, value")
94
104
  db.stmts[:delete] = db.prepare("DELETE FROM _ul_queue_ WHERE queue = ifnull($1, 'default') AND fire_at = $2 AND id = $3 RETURNING value")
95
105
  db
96
106
  end
@@ -11,6 +11,11 @@ module Litesupport
11
11
  @env ||= detect_environment
12
12
  end
13
13
 
14
+ def self.max_contexts
15
+ return 50 if environment == :fiber || environment == :polyphony
16
+ 5
17
+ end
18
+
14
19
  # identify which environment we are running in
15
20
  # we currently support :fiber, :polyphony, :iodine & :threaded
16
21
  # in the future we might want to expand to other environments
@@ -32,8 +37,8 @@ module Litesupport
32
37
  end
33
38
  # we should never reach here
34
39
  end
35
-
36
- def self.detect_context
40
+
41
+ def self.context
37
42
  if environment == :fiber || environment == :poylphony
38
43
  Fiber.current.storage
39
44
  else
@@ -41,8 +46,12 @@ module Litesupport
41
46
  end
42
47
  end
43
48
 
44
- def self.context
45
- @ctx ||= detect_context
49
+ def self.current_context
50
+ if environment == :fiber || environment == :poylphony
51
+ Fiber.current
52
+ else
53
+ Thread.current
54
+ end
46
55
  end
47
56
 
48
57
  # switch the execution context to allow others to run
@@ -55,7 +64,7 @@ module Litesupport
55
64
  Thread.current.switch_fiber
56
65
  true
57
66
  else
58
- # do nothing in case of thread, switching will auto-happen
67
+ #Thread.pass
59
68
  false
60
69
  end
61
70
  end
@@ -105,6 +114,21 @@ module Litesupport
105
114
  end
106
115
 
107
116
  end
117
+
118
+ module Forkable
119
+
120
+ def _fork(*args)
121
+ ppid = Process.pid
122
+ result = super
123
+ if Process.pid != ppid
124
+ # trigger a restart of all connections owned by Litesupport::Pool
125
+ end
126
+ result
127
+ end
128
+
129
+ end
130
+
131
+ #::Process.singleton_class.prepend(::Litesupport::Forkable)
108
132
 
109
133
  class Pool
110
134
 
@@ -120,11 +144,12 @@ module Litesupport
120
144
  end
121
145
 
122
146
  def acquire
147
+ # check for pid changes
123
148
  acquired = false
124
149
  result = nil
125
150
  while !acquired do
126
151
  @mutex.synchronize do
127
- if resource = @resources.find{|r| r[1] == :free}
152
+ if resource = @resources.find{|r| r[1] == :free }
128
153
  resource[1] = :busy
129
154
  begin
130
155
  result = yield resource[0]
@@ -133,11 +158,10 @@ module Litesupport
133
158
  ensure
134
159
  resource[1] = :free
135
160
  acquired = true
136
- return nil
137
161
  end
138
162
  end
139
163
  end
140
- sleep 0.0001 unless acquired
164
+ sleep 0.001 unless acquired
141
165
  end
142
166
  result
143
167
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Litestack
4
- VERSION = "0.1.6"
4
+ VERSION = "0.1.8"
5
5
  end