/** * Execute the job. * * @throws \Exception */ public function handle() { /** @var Task $task */ $task = Task::find($this->task_id); if (is_null($task)) { throw new Exception('TaskJob not found task with id', ['task_id' => $this->task_id]); } $task->start_running(); if (!class_exists($task->job_class)) { $task->failure('Class ' . strval($task->job_class) . ' not exists'); $this->delete(); return null; } $className = $task->job_class; /** @var Task $task */ $object = $task->taskable()->first(); /** @var BaseTask $job */ $job = new $className($object); try { list($rv, $errormessage) = $job->run(); if ($rv === true) { $job->success(); $task->success(); } else { $job->failure(new Exception('Job ' . $className . ' return value false and message: ' . $errormessage)); $task->failure('Job ' . $className . ' return value false and error message:' . $errormessage); } } catch (Exception $e) { $job->failure($e); $task->failure($e); } $this->delete(); }
public function createTaskWithJobClass($job_class) { $model_example = factory(\App\ModelExample::class)->create(); $this->assertNotNull($model_example); $task = \TPTaskRunner\Models\Task::createTask($job_class); $model_example->tasks()->save($task); return [$task, $model_example]; }
public function test_cron_tasks() { /** @var \TPTaskRunner\Models\Task $task_1 */ $task_1 = factory(\TPTaskRunner\Models\Task::class)->create(); $task_1->next_run_at = \Carbon\Carbon::now()->addDay(1); $task_1->save(); /** @var \TPTaskRunner\Models\Task $task_2 */ $task_2 = factory(\TPTaskRunner\Models\Task::class)->create(); $query_1_count = \TPTaskRunner\Models\Task::cron()->count(); $query_2_count = \TPTaskRunner\Models\Task::cron(\Carbon\Carbon::now()->addDay(2))->count(); $this->assertNotEquals($query_1_count, $query_2_count); $this->assertTrue($query_1_count < $query_2_count); }
/** * Erzeugt einen Job für den Task * @param $id * @return \Illuminate\Http\JsonResponse */ public function run($id) { $task = Task::findOrFail($id); $force_value = Input::get('force', false); $rv = $task->run($force = $force_value); return response()->json(['run' => $rv]); }
/** * Konstruktor create task with data * @param $job_class * @param $array * @return Task */ public static function createTaskWithData($job_class, $array) { $task = Task::createTask($job_class); $task->setJSONData($array); return $task; }
/** * Run all Tasks thats ready for that * @return array list of id`s with runned tasks */ public static function cronJob() { $tasks = Task::cron(); $started_tasks = []; /** @var Task $task */ foreach ($tasks as $task) { array_push($started_tasks, [$task->id]); $task->run(); } return $started_tasks; }