[Groonga-commit] ranguba/chupa-text at 3a0dcf3 [master] Add support for timeout as string again

Back to archive index
Sutou Kouhei null+****@clear*****
Fri Jun 14 11:32:11 JST 2019


Sutou Kouhei	2019-06-14 11:32:11 +0900 (Fri, 14 Jun 2019)

  Revision: 3a0dcf348007349f1ac9bb6df3c6948f1ae40f8f
  https://github.com/ranguba/chupa-text/commit/3a0dcf348007349f1ac9bb6df3c6948f1ae40f8f

  Message:
    Add support for timeout as string again

  Added files:
    lib/chupa-text/timeout-value.rb
  Modified files:
    lib/chupa-text.rb
    lib/chupa-text/external-command.rb
    lib/chupa-text/extractor.rb
    lib/chupa-text/loggable.rb
    test/test-external-command.rb

  Modified: lib/chupa-text.rb (+3 -1)
===================================================================
--- lib/chupa-text.rb    2019-06-13 18:57:24 +0900 (4d567a8)
+++ lib/chupa-text.rb    2019-06-14 11:32:11 +0900 (af95540)
@@ -1,4 +1,4 @@
-# Copyright (C) 2013  Kouhei Sutou <kou****@clear*****>
+# Copyright (C) 2013-2019  Sutou Kouhei <kou****@clear*****>
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -26,6 +26,8 @@ require "chupa-text/logger"
 require "chupa-text/loggable"
 require "chupa-text/unzippable"
 
+require "chupa-text/timeout-value"
+
 require "chupa-text/configuration"
 require "chupa-text/configuration-loader"
 require "chupa-text/mime-type"

  Modified: lib/chupa-text/external-command.rb (+5 -4)
===================================================================
--- lib/chupa-text/external-command.rb    2019-06-13 18:57:24 +0900 (bcef54b)
+++ lib/chupa-text/external-command.rb    2019-06-14 11:32:11 +0900 (2ea2722)
@@ -255,20 +255,21 @@ module ChupaText
     end
 
     def log_invalid_value(tag, value, type)
-      warn("#{log_tag}#{tag}[invalid] <#{value}>(#{type})")
+      super("#{log_tag}#{tag}", value, type)
     end
 
     def wait_process(pid, timeout, soft_timeout)
       tag = "[timeout]"
-      timeout = parse_time(tag, timeout || self.class.default_timeout)
-      soft_timeout = parse_time(tag, soft_timeout)
+      timeout = TimeoutValue.new(tag, timeout || self.class.default_timeout).raw
+      soft_timeout = TimeoutValue.new(tag, soft_timeout).raw
       if timeout
         timeout = soft_timeout if soft_timeout and soft_timeout < timeout
       else
         timeout = soft_timeout
       end
       if timeout
-        info("#{log_tag}#{tag}[use] <#{timeout}s>: <#{pid}>")
+        info("#{log_tag}#{tag}[use] " +
+             "<#{TimeoutValue.new(tag, timeout)}>: <#{pid}>")
         status = wait_process_timeout(pid, timeout)
         return status if status
         info("#{log_tag}#{tag}[terminate] <#{pid}>")

  Modified: lib/chupa-text/extractor.rb (+1 -1)
===================================================================
--- lib/chupa-text/extractor.rb    2019-06-13 18:57:24 +0900 (7e71038)
+++ lib/chupa-text/extractor.rb    2019-06-14 11:32:11 +0900 (c7c3f37)
@@ -123,7 +123,7 @@ module ChupaText
     end
 
     def with_timeout(data, &block)
-      timeout = data.timeout
+      timeout = TimeoutValue.new("#{log_tag}[timeout]", data.timeout).raw
       if timeout
         begin
           Timeout.timeout(timeout, &block)

  Modified: lib/chupa-text/loggable.rb (+5 -1)
===================================================================
--- lib/chupa-text/loggable.rb    2019-06-13 18:57:24 +0900 (a01fb13)
+++ lib/chupa-text/loggable.rb    2019-06-14 11:32:11 +0900 (45647bd)
@@ -1,4 +1,4 @@
-# Copyright (C) 2013  Kouhei Sutou <kou****@clear*****>
+# Copyright (C) 2013-2019  Sutou Kouhei <kou****@clear*****>
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -45,5 +45,9 @@ module ChupaText
     def unknown(*arguments, &block)
       logger.unknown(*arguments, &block)
     end
+
+    def log_invalid_value(tag, value, type)
+      warn("#{tag}[invalid] <#{value}>(#{type})")
+    end
   end
 end

  Added: lib/chupa-text/timeout-value.rb (+76 -0) 100644
===================================================================
--- /dev/null
+++ lib/chupa-text/timeout-value.rb    2019-06-14 11:32:11 +0900 (27a83d2)
@@ -0,0 +1,76 @@
+# Copyright (C) 2019  Sutou Kouhei <kou****@clear*****>
+#
+# 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
+
+require "English"
+
+module ChupaText
+  class TimeoutValue
+    include Comparable
+    include Loggable
+
+    attr_reader :raw
+    def initialize(tag, value)
+      value = parse(value) if value.is_a?(String)
+      @raw = value
+    end
+
+    def to_s
+      return "" if****@raw*****?
+
+      if @raw < 1
+        "%.2fms" % (@raw * 1000.0)
+      elsif @raw < 60
+        "%.2fs" % @raw
+      elsif @raw < (60 * 60)
+        "%.2fm" % (@raw / 60.0)
+      else
+        "%.2fh" % (@raw / 60.0 / 60.0)
+      end
+    end
+
+    private
+    def parse(value)
+      case value
+      when nil
+        nil
+      when Numeric
+        value
+      else
+        return nil if value.empty?
+        scale = 1
+        case value
+        when /h\z/i
+          scale = 60 * 60
+          number = $PREMATCH
+        when /m\z/i
+          scale = 60
+          number = $PREMATCH
+        when /s\z/i
+          number = $PREMATCH
+        else
+          number = value
+        end
+        begin
+          number = Float(number)
+        rescue ArgumentError
+          log_invalid_value(@tag, value, "time")
+          return nil
+        end
+        (number * scale).to_f
+      end
+    end
+  end
+end

  Modified: test/test-external-command.rb (+8 -8)
===================================================================
--- test/test-external-command.rb    2019-06-13 18:57:24 +0900 (53a0842)
+++ test/test-external-command.rb    2019-06-14 11:32:11 +0900 (43d4b05)
@@ -102,7 +102,7 @@ class TestExternalCommand < Test::Unit::TestCase
       assert_equal([
                      [
                        :info,
-                       "[external-command][timeout][use] <60.0s>: <#{pid}>",
+                       "[external-command][timeout][use] <1.00m>: <#{pid}>",
                      ]
                    ],
                    messages)
@@ -117,7 +117,7 @@ class TestExternalCommand < Test::Unit::TestCase
       assert_equal([
                      [
                        :info,
-                       "[external-command][timeout][use] <60.0s>: <#{pid}>",
+                       "[external-command][timeout][use] <1.00m>: <#{pid}>",
                      ]
                    ],
                    messages)
@@ -132,7 +132,7 @@ class TestExternalCommand < Test::Unit::TestCase
       assert_equal([
                      [
                        :info,
-                       "[external-command][timeout][use] <30.0s>: <#{pid}>",
+                       "[external-command][timeout][use] <30.00s>: <#{pid}>",
                      ]
                    ],
                    messages)
@@ -147,7 +147,7 @@ class TestExternalCommand < Test::Unit::TestCase
       assert_equal([
                      [
                        :info,
-                       "[external-command][timeout][use] <30.0s>: <#{pid}>",
+                       "[external-command][timeout][use] <30.00s>: <#{pid}>",
                      ]
                    ],
                    messages)
@@ -162,7 +162,7 @@ class TestExternalCommand < Test::Unit::TestCase
       assert_equal([
                      [
                        :info,
-                       "[external-command][timeout][use] <60.0s>: <#{pid}>",
+                       "[external-command][timeout][use] <1.00m>: <#{pid}>",
                      ]
                    ],
                    messages)
@@ -178,7 +178,7 @@ class TestExternalCommand < Test::Unit::TestCase
       assert_equal([
                      [
                        :info,
-                       "[external-command][timeout][use] <60.0s>: <#{pid}>",
+                       "[external-command][timeout][use] <1.00m>: <#{pid}>",
                      ]
                    ],
                    messages)
@@ -194,7 +194,7 @@ class TestExternalCommand < Test::Unit::TestCase
       assert_equal([
                      [
                        :info,
-                       "[external-command][timeout][use] <30.0s>: <#{pid}>",
+                       "[external-command][timeout][use] <30.00s>: <#{pid}>",
                      ]
                    ],
                    messages)
@@ -209,7 +209,7 @@ class TestExternalCommand < Test::Unit::TestCase
       assert_equal([
                      [
                        :info,
-                       "[external-command][timeout][use] <30.0s>: <#{pid}>",
+                       "[external-command][timeout][use] <30.00s>: <#{pid}>",
                      ]
                    ],
                    messages)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20190614/4b77200c/attachment-0001.html>


More information about the Groonga-commit mailing list
Back to archive index