hotdog 0.21.2 → 0.22.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: bd4e58bca7d389bc16afeed6bd05404855b89343
4
- data.tar.gz: e401b563a1f9555ad60f3901bbad881831af114f
3
+ metadata.gz: 10ca4475d60ac71c395df0cc485978f0a033fba5
4
+ data.tar.gz: f4cff2af354ca05fb772f72c27c7ca6fd5ba7f60
5
5
  SHA512:
6
- metadata.gz: a8f8ebbfcd3243de29b784d0427af224747d69814ba835be3b1ed3f0a224c4c2e4075258feb88152dd10c9083240da48d9671e061a8a980a3db3e1b986d3f628
7
- data.tar.gz: dbfa93b6bd5590d6a9474639fb64967c809b6c91a363c917cd9516364aa4dc6d981ac159c2961ffa20f2eaf785b360d31963a85c79bac87fb6337c61d5df4fd4
6
+ metadata.gz: 707e11d863479ae21086db299935460c5e78376a2c0a90f60d516a0c521f7ce95a39341f32523b1499833a5265e573e27f49fef7bd3e402728aa5f95c9be26ac
7
+ data.tar.gz: f604a28cc6aef9ea8a4ae98bc9f3b08d0dd7aa0b86d8f9ea042a28a7dece15ea6557cc0a6feef929d0f8a953b6e82802cb9b0eaa23f3684760b694235d9ebf92
@@ -35,8 +35,9 @@ module Hotdog
35
35
  scope.slice("host:".length, scope.length)
36
36
  }
37
37
  if 0 < hosts.length
38
- if open_db
39
- with_retry do
38
+ # Try reloading database after error as a workaround for nested transaction.
39
+ with_retry(error_handler: ->(error) { reload }) do
40
+ if open_db
40
41
  @db.transaction do
41
42
  hosts.each_slice(SQLITE_LIMIT_COMPOUND_SELECT) do |hosts|
42
43
  execute_db(@db, "DELETE FROM hosts_tags WHERE host_id IN ( SELECT id FROM hosts WHERE name IN (%s) );" % hosts.map { "?" }.join(", "), hosts)
@@ -27,8 +27,9 @@ module Hotdog
27
27
  hosts = args.map { |arg|
28
28
  arg.sub(/\Ahost:/, "")
29
29
  }
30
- if open_db
31
- with_retry do
30
+ # Try reloading database after error as a workaround for nested transaction.
31
+ with_retry(error_handler: ->(error) { reload }) do
32
+ if open_db
32
33
  @db.transaction do
33
34
  create_tags(@db, options[:tags])
34
35
  options[:tags].each do |tag|
@@ -32,8 +32,9 @@ module Hotdog
32
32
  # refresh all persistent.db since there is no way to identify user tags
33
33
  remove_db(@db)
34
34
  else
35
- if open_db
36
- with_retry do
35
+ # Try reloading database after error as a workaround for nested transaction.
36
+ with_retry(error_handler: -> (error) { reload }) do
37
+ if open_db
37
38
  @db.transaction do
38
39
  options[:tags].each do |tag|
39
40
  disassociate_tag_hosts(@db, tag, hosts)
@@ -12,16 +12,13 @@ require "uri"
12
12
  module Hotdog
13
13
  module Commands
14
14
  class BaseCommand
15
- MASK_DATABASE = 0xffff0000
16
- MASK_QUERY = 0x0000ffff
17
-
18
15
  def initialize(application)
19
16
  @application = application
20
17
  @logger = application.logger
21
18
  @options = application.options
22
19
  @dog = nil # lazy initialization
23
20
  @prepared_statements = {}
24
- @persistent_db_path = File.join(@options.fetch(:confdir, "."), "persistent.db")
21
+ @persistent_db_path = File.join(@options.fetch(:confdir, "."), "hotdog.sqlite3")
25
22
  end
26
23
  attr_reader :application
27
24
  attr_reader :logger
@@ -42,11 +39,16 @@ module Hotdog
42
39
  end
43
40
 
44
41
  def reload(options={})
45
- if @db
46
- close_db(@db)
47
- @db = nil
42
+ options = @options.merge(options)
43
+ if options[:offline]
44
+ logger.info("skip reloading on offline mode.")
45
+ else
46
+ if @db
47
+ close_db(@db)
48
+ @db = nil
49
+ end
50
+ update_db(options)
48
51
  end
49
- update_db(options)
50
52
  end
51
53
 
52
54
  def define_options(optparse, options={})
@@ -67,8 +69,7 @@ module Hotdog
67
69
  end
68
70
 
69
71
  def prepare(db, query)
70
- k = (db.hash & MASK_DATABASE) | (query.hash & MASK_QUERY)
71
- @prepared_statements[k] ||= db.prepare(query)
72
+ @prepared_statements[query] ||= db.prepare(query)
72
73
  end
73
74
 
74
75
  def format(result, options={})
@@ -80,6 +81,7 @@ module Hotdog
80
81
  end
81
82
 
82
83
  def get_hosts(host_ids, tags=nil)
84
+ host_ids = Array(host_ids)
83
85
  tags ||= @options[:tags]
84
86
  update_db
85
87
  if host_ids.empty?
@@ -117,6 +119,7 @@ module Hotdog
117
119
  end
118
120
 
119
121
  def get_fields(host_ids)
122
+ host_ids = Array(host_ids)
120
123
  host_ids.each_slice(SQLITE_LIMIT_COMPOUND_SELECT).flat_map { |host_ids|
121
124
  q = "SELECT DISTINCT tags.name FROM hosts_tags " \
122
125
  "INNER JOIN tags ON hosts_tags.tag_id = tags.id " \
@@ -126,6 +129,7 @@ module Hotdog
126
129
  end
127
130
 
128
131
  def get_hosts_fields(host_ids, fields, options={})
132
+ host_ids = Array(host_ids)
129
133
  case fields.length
130
134
  when 0
131
135
  [[], fields]
@@ -156,6 +160,7 @@ module Hotdog
156
160
  end
157
161
 
158
162
  def get_hosts_field(host_ids, field, options={})
163
+ host_ids = Array(host_ids)
159
164
  if /\Ahost\z/i =~ field
160
165
  result = host_ids.each_slice(SQLITE_LIMIT_COMPOUND_SELECT).flat_map { |host_ids|
161
166
  execute("SELECT name FROM hosts WHERE id IN (%s) ORDER BY id;" % host_ids.map { "?" }.join(", "), host_ids).map { |row| row.to_a }
@@ -192,11 +197,10 @@ module Hotdog
192
197
  end
193
198
 
194
199
  def close_db(db, options={})
195
- @prepared_statements = @prepared_statements.reject { |k, statement|
196
- (db.hash & MASK_DATABASE == k & MASK_DATABASE).tap do |delete_p|
197
- statement.close() if delete_p
198
- end
199
- }
200
+ @prepared_statements.each do |query, statement|
201
+ statement.close()
202
+ end
203
+ @prepared_statements.clear()
200
204
  db.close()
201
205
  end
202
206
 
@@ -257,16 +261,15 @@ module Hotdog
257
261
 
258
262
  def create_db(db, options={})
259
263
  options = @options.merge(options)
260
- execute_db(db, "CREATE TABLE IF NOT EXISTS hosts (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(255) NOT NULL COLLATE NOCASE);")
261
- execute_db(db, "CREATE UNIQUE INDEX IF NOT EXISTS hosts_name ON hosts (name);")
262
- execute_db(db, "CREATE TABLE IF NOT EXISTS tags (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(200) NOT NULL COLLATE NOCASE, value VARCHAR(200) NOT NULL COLLATE NOCASE);")
263
- execute_db(db, "CREATE UNIQUE INDEX IF NOT EXISTS tags_name_value ON tags (name, value);")
264
- execute_db(db, "CREATE TABLE IF NOT EXISTS hosts_tags (host_id INTEGER NOT NULL, tag_id INTEGER NOT NULL);")
265
- execute_db(db, "CREATE UNIQUE INDEX IF NOT EXISTS hosts_tags_host_id_tag_id ON hosts_tags (host_id, tag_id);")
266
-
267
264
  all_tags = get_all_tags()
268
-
269
265
  db.transaction do
266
+ execute_db(db, "CREATE TABLE IF NOT EXISTS hosts (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(255) NOT NULL COLLATE NOCASE);")
267
+ execute_db(db, "CREATE UNIQUE INDEX IF NOT EXISTS hosts_name ON hosts (name);")
268
+ execute_db(db, "CREATE TABLE IF NOT EXISTS tags (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(200) NOT NULL COLLATE NOCASE, value VARCHAR(200) NOT NULL COLLATE NOCASE);")
269
+ execute_db(db, "CREATE UNIQUE INDEX IF NOT EXISTS tags_name_value ON tags (name, value);")
270
+ execute_db(db, "CREATE TABLE IF NOT EXISTS hosts_tags (host_id INTEGER NOT NULL, tag_id INTEGER NOT NULL);")
271
+ execute_db(db, "CREATE UNIQUE INDEX IF NOT EXISTS hosts_tags_host_id_tag_id ON hosts_tags (host_id, tag_id);")
272
+
270
273
  known_tags = all_tags.keys.map { |tag| split_tag(tag) }.uniq
271
274
  create_tags(db, known_tags)
272
275
 
@@ -418,11 +421,16 @@ module Hotdog
418
421
  begin
419
422
  return yield
420
423
  rescue => error
421
- logger.warn("#{error.class}: #{error.message}")
424
+ if error_handler = options[:error_handler]
425
+ error_handler.call(error)
426
+ end
427
+ logger.info("#{error.class}: #{error.message}")
422
428
  error.backtrace.each do |frame|
423
- logger.debug("\t#{frame}")
429
+ logger.info("\t#{frame}")
424
430
  end
425
- sleep([options[:retry_delay] || (2<<i), options[:retry_max_delay] || 60].min)
431
+ wait = [options[:retry_delay] || (2<<i), options[:retry_max_delay] || 60].min
432
+ logger.info("will retry after #{wait} seconds....")
433
+ sleep(wait)
426
434
  end
427
435
  end
428
436
  raise("retry count exceeded")
@@ -1,3 +1,3 @@
1
1
  module Hotdog
2
- VERSION = "0.21.2"
2
+ VERSION = "0.22.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hotdog
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.21.2
4
+ version: 0.22.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yamashita Yuu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-03 00:00:00.000000000 Z
11
+ date: 2017-02-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler