Conditions | 20 |
Total Lines | 102 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like Processor.on_task() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | #!/usr/bin/env python |
||
102 | def on_task(self, task, response): |
||
103 | '''Deal one task''' |
||
104 | start_time = time.time() |
||
105 | response = rebuild_response(response) |
||
106 | |||
107 | try: |
||
108 | assert 'taskid' in task, 'need taskid in task' |
||
109 | project = task['project'] |
||
110 | updatetime = task.get('project_updatetime', None) |
||
111 | md5sum = task.get('project_md5sum', None) |
||
112 | project_data = self.project_manager.get(project, updatetime, md5sum) |
||
113 | assert project_data, "no such project!" |
||
114 | if project_data.get('exception'): |
||
115 | ret = ProcessorResult(logs=(project_data.get('exception_log'), ), |
||
116 | exception=project_data['exception']) |
||
117 | else: |
||
118 | ret = project_data['instance'].run_task( |
||
119 | project_data['module'], task, response) |
||
120 | except Exception as e: |
||
121 | logstr = traceback.format_exc() |
||
122 | ret = ProcessorResult(logs=(logstr, ), exception=e) |
||
123 | process_time = time.time() - start_time |
||
124 | |||
125 | if not ret.extinfo.get('not_send_status', False): |
||
126 | if ret.exception: |
||
127 | track_headers = dict(response.headers) |
||
128 | else: |
||
129 | track_headers = {} |
||
130 | for name in ('etag', 'last-modified'): |
||
131 | if name not in response.headers: |
||
132 | continue |
||
133 | track_headers[name] = response.headers[name] |
||
134 | |||
135 | status_pack = { |
||
136 | 'taskid': task['taskid'], |
||
137 | 'project': task['project'], |
||
138 | 'url': task.get('url'), |
||
139 | 'track': { |
||
140 | 'fetch': { |
||
141 | 'ok': response.isok(), |
||
142 | 'redirect_url': response.url if response.url != response.orig_url else None, |
||
143 | 'time': response.time, |
||
144 | 'error': response.error, |
||
145 | 'status_code': response.status_code, |
||
146 | 'encoding': getattr(response, '_encoding', None), |
||
147 | 'headers': track_headers, |
||
148 | 'content': response.text[:500] if ret.exception else None, |
||
149 | }, |
||
150 | 'process': { |
||
151 | 'ok': not ret.exception, |
||
152 | 'time': process_time, |
||
153 | 'follows': len(ret.follows), |
||
154 | 'result': ( |
||
155 | None if ret.result is None |
||
156 | else utils.text(ret.result)[:self.RESULT_RESULT_LIMIT] |
||
157 | ), |
||
158 | 'logs': ret.logstr()[-self.RESULT_LOGS_LIMIT:], |
||
159 | 'exception': ret.exception, |
||
160 | }, |
||
161 | 'save': ret.save, |
||
162 | }, |
||
163 | } |
||
164 | if 'schedule' in task: |
||
165 | status_pack['schedule'] = task['schedule'] |
||
166 | |||
167 | # FIXME: unicode_obj should used in scheduler before store to database |
||
168 | # it's used here for performance. |
||
169 | self.status_queue.put(utils.unicode_obj(status_pack)) |
||
170 | |||
171 | # FIXME: unicode_obj should used in scheduler before store to database |
||
172 | # it's used here for performance. |
||
173 | if ret.follows: |
||
174 | for each in (ret.follows[x:x + 1000] for x in range(0, len(ret.follows), 1000)): |
||
175 | self.newtask_queue.put([utils.unicode_obj(newtask) for newtask in each]) |
||
176 | |||
177 | for project, msg, url in ret.messages: |
||
178 | try: |
||
179 | self.on_task({ |
||
180 | 'taskid': utils.md5string(url), |
||
181 | 'project': project, |
||
182 | 'url': url, |
||
183 | 'process': { |
||
184 | 'callback': '_on_message', |
||
185 | } |
||
186 | }, { |
||
187 | 'status_code': 200, |
||
188 | 'url': url, |
||
189 | 'save': (task['project'], msg), |
||
190 | }) |
||
191 | except Exception as e: |
||
192 | logger.exception('Sending message error.') |
||
193 | continue |
||
194 | |||
195 | if ret.exception: |
||
196 | logger_func = logger.error |
||
197 | else: |
||
198 | logger_func = logger.info |
||
199 | logger_func('process %s:%s %s -> [%d] len:%d -> result:%.10r fol:%d msg:%d err:%r' % ( |
||
200 | task['project'], task['taskid'], |
||
201 | task.get('url'), response.status_code, len(response.content), |
||
202 | ret.result, len(ret.follows), len(ret.messages), ret.exception)) |
||
203 | return True |
||
204 | |||
230 |