|
6 | 6 | # The full license is in the file LICENSE, distributed with this software.
|
7 | 7 | # -----------------------------------------------------------------------------
|
8 | 8 |
|
| 9 | +from json import loads, dumps |
| 10 | + |
| 11 | +from qiita_core.qiita_settings import r_client |
9 | 12 | 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 | + |
11 | 84 |
|
12 | 85 | with TRN:
|
13 | 86 | # Retrieve the Qiita plugin
|
|
44 | 117 |
|
45 | 118 | # Create the update sample template command
|
46 | 119 | 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) |
49 | 122 |
|
50 | 123 | # Create the delete sample template command
|
51 | 124 | parameters = {'study': ['integer', None]}
|
|
55 | 128 | # Create the update prep template command
|
56 | 129 | parameters = {'prep_template': ['integer', None],
|
57 | 130 | '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) |
60 | 133 |
|
61 | 134 | # Create the delete sample or column command
|
62 | 135 | parameters = {
|
|
72 | 145 | parameters = {'job_id': ['string', None], 'payload': ['string', None]}
|
73 | 146 | Command.create(qiita_plugin, "complete_job", "Completes a given job",
|
74 | 147 | 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