Skip to content

Commit 92bc3db

Browse files
author
Shakeel Mohamed
committed
Add Service.job() helper method to get a Job by its sid.
Also added tests & an example.
1 parent 6a234e7 commit 92bc3db

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

examples/get_job.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python
2+
#
3+
# Copyright 2011-2015 Splunk, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License"): you may
6+
# not use this file except in compliance with the License. You may obtain
7+
# a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
# License for the specific language governing permissions and limitations
15+
# under the License.
16+
17+
"""A simple example showing to use the Service.job method to retrieve
18+
a search Job by its sid.
19+
"""
20+
21+
import sys
22+
import os
23+
import time
24+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
25+
import splunklib.client as client
26+
27+
try:
28+
from utils import *
29+
except ImportError:
30+
raise Exception("Add the SDK repository to your PYTHONPATH to run the examples "
31+
"(e.g., export PYTHONPATH=~/splunk-sdk-python.")
32+
33+
def main(argv):
34+
opts = parse(argv, {}, ".splunkrc")
35+
service = client.connect(**opts.kwargs)
36+
37+
# Execute a simple search, and store the sid
38+
sid = service.search("search index=_internal | head 5").sid
39+
40+
# Now, we can get the `Job`
41+
job = service.job(sid)
42+
43+
# Wait for the job to complete
44+
while not job.is_done():
45+
time.sleep(1)
46+
47+
print "Number of events found: %d" % int(job["eventCount"])
48+
49+
if __name__ == "__main__":
50+
main(sys.argv[1:])
51+

splunklib/client.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,13 @@ def inputs(self):
457457
"""
458458
return Inputs(self)
459459

460+
def job(self, sid):
461+
"""Retrieves a search job by sid.
462+
463+
:return: A :class:`Job` object.
464+
"""
465+
return Job(self, sid).refresh()
466+
460467
@property
461468
def jobs(self):
462469
"""Returns the collection of current search jobs.

tests/test_job.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,18 @@ def test_read_jobs(self):
180180
job.refresh()
181181
self.check_job(job)
182182

183+
def test_get_job(self):
184+
sid = self.service.search("search index=_internal | head 10").sid
185+
self.assertTrue(len(sid) > 0)
186+
187+
job = self.service.job(sid)
188+
self.assertIsNotNone(job)
189+
190+
while not job.is_done():
191+
sleep(1)
192+
193+
self.assertEqual(10, int(job["eventCount"]))
194+
self.assertEqual(10, int(job["resultCount"]))
183195

184196
class TestJobWithDelayedDone(testlib.SDKTestCase):
185197
def setUp(self):

0 commit comments

Comments
 (0)