1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* AnimeDb package. |
4
|
|
|
* |
5
|
|
|
* @author Peter Gribanov <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
7
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 GPL v3 |
8
|
|
|
*/ |
9
|
|
|
namespace AnimeDb\Bundle\AppBundle\Repository; |
10
|
|
|
|
11
|
|
|
use Doctrine\ORM\EntityRepository; |
12
|
|
|
use AnimeDb\Bundle\AppBundle\Entity\Task as TaskEntity; |
13
|
|
|
|
14
|
|
|
class Task extends EntityRepository |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Maximum standby time. |
18
|
|
|
* |
19
|
|
|
* Between scans tasks can interpose new task. |
20
|
|
|
* If no limit standby scheduler can not handle the new task before |
21
|
|
|
* the arrival of the start time of the next task. |
22
|
|
|
* For example the scheduler can expect a few days before the execution of the tasks |
23
|
|
|
* that must be performed every hour. |
24
|
|
|
* |
25
|
|
|
* @var int |
26
|
|
|
*/ |
27
|
|
|
const MAX_STANDBY_TIME = 3600; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return TaskEntity|null |
31
|
|
|
*/ |
32
|
|
|
public function getNextTask() |
33
|
|
|
{ |
34
|
|
|
return $this->getEntityManager()->createQuery(' |
35
|
|
|
SELECT |
36
|
|
|
t |
37
|
|
|
FROM |
38
|
|
|
AnimeDbAppBundle:Task t |
39
|
|
|
WHERE |
40
|
|
|
t.status = :status AND t.next_run <= :time |
41
|
|
|
ORDER BY |
42
|
|
|
t.next_run ASC |
43
|
|
|
') |
44
|
|
|
->setMaxResults(1) |
45
|
|
|
->setParameter('status', TaskEntity::STATUS_ENABLED) |
46
|
|
|
->setParameter('time', date('Y-m-d H:i:s')) |
47
|
|
|
->getOneOrNullResult(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return int |
52
|
|
|
*/ |
53
|
|
|
public function getWaitingTime() |
54
|
|
|
{ |
55
|
|
|
// get next task |
56
|
|
|
$task = $this->getEntityManager()->createQuery(' |
57
|
|
|
SELECT |
58
|
|
|
t |
59
|
|
|
FROM |
60
|
|
|
AnimeDbAppBundle:Task t |
61
|
|
|
WHERE |
62
|
|
|
t.status = :status |
63
|
|
|
ORDER BY |
64
|
|
|
t.next_run ASC |
65
|
|
|
') |
66
|
|
|
->setMaxResults(1) |
67
|
|
|
->setParameter('status', TaskEntity::STATUS_ENABLED) |
68
|
|
|
->getOneOrNullResult(); |
69
|
|
|
|
70
|
|
|
// task is exists |
71
|
|
|
if ($task instanceof TaskEntity) { |
72
|
|
|
$task_time = $task->getNextRun()->getTimestamp() - time(); |
73
|
|
|
if ($task_time > 0) { |
74
|
|
|
return min($task_time, self::MAX_STANDBY_TIME); |
75
|
|
|
} else { |
76
|
|
|
return 0; // need run now |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return self::MAX_STANDBY_TIME; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|