-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTaskLogModel.php
More file actions
132 lines (112 loc) · 2.57 KB
/
TaskLogModel.php
File metadata and controls
132 lines (112 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php declare(strict_types=1);
namespace Torr\TaskManager\Model;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Psr\Log\LoggerInterface;
use Torr\TaskManager\Entity\TaskLog;
use Torr\TaskManager\Entity\TaskRun;
use Torr\TaskManager\Task\DispatchAfterRunTask\DispatchAfterRunTask;
use Torr\TaskManager\Task\Task;
final class TaskLogModel
{
public const bool SHOW_ALL_TASKS = true;
public const bool HIDE_INTERNAL_TASKS = false;
/** @var EntityRepository<TaskLog> */
private EntityRepository $repository;
/**
*/
public function __construct (
private readonly EntityManagerInterface $entityManager,
private readonly LoggerInterface $logger,
)
{
$this->repository = $this->entityManager->getRepository(TaskLog::class);
}
/**
*
*/
public function findById (int $id) : ?TaskLog
{
return $this->repository->find($id);
}
/**
* Gets or creates the log entry for the given task
*/
public function getLogForTask (Task $task) : TaskLog
{
$log = $this->repository->findOneBy([
"taskId" => $task->ulid,
]);
if (null !== $log)
{
return $log;
}
// if it isn't created yet, create a new one
$log = new TaskLog($task);
$this->entityManager->persist($log);
return $log;
}
/**
*
*/
public function getTaskCount () : int
{
return $this->repository->count();
}
/**
* Returns the latest task log entries
*
* @return TaskLog[]
*/
public function getMostRecentEntries (
int $limit = 100,
bool $showAll = self::SHOW_ALL_TASKS,
) : array
{
$builder = $this->repository->createQueryBuilder("task")
->select("task, run")
->leftJoin("task.runs", "run")
->addOrderBy("task.timeQueued", "DESC")
->setMaxResults($limit);
if (self::HIDE_INTERNAL_TASKS === $showAll)
{
$builder
->andWhere("task.taskClass NOT IN (:internalTasks)")
->setParameter("internalTasks", [
DispatchAfterRunTask::class,
]);
}
/** @var TaskLog[] */
return (new Paginator($builder->getQuery()))
->getQuery()
->getResult();
}
/**
* Creates a new run for the given task (lok) and marks it as persisted.
*/
public function createRunForTask (TaskLog $log) : TaskRun
{
$run = $log->createRun($this->logger);
$this->entityManager->persist($run);
return $run;
}
/**
* @return $this
*/
public function flush () : static
{
$this->entityManager->flush();
return $this;
}
/**
* Marks the log entry for removal
*
* @return $this
*/
public function remove (TaskLog $log) : static
{
$this->entityManager->remove($log);
return $this;
}
}