postjob 0.3.5 → 0.3.6

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: 19ac068f3a63b97556c7ce6aefe07e10ead759f1
4
- data.tar.gz: 003e5725fa65129288ddc34eac9625a751cc252e
3
+ metadata.gz: cc0ff8a30a5358fa8df2bbdd1f57b0325c1868ab
4
+ data.tar.gz: 0c4349e79f4b3d9397cf3edef49b04ada2ab0a63
5
5
  SHA512:
6
- metadata.gz: 3874859c70f5c0182189acbb7cbf6e16130497b6ea5783658f280f2355a6e205a020724100c293f0fe427fa146251e1fc57756af6ad892ed08b24b68c9745745
7
- data.tar.gz: 9f040743503861a3aaa2571a56d4081c4c915649b558990f201218e9207c2dc8e3576332ed7793c6a13835db66bbdaeab2687d02a8d5ff14adb0f8c284e9a945
6
+ metadata.gz: 58c65caff06342c0f397f803aa585341724f930a5a9a407a9c0d1cc9dd31563733c9ccef7a1ed5ab6516b2141f39607b916bde24725a3dfe250840d58a66dcd6
7
+ data.tar.gz: 488d9e2afb636eb561d27a8b0567499f30ca45cdfac21bd19ee100bda6a7eec057d126ddf2f1b0330c8fa8b7f4cc04c4ada37fefd6271939a1a4e12b29d40b11
@@ -1,19 +1,10 @@
1
1
  # rubocop:disable Lint/HandleExceptions
2
- # rubocop:disable Metrics/ModuleLength
3
2
 
4
3
  module Postjob::CLI
5
4
  private
6
5
 
7
- def ps_query(conditions = [])
8
- conditions.compact!
9
-
10
- conditions << "TRUE"
11
- condition_fragment = conditions
12
- .compact
13
- .map { |s| "(#{s})" }
14
- .join(" AND ")
15
-
16
- <<~SQL
6
+ def ps_query(tags:, limit:)
7
+ sql = <<-SQL
17
8
  SELECT
18
9
  id,
19
10
  full_id,
@@ -39,16 +30,14 @@ module Postjob::CLI
39
30
  COALESCE(processing_client, '') || COALESCE('/' || processing_client_identifier, '') AS worker,
40
31
  tags
41
32
  FROM postjob.postjobs
42
- WHERE #{condition_fragment}
43
- ORDER BY root_id DESC, id ASC
44
33
  SQL
45
- end
46
34
 
47
- def tags_condition(tags)
48
- return nil unless tags
35
+ scope = Simple::SQL::Scope.new(sql)
36
+ scope = scope.where("tags @> ?", Postjob::Queue::Encoder.encode(parse_tags(tags))) if tags
49
37
 
50
- kv = parse_tags(tags)
51
- "tags @> '#{Postjob::Queue::Encoder.encode(kv)}'"
38
+ scope
39
+ .paginate(per: limit, page: 1)
40
+ .order_by("root_id DESC, id ASC")
52
41
  end
53
42
 
54
43
  public
@@ -78,15 +67,26 @@ module Postjob::CLI
78
67
  return
79
68
  end
80
69
 
81
- conditions = []
82
- conditions << "root_id=id OR status NOT IN ('ready', 'sleep', 'ok') OR failed_attempts > 0"
83
- conditions << tags_condition(tags)
84
- conditions << ids_condition(ids)
70
+ query = ps_query(tags: tags, limit: limit)
71
+ query = query.where("root_id=id OR status NOT IN ('ready', 'sleep', 'ok') OR failed_attempts > 0")
72
+
73
+ print_results query: query
74
+ end
75
+
76
+ def ps_full(*ids, limit: 100, tags: nil)
77
+ connect_to_database!
78
+
79
+ query = ps_query(tags: tags, limit: limit)
80
+
81
+ unless ids.empty?
82
+ parsed_ids = parse_ids(*ids)
83
+ query = query.where("root_id IN (#{parsed_ids.join(',')})")
84
+ end
85
85
 
86
- query = ps_query(conditions)
87
- print_sql limit: limit, query: query
86
+ print_results query: query, on_empty: "Note that ps_full requires the **root ids**"
88
87
  end
89
88
 
89
+ # Show up-to-date information once per second
90
90
  def ps_top(*ids, limit: "100", tags: nil, full: false)
91
91
  loop do
92
92
  system "clear"
@@ -102,51 +102,35 @@ module Postjob::CLI
102
102
 
103
103
  # Show all information about this job
104
104
  def ps_show(id, *ids)
105
- ids = ([id] + ids).map { |s| Integer(s) }
105
+ connect_to_database!
106
106
 
107
- jobs = Simple::SQL.all <<~SQL, ids, into: Postjob::Job
108
- SELECT * FROM postjob.postjobs WHERE id = ANY($1)
109
- SQL
107
+ scope = Simple::SQL::Scope.new("SELECT * FROM postjob.postjobs")
108
+ scope = scope.where("id = ANY(?)", parse_ids(id, *ids))
110
109
 
111
- jobs.each do |job|
110
+ Simple::SQL.all(scope, into: Postjob::Job) do |job|
112
111
  pp job
113
112
  end
114
113
  end
115
114
 
116
- def ps_full(*ids, limit: 100, tags: nil)
117
- connect_to_database!
118
-
119
- conditions = []
120
- conditions << tags_condition(tags)
121
- conditions << ids_condition(ids)
122
-
123
- query = ps_query(conditions)
124
-
125
- limit = Integer(limit)
126
- print_sql limit: limit, query: query
127
- end
128
-
129
115
  private
130
116
 
131
117
  def parse_ids(*ids)
132
- return [] if ids.empty?
133
- ids.flatten.inject([]) { |a, ids_string| a.concat ids_string.split(",") }
134
- .map { |p| Integer(p) }
135
- .uniq
118
+ ids.map do |s|
119
+ s = s.gsub(/.*\./, "")
120
+ Integer(s)
121
+ end.uniq
136
122
  end
137
123
 
138
- def ids_condition(ids)
139
- ids = parse_ids(ids)
140
- return nil if ids.empty?
141
- "root_id IN (#{ids.join(',')})"
142
- end
124
+ def print_results(query:, on_empty: nil)
125
+ records = Simple::SQL.all(query, into: Hash)
126
+ tp records
143
127
 
144
- def print_sql(limit:, query:)
145
- records = Simple::SQL.all("#{query} LIMIT $1+1", limit, into: Hash)
128
+ if records.total_count > records.length
129
+ logger.warn "Output limited up to limit #{records.length}. Use the --limit command line option for a different limit."
130
+ end
146
131
 
147
- tp records[0, limit]
148
- if records.length > limit
149
- logger.warn "Output limited up to limit #{limit}. Use the --limit command line option for a different limit."
132
+ if records.empty? && on_empty
133
+ logger.warn(on_empty)
150
134
  end
151
135
  end
152
136
  end
@@ -142,7 +142,7 @@ module Postjob::Runner
142
142
  error_backtrace = error_backtrace.map { |path| path.start_with?(curdir) ? path[curdir.length..-1] : path }
143
143
 
144
144
  shutdown = should_shutdown?(exception) ? :shutdown : nil
145
- [ state, [exception.class.name, exception.to_s, error_backtrace], shutdown ]
145
+ [ state, [exception.class.name, exception.message, error_backtrace], shutdown ]
146
146
  end
147
147
 
148
148
  def log_result!(job, status, value)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: postjob
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.5
4
+ version: 0.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - radiospiel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-05 00:00:00.000000000 Z
11
+ date: 2018-04-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -120,20 +120,20 @@ dependencies:
120
120
  requirements:
121
121
  - - "~>"
122
122
  - !ruby/object:Gem::Version
123
- version: '0.3'
123
+ version: '0.4'
124
124
  - - ">="
125
125
  - !ruby/object:Gem::Version
126
- version: 0.3.7
126
+ version: 0.4.2
127
127
  type: :runtime
128
128
  prerelease: false
129
129
  version_requirements: !ruby/object:Gem::Requirement
130
130
  requirements:
131
131
  - - "~>"
132
132
  - !ruby/object:Gem::Version
133
- version: '0.3'
133
+ version: '0.4'
134
134
  - - ">="
135
135
  - !ruby/object:Gem::Version
136
- version: 0.3.7
136
+ version: 0.4.2
137
137
  - !ruby/object:Gem::Dependency
138
138
  name: simple-cli
139
139
  requirement: !ruby/object:Gem::Requirement