postqueue 0.4.2 → 0.5.0

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
  SHA1:
3
- metadata.gz: d9f6d7f05810076cd9c15f8b47515817b2e331be
4
- data.tar.gz: 0d0bff63426af6568730ea89e8da3ce3e5a3b015
3
+ metadata.gz: 2edb04093f702ef2fb42d716de3ebdc71c9a65f3
4
+ data.tar.gz: f2148b265f5669b0b8a81610d1b355d7e8a9036e
5
5
  SHA512:
6
- metadata.gz: 6b84633af24db245a98380b96ed17021650945d0ffe554eaaa54c0a3adf8fbf7ecdc8b3036443044371822980f0f0129ab6bb93b89bad4dfce5308ab05d406e7
7
- data.tar.gz: 6e9950292583f300ad5f0d03eb777c8337704986231a6b5dee71b19f4d00e4c102ff6b64e7124a66e930520b7193443a1d294d763b5993048c26ebb5d3eb41c8
6
+ metadata.gz: 6b2a2098f86dfa48039e57e3c61d7491c22f19cb0e016af0af0e055d561dc0ff91605e0b3b2a4ae3c5f16acda96aa4e606a8fb4a9d326e97740bca50f2e5a615
7
+ data.tar.gz: 9bb0c84cb618a457d5410ef7c2337035ffe15573085940011730165a2b1791b5f05f4a2bf8ca766ea879d7a6d68ebe00e179147ba3fc7ac5247647363dc813a9
@@ -0,0 +1,29 @@
1
+ require "ostruct"
2
+
3
+ module Postqueue
4
+ module CLI
5
+ module Stats
6
+ module_function
7
+
8
+ def stats(_options)
9
+ require "table_print"
10
+ sql = <<-SQL
11
+ SELECT op,
12
+ COUNT(*) AS count,
13
+ MIN(now() - created_at) AS min_age,
14
+ MAX(now() - created_at) AS max_age,
15
+ AVG(now() - created_at) AS avg_age
16
+ FROM #{Postqueue.item_class.table_name} GROUP BY op
17
+ SQL
18
+
19
+ recs = Postqueue.item_class.find_by_sql(sql)
20
+ tp recs, :op, :count, :avg_age, :min_age, :max_age
21
+ end
22
+
23
+ def peek(_options)
24
+ require "table_print"
25
+ tp Postqueue.default_queue.upcoming(subselect: false).limit(100).all
26
+ end
27
+ end
28
+ end
29
+ end
data/lib/postqueue/cli.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "ostruct"
2
2
 
3
3
  require_relative "cli/options_parser"
4
+ require_relative "cli/stats"
4
5
 
5
6
  module Postqueue
6
7
  module CLI
@@ -12,14 +13,12 @@ module Postqueue
12
13
  @options = OptionsParser.parse_args(argv)
13
14
 
14
15
  case options.sub_command
15
- when "stats"
16
+ when "stats", "peek"
16
17
  connect_to_database!
17
- sql = "SELECT * FROM #{Postqueue.item_class.table_name}"
18
- tp Postqueue.item_class.find_by_sql(sql)
18
+ Stats.send options.sub_command, options
19
19
  when "enqueue"
20
20
  connect_to_database!
21
21
  count = Postqueue.enqueue op: options.op, entity_id: options.entity_ids
22
- puts "returned #{count.inspect}"
23
22
  Postqueue.logger.info "Enqueued #{count} queue items"
24
23
  when "process"
25
24
  connect_to_instance!
@@ -50,8 +50,6 @@ module Postqueue
50
50
  callbacks[op] || callbacks["*"]
51
51
  end
52
52
 
53
- private
54
-
55
53
  def run_callback(op:, entity_ids:)
56
54
  queue_times = item_class.find_by_sql <<-SQL
57
55
  SELECT extract('epoch' from AVG(now() - created_at)) AS avg,
@@ -7,18 +7,19 @@ module Postqueue
7
7
  end
8
8
 
9
9
  def run!
10
- if !run
11
- run do |queue|
12
- while true do
13
- queue.logger.debug "#{queue}: Processing until empty"
14
- queue.process_until_empty
15
- queue.logger.debug "#{queue}: sleeping"
16
- sleep 1
17
- end
10
+ set_default_runner unless @run
11
+ @run.call(self)
12
+ end
13
+
14
+ def set_default_runner
15
+ run do |queue|
16
+ loop do
17
+ queue.logger.debug "#{queue}: Processing until empty"
18
+ queue.process_until_empty
19
+ queue.logger.debug "#{queue}: sleeping"
20
+ sleep 1
18
21
  end
19
22
  end
20
-
21
- run.call(self)
22
23
  end
23
24
  end
24
25
  end
@@ -1,14 +1,19 @@
1
1
  module Postqueue
2
2
  class Queue
3
+ def upcoming(relation = nil, subselect: true) #:nodoc:
4
+ relation = item_class.all if relation.nil?
5
+ relation = relation.select(:id, :entity_id, :op) if subselect
6
+
7
+ # Ordering by next_run_at and id should not strictly be necessary, but helps
8
+ # processing entries in the passed in order when enqueued at the same time.
9
+ relation.where("failed_attempts < ? AND next_run_at < ?", max_attemps, Time.now)
10
+ .order(:next_run_at, :id)
11
+ end
12
+
3
13
  # Select and lock up to \a limit unlocked items in the queue. Used by
4
14
  # select_and_lock_batch.
5
15
  def select_and_lock(relation, limit:)
6
- # Ordering by next_run_at and id should not strictly be necessary, but helps
7
- # processing entries in the passed in order when enqueued at the same time.
8
- relation = relation
9
- .select(:id, :entity_id, :op)
10
- .where("failed_attempts < ? AND next_run_at < ?", max_attemps, Time.now)
11
- .order(:next_run_at, :id)
16
+ relation = upcoming(relation)
12
17
 
13
18
  # FOR UPDATE SKIP LOCKED selects and locks entries, but skips those that
14
19
  # are already locked - preventing this transaction from being locked.
@@ -50,7 +50,7 @@ module Postqueue
50
50
  return enqueued_items unless enqueued_items > 0
51
51
 
52
52
  process_until_empty(op: op) unless async_processing?
53
- return enqueued_items
53
+ enqueued_items
54
54
  end
55
55
  end
56
56
  end
@@ -1,3 +1,3 @@
1
1
  module Postqueue
2
- VERSION = "0.4.2"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -21,7 +21,8 @@ describe "concurrency tests" do
21
21
  log = File.open(LOG_FILE, "a")
22
22
  queue = Postqueue.new
23
23
  queue.on "*" do |_op, entity_ids|
24
- sleep(0.0001); log.write "#{entity_ids.first}\n"
24
+ sleep(0.0001)
25
+ log.write "#{entity_ids.first}\n"
25
26
  end
26
27
  queue.process_until_empty
27
28
  log.close
@@ -33,9 +34,7 @@ describe "concurrency tests" do
33
34
  def run_scenario(cnt, n_threads)
34
35
  FileUtils.rm_rf LOG_FILE
35
36
 
36
- queue = Postqueue.new do |queue|
37
- # queue.default_batch_size = 10
38
- end
37
+ queue = Postqueue.new
39
38
 
40
39
  benchmark "enqueuing #{cnt} ops" do
41
40
  queue.enqueue op: "myop", entity_id: (1..cnt)
@@ -67,9 +66,7 @@ describe "concurrency tests" do
67
66
  it "enqueues many entries" do
68
67
  cnt = 1000
69
68
 
70
- queue = Postqueue.new do |queue|
71
- # queue.default_batch_size = 10
72
- end
69
+ queue = Postqueue.new
73
70
  benchmark "enqueuing #{cnt} ops" do
74
71
  queue.enqueue op: "myop", entity_id: (1..cnt)
75
72
  end
@@ -15,7 +15,9 @@ describe "error handling" do
15
15
 
16
16
  context "when handler raises an exception" do
17
17
  before do
18
- queue.on "mytype" do raise E end
18
+ queue.on "mytype" do
19
+ raise E
20
+ end
19
21
  queue.enqueue op: "mytype", entity_id: 12
20
22
  end
21
23
 
@@ -42,7 +42,6 @@ describe "processing" do
42
42
 
43
43
  it "calls the registered handler and returns the processed entries" do
44
44
  queue.enqueue op: "otherop", entity_id: 112
45
- called = false
46
45
  queue.process_one(op: "otherop")
47
46
 
48
47
  op, ids = processed_events.first
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: postqueue
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - radiospiel
@@ -163,6 +163,7 @@ files:
163
163
  - lib/postqueue.rb
164
164
  - lib/postqueue/cli.rb
165
165
  - lib/postqueue/cli/options_parser.rb
166
+ - lib/postqueue/cli/stats.rb
166
167
  - lib/postqueue/default_queue.rb
167
168
  - lib/postqueue/item.rb
168
169
  - lib/postqueue/item/enqueue.rb