1
1
import asyncio
2
2
import inspect
3
- import io
4
3
from concurrent .futures import ThreadPoolExecutor
5
4
from logging import getLogger
6
5
from time import time
11
10
from taskiq .abc .broker import AsyncBroker
12
11
from taskiq .abc .middleware import TaskiqMiddleware
13
12
from taskiq .cli .worker .args import WorkerArgs
14
- from taskiq .cli .worker .log_collector import log_collector
15
13
from taskiq .cli .worker .params_parser import parse_params
16
14
from taskiq .context import Context
17
15
from taskiq .message import BrokerMessage , TaskiqMessage
@@ -150,8 +148,6 @@ async def run_task( # noqa: C901, WPS210
150
148
:return: result of execution.
151
149
"""
152
150
loop = asyncio .get_running_loop ()
153
- # Buffer to capture logs.
154
- logs = io .StringIO ()
155
151
returned = None
156
152
found_exception = None
157
153
signature = self .task_signatures .get (message .task_name )
@@ -160,55 +156,51 @@ async def run_task( # noqa: C901, WPS210
160
156
signature = None
161
157
parse_params (signature , self .task_hints .get (message .task_name ) or {}, message )
162
158
163
- # Captures function's logs.
164
- with log_collector (logs , self .cli_args .log_collector_format ):
165
- dep_ctx = None
166
- if dependency_graph :
167
- # Create a context for dependency resolving.
168
- dep_ctx = dependency_graph .async_ctx (
169
- {
170
- Context : Context (message , self .broker ),
171
- TaskiqState : self .broker .state ,
172
- },
173
- )
174
- # Resolve all function's dependencies.
175
- dep_kwargs = await dep_ctx .resolve_kwargs ()
176
- for key , val in dep_kwargs .items ():
177
- if key not in message .kwargs :
178
- message .kwargs [key ] = val
179
- # Start a timer.
180
- start_time = time ()
181
- try :
182
- # If the function is a coroutine we await it.
183
- if asyncio .iscoroutinefunction (target ):
184
- returned = await target (* message .args , ** message .kwargs )
185
- else :
186
- # If this is a synchronous function we
187
- # run it in executor.
188
- returned = await loop .run_in_executor (
189
- self .executor ,
190
- _run_sync ,
191
- target ,
192
- message ,
193
- )
194
- except Exception as exc :
195
- found_exception = exc
196
- logger .error (
197
- "Exception found while executing function: %s" ,
198
- exc ,
199
- exc_info = True ,
159
+ dep_ctx = None
160
+ if dependency_graph :
161
+ # Create a context for dependency resolving.
162
+ dep_ctx = dependency_graph .async_ctx (
163
+ {
164
+ Context : Context (message , self .broker ),
165
+ TaskiqState : self .broker .state ,
166
+ },
167
+ )
168
+ # Resolve all function's dependencies.
169
+ dep_kwargs = await dep_ctx .resolve_kwargs ()
170
+ for key , val in dep_kwargs .items ():
171
+ if key not in message .kwargs :
172
+ message .kwargs [key ] = val
173
+ # Start a timer.
174
+ start_time = time ()
175
+ try :
176
+ # If the function is a coroutine we await it.
177
+ if asyncio .iscoroutinefunction (target ):
178
+ returned = await target (* message .args , ** message .kwargs )
179
+ else :
180
+ # If this is a synchronous function we
181
+ # run it in executor.
182
+ returned = await loop .run_in_executor (
183
+ self .executor ,
184
+ _run_sync ,
185
+ target ,
186
+ message ,
200
187
)
201
- # Stop the timer.
202
- execution_time = time () - start_time
203
- if dep_ctx :
204
- await dep_ctx .close ()
188
+ except Exception as exc :
189
+ found_exception = exc
190
+ logger .error (
191
+ "Exception found while executing function: %s" ,
192
+ exc ,
193
+ exc_info = True ,
194
+ )
195
+ # Stop the timer.
196
+ execution_time = time () - start_time
197
+ if dep_ctx :
198
+ await dep_ctx .close ()
205
199
206
- raw_logs = logs .getvalue ()
207
- logs .close ()
208
200
# Assemble result.
209
201
result : "TaskiqResult[Any]" = TaskiqResult (
210
202
is_err = found_exception is not None ,
211
- log = raw_logs ,
203
+ log = None ,
212
204
return_value = returned ,
213
205
execution_time = execution_time ,
214
206
)
0 commit comments