Navigation Menu

Skip to content

Commit

Permalink
Extract common running code
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Oct 27, 2017
1 parent 468e196 commit 5b7e5d6
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 50 deletions.
33 changes: 10 additions & 23 deletions lib/groonga/client/command-line/groonga-client-index-check.rb
Expand Up @@ -17,6 +17,7 @@

require "groonga/client"
require "groonga/client/command-line/parser"
require "groonga/client/command-line/runner"

module Groonga
class Client
Expand All @@ -39,7 +40,7 @@ def run(arguments)

parser.open_client do |client|
checker = Checker.new(client, @methods, indexes)
checker.check
checker.run
end
end

Expand Down Expand Up @@ -67,34 +68,20 @@ def parse_command_line(parser)
end
end

class Checker
class Checker < Runner
def initialize(client, methods, targets)
@client = client
super(client)
@methods = methods
@targets = targets
end

def check
catch(:fail) do
succeeded = true
@methods.each do |method|
succeeded = false unless __send__("check_#{method}")
end
succeeded
end
end

def abort_run(message)
$stderr.puts(message)
throw(:fail, false)
end

def execute_command(name, arguments={})
response = @client.execute(name, arguments)
unless response.success?
abort_run("Failed to run #{name}: #{response.inspect}")
private
def run_internal
succeeded = true
@methods.each do |method|
succeeded = false unless __send__("check_#{method}")
end
response
succeeded
end

def table_list
Expand Down
39 changes: 12 additions & 27 deletions lib/groonga/client/command-line/groonga-client-index-recreate.rb
Expand Up @@ -18,6 +18,7 @@

require "groonga/client"
require "groonga/client/command-line/parser"
require "groonga/client/command-line/runner"

module Groonga
class Client
Expand All @@ -36,8 +37,8 @@ def run(arguments)
end

parser.open_client do |client|
runner = Runner.new(client, @interval, indexes)
runner.run do
recreator = Recreator.new(client, @interval, indexes)
recreator.run do
@n_workers.times do
client.database_unmap
end
Expand Down Expand Up @@ -71,39 +72,23 @@ def parse_command_line(parser)
end
end

class Runner
class Recreator < Runner
def initialize(client, interval, target_indexes)
@client = client
super(client)
@interval = interval
@target_indexes = target_indexes
@now = Time.now
end

def run
catch do |tag|
@abort_tag = tag
alias_column = ensure_alias_column
@target_indexes.each do |index|
current_index = recreate_index(index, alias_column)
remove_old_indexes(index, current_index)
end
yield if block_given?
true
end
end

private
def abort_run(message)
$stderr.puts(message)
throw(@abort_tag, false)
end

def execute_command(name, arguments={})
response = @client.execute(name, arguments)
unless response.success?
abort_run("Failed to run #{name}: #{response.inspect}")
def run_internal
alias_column = ensure_alias_column
@target_indexes.each do |index|
current_index = recreate_index(index, alias_column)
remove_old_indexes(index, current_index)
end
response
yield if block_given?
true
end

def config_get(key)
Expand Down
48 changes: 48 additions & 0 deletions lib/groonga/client/command-line/runner.rb
@@ -0,0 +1,48 @@
# Copyright (C) 2017 Kouhei Sutou <kou@clear-code.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

module Groonga
class Client
module CommandLine
class Runner
def initialize(client)
@client = client
end

def run(&block)
catch do |tag|
@abort_tag = tag
run_internal(&block)
end
end

private
def abort_run(message)
$stderr.puts(message)
throw(@abort_tag, false)
end

def execute_command(name, arguments={})
response = @client.execute(name, arguments)
unless response.success?
abort_run("Failed to run #{name}: #{response.inspect}")
end
response
end
end
end
end
end

0 comments on commit 5b7e5d6

Please sign in to comment.