11import asyncio
22import inspect
3- import io
43from concurrent .futures import ThreadPoolExecutor
54from logging import getLogger
65from time import time
1110from taskiq .abc .broker import AsyncBroker
1211from taskiq .abc .middleware import TaskiqMiddleware
1312from taskiq .cli .worker .args import WorkerArgs
14- from taskiq .cli .worker .log_collector import log_collector
1513from taskiq .cli .worker .params_parser import parse_params
1614from taskiq .context import Context
1715from taskiq .message import BrokerMessage , TaskiqMessage
@@ -150,8 +148,6 @@ async def run_task( # noqa: C901, WPS210
150148 :return: result of execution.
151149 """
152150 loop = asyncio .get_running_loop ()
153- # Buffer to capture logs.
154- logs = io .StringIO ()
155151 returned = None
156152 found_exception = None
157153 signature = self .task_signatures .get (message .task_name )
@@ -160,55 +156,51 @@ async def run_task( # noqa: C901, WPS210
160156 signature = None
161157 parse_params (signature , self .task_hints .get (message .task_name ) or {}, message )
162158
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 ,
200187 )
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 ()
205199
206- raw_logs = logs .getvalue ()
207- logs .close ()
208200 # Assemble result.
209201 result : "TaskiqResult[Any]" = TaskiqResult (
210202 is_err = found_exception is not None ,
211- log = raw_logs ,
203+ log = None ,
212204 return_value = returned ,
213205 execution_time = execution_time ,
214206 )
0 commit comments