File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace G4 \Tasker \Tasker2 ;
4+
5+ class RetryAfterResolver
6+ {
7+ const RETRY_AFTER_60 = 60 ;
8+ const RETRY_AFTER_300 = 300 ;
9+ const RETRY_AFTER_1000 = 1000 ;
10+
11+ /**
12+ * @var int
13+ */
14+ private $ startedCount ;
15+
16+ /**
17+ * RetryAfterResolver constructor.
18+ * @param int $startedCount
19+ */
20+ public function __construct ($ startedCount )
21+ {
22+ $ this ->startedCount = (int ) $ startedCount ;
23+ }
24+
25+ /**
26+ * Return after how much seconds task should be retried based on number of starting
27+ *
28+ * @return int
29+ */
30+ public function resolve ()
31+ {
32+ if ($ this ->startedCount === 2 ) {
33+ return self ::RETRY_AFTER_300 ;
34+ }
35+
36+ if ($ this ->startedCount === 3 ) {
37+ return self ::RETRY_AFTER_1000 ;
38+ }
39+
40+ return self ::RETRY_AFTER_60 ;
41+ }
42+ }
Original file line number Diff line number Diff line change @@ -77,8 +77,7 @@ public function execute()
7777 $ this ->taskDomain
7878 ->setStatusWorking ()
7979 ->setIdentifier (gethostname ())
80- ->setTsStarted (time ())
81- ->setStartedCount ($ this ->taskDomain ->getStartedCount () + 1 );
80+ ->setTsStarted (time ());
8281
8382 $ this ->logTaskStart ();
8483 $ this ->logNewRelicStart ();
@@ -92,6 +91,9 @@ public function execute()
9291
9392 try {
9493 $ this ->checkMaxRetryAttempts ();
94+
95+ $ this ->taskDomain ->setStartedCount ($ this ->taskDomain ->getStartedCount () + 1 );
96+
9597 $ task ->execute ();
9698 $ this ->logNewRelicEnd ();
9799 } catch (\Exception $ e ) {
@@ -247,7 +249,7 @@ private function requeueTask()
247249 $ this ->taskDomain
248250 ->setTsStarted (0 )
249251 ->setExecTime (-1 )
250- ->setTsCreated (time () + self :: RETRY_AFTER )
252+ ->setTsCreated (time () + ( new RetryAfterResolver ( $ this -> taskDomain -> getStartedCount ()))-> resolve () )
251253 ->setTaskId (null )
252254 ->setStatus (\G4 \Tasker \Consts::STATUS_PENDING );
253255 $ this ->taskRepository ->add ($ this ->taskDomain );
Original file line number Diff line number Diff line change 1+ <?php
2+
3+
4+ class RetryAfterResolverTest extends \PHPUnit_Framework_TestCase
5+ {
6+ /**
7+ * @dataProvider resolverDataProvider
8+ */
9+ public function testResolve ($ startedCount , $ retryAfter )
10+ {
11+ $ resolver = new \G4 \Tasker \Tasker2 \RetryAfterResolver ($ startedCount );
12+
13+ $ this ->assertSame ($ retryAfter , $ resolver ->resolve ());
14+ }
15+
16+ public function resolverDataProvider ()
17+ {
18+ return [
19+ [1 , 60 ],
20+ [2 , 300 ],
21+ [3 , 1000 ]
22+ ];
23+ }
24+ }
You can’t perform that action at this time.
0 commit comments