Skip to content

Commit fb65ff1

Browse files
josenavasantgonza
authored andcommitted
Fixing the redis DB (#2277)
* Fixing the redis DB * Addressing @antgonza's comments
1 parent 22d52f0 commit fb65ff1

File tree

2 files changed

+108
-7
lines changed

2 files changed

+108
-7
lines changed

qiita_db/environment_manager.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ def drop_and_rebuild_tst_database():
308308
"""Drops the qiita schema and rebuilds the test database
309309
"""
310310
with qdb.sql_connection.TRN:
311+
r_client.flushdb()
311312
# Drop the schema
312313
qdb.sql_connection.TRN.add("DROP SCHEMA IF EXISTS qiita CASCADE")
313314
# Set the database to unpatched
@@ -318,8 +319,6 @@ def drop_and_rebuild_tst_database():
318319

319320
qdb.sql_connection.TRN.execute()
320321

321-
r_client.flushdb()
322-
323322

324323
def reset_test_database(wrapped_fn):
325324
"""Decorator that drops the qiita schema, rebuilds and repopulates the

qiita_db/support_files/patches/python_patches/58.py

Lines changed: 107 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,81 @@
66
# The full license is in the file LICENSE, distributed with this software.
77
# -----------------------------------------------------------------------------
88

9+
from json import loads, dumps
10+
11+
from qiita_core.qiita_settings import r_client
912
from qiita_db.sql_connection import TRN
10-
from qiita_db.software import Software, Command
13+
from qiita_db.software import Software, Command, Parameters
14+
from qiita_db.processing_job import ProcessingJob
15+
from qiita_db.study import Study
16+
from qiita_db.exceptions import QiitaDBUnknownIDError
17+
from qiita_db.metadata_template.prep_template import PrepTemplate
18+
19+
20+
def correct_redis_data(key, cmd, values_dict, user):
21+
"""Corrects the data stored in the redis DB
22+
23+
Parameters
24+
----------
25+
key: str
26+
The redis key to fix
27+
cmd : qiita_db.software.Command
28+
Command to use to create the processing job
29+
values_dict : dict
30+
Dictionary used to instantiate the parameters of the command
31+
user : qiita_db.user. User
32+
The user that will own the job
33+
"""
34+
info = r_client.get(key)
35+
if info:
36+
info = loads(info)
37+
if info['job_id'] is not None:
38+
if 'is_qiita_job' in info:
39+
if info['is_qiita_job']:
40+
job = ProcessingJob(info['job_id'])
41+
payload = {'job_id': info['job_id'],
42+
'alert_type': info['status'],
43+
'alert_msg': info['alert_msg']}
44+
r_client.set(key, dumps(payload))
45+
else:
46+
# These jobs don't contain any information on the live
47+
# dump. We can safely delete the key
48+
r_client.delete(key)
49+
else:
50+
# These jobs don't contain any information on the live
51+
# dump. We can safely delete the key
52+
r_client.delete(key)
53+
else:
54+
# Job is null, we have the information here
55+
if info['status'] == 'success':
56+
# In the success case no information is stored. We can
57+
# safely delete the key
58+
r_client.delete(key)
59+
elif info['status'] == 'warning':
60+
# In case of warning the key message stores the warning
61+
# message. We need to create a new job, mark it as
62+
# successful and store the error message as expected by
63+
# the new structure
64+
params = Parameters.load(cmd, values_dict=values_dict)
65+
job = ProcessingJob.create(user, params)
66+
job._set_status('success')
67+
payload = {'job_id': job.id,
68+
'alert_type': 'warning',
69+
'alert_msg': info['message']}
70+
r_client.set(key, dumps(payload))
71+
else:
72+
# The status is error. The key message stores the error
73+
# message. We need to create a new job and mark it as
74+
# failed with the given error message
75+
params = Parameters.load(cmd, values_dict=values_dict)
76+
job = ProcessingJob(user, params)
77+
job._set_error(info['message'])
78+
payload = {'job_id': job.id}
79+
r_client.set(key, dumps(payload))
80+
else:
81+
# The key doesn't contain any information. Delete the key
82+
r_client.delete(key)
83+
1184

1285
with TRN:
1386
# Retrieve the Qiita plugin
@@ -44,8 +117,8 @@
44117

45118
# Create the update sample template command
46119
parameters = {'study': ['integer', None], 'template_fp': ['string', None]}
47-
Command.create(qiita_plugin, "update_sample_template",
48-
"Updates the sample template", parameters)
120+
st_cmd = Command.create(qiita_plugin, "update_sample_template",
121+
"Updates the sample template", parameters)
49122

50123
# Create the delete sample template command
51124
parameters = {'study': ['integer', None]}
@@ -55,8 +128,8 @@
55128
# Create the update prep template command
56129
parameters = {'prep_template': ['integer', None],
57130
'template_fp': ['string', None]}
58-
Command.create(qiita_plugin, "update_prep_template",
59-
"Updates the prep template", parameters)
131+
pt_cmd = Command.create(qiita_plugin, "update_prep_template",
132+
"Updates the prep template", parameters)
60133

61134
# Create the delete sample or column command
62135
parameters = {
@@ -72,3 +145,32 @@
72145
parameters = {'job_id': ['string', None], 'payload': ['string', None]}
73146
Command.create(qiita_plugin, "complete_job", "Completes a given job",
74147
parameters)
148+
149+
# Assumptions on the structure of the data in the redis database has
150+
# changed, we need to fix to avoid failures
151+
# Get all the sample template keys
152+
for key in r_client.keys('sample_template_[0-9]*'):
153+
try:
154+
study = Study(int(key.split('_')[-1]))
155+
user = study.owner
156+
except QiitaDBUnknownIDError:
157+
# This means that the study no longer exists - delete the key
158+
# and continue
159+
r_client.delete(key)
160+
continue
161+
values_dict = {'study': study.id, 'template_fp': 'ignored-patch58'}
162+
correct_redis_data(key, st_cmd, values_dict, user)
163+
164+
# Get all the prep template keys
165+
for key in r_client.keys('prep_template_[0-9]*'):
166+
try:
167+
pt = PrepTemplate(int(key.split('_')[-1]))
168+
user = Study(pt.study_id).owner
169+
except QiitaDBUnknownIDError:
170+
# This means that the prep template no longer exists - delete the
171+
# key and continue
172+
r_client.delete(key)
173+
continue
174+
values_dict = {'prep_template': pt.id,
175+
'template_fp': 'ignored-patch58'}
176+
correct_redis_data(key, pt_cmd, values_dict, user)

0 commit comments

Comments
 (0)