Method: Fluent::PluginHelper::Storage#storage_create

Defined in:
lib/fluent/plugin_helper/storage.rb

#storage_create(usage: '', type: nil, conf: nil, default_type: nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/fluent/plugin_helper/storage.rb', line 32

def storage_create(usage: '', type: nil, conf: nil, default_type: nil)
  if conf && conf.respond_to?(:arg) && !conf.arg.empty?
    usage = conf.arg
  end
  if !usage.empty? && usage !~ /^[a-zA-Z][-_.a-zA-Z0-9]*$/
    raise Fluent::ConfigError, "Argument in <storage ARG> uses invalid characters: '#{usage}'"
  end

  s = @_storages[usage]
  if s && s.running
    return s.storage
  elsif s
    # storage is already created, but not loaded / started
  else # !s
    type = if type
             type
           elsif conf && conf.respond_to?(:[])
             raise Fluent::ConfigError, "@type is required in <storage>" unless conf['@type']
             conf['@type']
           elsif default_type
             default_type
           else
             raise ArgumentError, "BUG: both type and conf are not specified"
           end
    storage = Plugin.new_storage(type, parent: self)
    config = case conf
             when Fluent::Config::Element
               conf
             when Hash
               # in code, programmer may use symbols as keys, but Element needs strings
               conf = Hash[conf.map{|k,v| [k.to_s, v]}]
               Fluent::Config::Element.new('storage', usage, conf, [])
             when nil
               Fluent::Config::Element.new('storage', usage, {'@type' => type}, [])
             else
               raise ArgumentError, "BUG: conf must be a Element, Hash (or unspecified), but '#{conf.class}'"
             end
    storage.configure(config)
    if @_storages_started
      storage.start
    end
    s = @_storages[usage] = StorageState.new(wrap_instance(storage), false)
  end

  s.storage
end