99Licensed under GNU General Public License version 3. See LICENSE for details.
1010"""
1111
12- import os , datetime
12+ import os
13+ import datetime
1314import getpass
15+
16+ import pymongo
17+ import bson
18+
1419import DrQueue
1520
1621
22+ def connect_db ():
23+ """
24+ :return: MongoDB connection object
25+ """
26+ host = os .getenv ('DRQUEUE_MONGODB' , None )
27+ if not host :
28+ raise RuntimeError ("Error: DRQUEUE_MONGODB not set!" )
29+
30+ print ("Connect MongoDB on %s" % host )
31+ connection = pymongo .Connection (host )
32+ db = connection ['ipythondb' ]
33+ return db
34+
35+
36+ def get_jobs ():
37+ """
38+ :return: return 'drqueue_jobs'
39+ """
40+ db = connect_db ()
41+ jobs = db ['drqueue_jobs' ]
42+ return jobs
43+
44+
1745class Job (dict ):
1846 """Subclass of dict for collecting Job attribute values."""
1947 def __init__ (self , name , startframe , endframe , blocksize , renderer , scenefile , retries = 1 , owner = getpass .getuser (), options = {}, created_with = None , limits = {}):
@@ -101,36 +129,26 @@ def __init__(self, name, startframe, endframe, blocksize, renderer, scenefile, r
101129
102130 @staticmethod
103131 def store_db (job ):
104- import pymongo
105132 """store job information in MongoDB"""
106- connection = pymongo .Connection (os .getenv ('DRQUEUE_MONGODB' ))
107- db = connection ['ipythondb' ]
108- jobs = db ['drqueue_jobs' ]
133+ jobs = get_jobs ()
109134 job_id = jobs .insert (job )
110135 job ['_id' ] = str (job ['_id' ])
111136 return job_id
112137
113138
114139 @staticmethod
115140 def update_db (job ):
116- import pymongo
117141 """update job information in MongoDB"""
118- connection = pymongo .Connection (os .getenv ('DRQUEUE_MONGODB' ))
119- db = connection ['ipythondb' ]
120- jobs = db ['drqueue_jobs' ]
142+ jobs = get_jobs ()
121143 job_id = jobs .save (job )
122144 job ['_id' ] = str (job ['_id' ])
123145 return job_id
124146
125147
126148 @staticmethod
127149 def query_db (job_id ):
128- import pymongo
129- import bson
130150 """query job information from MongoDB"""
131- connection = pymongo .Connection (os .getenv ('DRQUEUE_MONGODB' ))
132- db = connection ['ipythondb' ]
133- jobs = db ['drqueue_jobs' ]
151+ jobs = get_jobs ()
134152 try :
135153 job = jobs .find_one ({"_id" : bson .ObjectId (job_id )})
136154 except bson .errors .InvalidId :
@@ -141,22 +159,15 @@ def query_db(job_id):
141159
142160 @staticmethod
143161 def delete_from_db (job_id ):
144- import pymongo
145- import bson
146162 """query job information from MongoDB"""
147- connection = pymongo .Connection (os .getenv ('DRQUEUE_MONGODB' ))
148- db = connection ['ipythondb' ]
149- jobs = db ['drqueue_jobs' ]
163+ jobs = get_jobs ()
150164 return jobs .remove ({"_id" : bson .ObjectId (job_id )})
151165
152166
153167 @staticmethod
154168 def query_jobnames ():
155- import pymongo
156169 """query job names from MongoDB"""
157- connection = pymongo .Connection (os .getenv ('DRQUEUE_MONGODB' ))
158- db = connection ['ipythondb' ]
159- jobs = db ['drqueue_jobs' ]
170+ jobs = get_jobs ()
160171 names = []
161172 for job in jobs .find ():
162173 names .append (job ['name' ])
@@ -165,21 +176,15 @@ def query_jobnames():
165176
166177 @staticmethod
167178 def query_job_by_name (job_name ):
168- import pymongo
169179 """query job information from MongoDB by name"""
170- connection = pymongo .Connection (os .getenv ('DRQUEUE_MONGODB' ))
171- db = connection ['ipythondb' ]
172- jobs = db ['drqueue_jobs' ]
180+ jobs = get_jobs ()
173181 job = jobs .find_one ({"name" : job_name })
174182 return job
175183
176184
177185 @staticmethod
178186 def query_job_list ():
179- import pymongo
180187 """query list of jobs from MongoDB"""
181- connection = pymongo .Connection (os .getenv ('DRQUEUE_MONGODB' ))
182- db = connection ['ipythondb' ]
183- jobs = db ['drqueue_jobs' ]
188+ jobs = get_jobs ()
184189 return list (jobs .find ())
185190
0 commit comments