Class: Splunk::Jobs

Inherits:
Collection show all
Defined in:
lib/splunk-sdk-ruby/collection/jobs.rb

Overview

Class representing a search job in Splunk.

Jobs adds two additional methods to Collection to start additional kinds of search job. The basic create method starts a normal, asynchronous search job. The two new methods, create_oneshot and create_stream, creating oneshot and streaming searches, respectively, which block until the search finishes and return the results directly.

Instance Attribute Summary

Attributes inherited from ReadOnlyCollection

#entity_class, #resource, #service

Instance Method Summary collapse

Methods inherited from Collection

#delete, #delete_if

Methods inherited from ReadOnlyCollection

#assoc, #each, #each_key, #each_pair, #each_value, #empty?, #fetch, #has_key?, #keys, #length, #values

Constructor Details

#initialize(service) ⇒ Jobs

Returns a new instance of Jobs.



37
38
39
40
41
42
43
# File 'lib/splunk-sdk-ruby/collection/jobs.rb', line 37

def initialize(service)
  super(service, PATH_JOBS, entity_class=Job)

  # +Jobs+ is one of the inconsistent collections where 0 means
  # list all, not -1.
  @infinite_count = 0
end

Instance Method Details

#atom_entry_to_entity(entry) ⇒ Object

:nodoc:



45
46
47
48
# File 'lib/splunk-sdk-ruby/collection/jobs.rb', line 45

def atom_entry_to_entity(entry) # :nodoc:
  sid = entry["content"]["sid"]
  return Job.new(@service, sid)
end

#create(query, args = {}) ⇒ Object

Creates an asynchronous search job.

The search job requires a query, and takes a hash of other, optional arguments, which are documented in the Splunk REST documentation.



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/splunk-sdk-ruby/collection/jobs.rb', line 56

def create(query, args={})
  if args.has_key?(:exec_mode)
    raise ArgumentError.new("Cannot specify exec_mode for create. Use " +
                                "create_oneshot or create_stream instead.")
  end

  args['search'] = query
  response = @service.request(:method => :POST,
                              :resource => @resource,
                              :body => args)
  sid = Splunk::text_at_xpath("/response/sid", response.body)
  Job.new(@service, sid)
end

#create_export(query, args = {}) ⇒ Object

Creates a blocking search without transforming search commands.

The create_export method starts a search query, and any optional arguments specified in a hash (which are identical to those taken by create). It then blocks until the job is finished, and returns the events found by the job before any transforming search commands (equivalent to calling events on a Job).

Returns: a stream readable by MultiResultsReader.



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/splunk-sdk-ruby/collection/jobs.rb', line 105

def create_export(query, args={})
  args["search"] = query
  # Suppress segmentation (<sg> tags in the XML response) by default:
  if !args.has_key?(:segmentation)
    args[:segmentation] = "none"
  end
  response = @service.request(:method => :GET,
                              :resource => @resource + ["export"],
                              :query => args)
  return ExportStream.new(response.body)
end

#create_oneshot(query, args = {}) ⇒ Object

Creates a blocking search.

The create_oneshot method starts a search query, and any optional arguments specified in a hash (which are identical to those taken by create). It then blocks until the job finished, and returns the results, as transformed by any transforming search commands in query (equivalent to calling the results method on a Job).

Returns: a stream readable by ResultsReader.



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/splunk-sdk-ruby/collection/jobs.rb', line 81

def create_oneshot(query, args={})
  args[:search] = query
  args[:exec_mode] = 'oneshot'
  # Suppress segmentation (<sg> tags in the XML response) by default:
  if !args.has_key?(:segmentation)
    args[:segmentation] = "none"
  end
  response = @service.request(:method => :POST,
                              :resource => @resource,
                              :body => args)
  return response.body
end

#create_stream(query, args = {}) ⇒ Object

Deprecated.



118
119
120
121
# File 'lib/splunk-sdk-ruby/collection/jobs.rb', line 118

def create_stream(query, args={}) # :nodoc:
  warn "[DEPRECATION] Jobs#create_stream is deprecated. Use Jobs#create_export instead."
  create_export(query, args)
end