Bläddra i källkod

refactor: 闭包任务补充__job_name__

runphp 1 vecka sedan
förälder
incheckning
5f60fb361e
1 ändrade filer med 12 tillägg och 1 borttagningar
  1. 12 1
      src/Job/BaseJob.php

+ 12 - 1
src/Job/BaseJob.php

@@ -6,6 +6,7 @@ namespace SixShop\Core\Job;
 
 use Closure;
 use Exception;
+use ReflectionFunction;
 use think\facade\Log;
 use think\queue\Job;
 use Throwable;
@@ -27,7 +28,17 @@ abstract class BaseJob
     public static function dispatch(mixed $data = '', int $delay = 0, ?string $queue = null): JobDispatcher
     {
         if ($data instanceof Closure) {
-            $data = ['__job_closure__' => serialize($data)];
+            $ref = new ReflectionFunction($data);
+            $file = (string) $ref->getFileName();
+            // 转为相对于 backend/ 的路径
+            $baseDir = dirname(__DIR__, 5);
+            if (str_starts_with($file, $baseDir . '/')) {
+                $file = substr($file, strlen($baseDir) + 1);
+            }
+            $data = [
+                '__job_closure__' => serialize($data),
+                '__job_name__' => $file . ':' . $ref->getStartLine(),
+            ];
         }
         return new JobDispatcher(static::class, $data, $delay, $queue);
     }