1+ <?php
2+
3+ namespace Stackkit \LaravelGooglePubSubQueue ;
4+
5+ use Google \Cloud \PubSub \Message ;
6+ use Illuminate \Container \Container ;
7+ use Illuminate \Contracts \Queue \Job as JobContract ;
8+ use Illuminate \Queue \Jobs \Job ;
9+ use Stackkit \LaravelGooglePubSubQueue \PubSubQueue ;
10+
11+ class PubSubJob extends Job implements JobContract
12+ {
13+ /**
14+ * The PubSub queue.
15+ *
16+ * @var \Stackkit\LaravelGooglePubSubQueue\PubSubQueue
17+ */
18+ protected $ pubsub ;
19+
20+ /**
21+ * The job instance.
22+ *
23+ * @var array
24+ */
25+ protected $ job ;
26+
27+ /**
28+ * Create a new job instance.
29+ *
30+ * @param \Illuminate\Container\Container $container
31+ * @param \Stackkit\LaravelGooglePubSubQueue\PubSubQueue $sqs
32+ * @param \Google\Cloud\PubSub\Message $job
33+ * @param string $connectionName
34+ * @param string $queue
35+ */
36+ public function __construct (Container $ container , PubSubQueue $ pubsub , Message $ job , $ connectionName , $ queue )
37+ {
38+ $ this ->pubsub = $ pubsub ;
39+ $ this ->job = $ job ;
40+ $ this ->queue = $ queue ;
41+ $ this ->container = $ container ;
42+ $ this ->connectionName = $ connectionName ;
43+
44+ $ this ->decoded = $ this ->payload ();
45+ }
46+
47+ /**
48+ * Get the job identifier.
49+ *
50+ * @return string
51+ */
52+ public function getJobId ()
53+ {
54+ return $ this ->decoded ['id ' ] ?? null ;
55+ }
56+
57+ /**
58+ * Get the raw body of the job.
59+ *
60+ * @return string
61+ */
62+ public function getRawBody ()
63+ {
64+ return base64_decode ($ this ->job ->data ());
65+ }
66+
67+ /**
68+ * Get the number of times the job has been attempted.
69+ *
70+ * @return int
71+ */
72+ public function attempts ()
73+ {
74+ return ((int ) $ this ->job ->attribute ('attempts ' ) ?? 0 ) + 1 ;
75+ }
76+
77+ /**
78+ * Release the job back into the queue.
79+ *
80+ * @param int $delay
81+ * @return void
82+ */
83+ public function release ($ delay = 0 )
84+ {
85+ parent ::release ($ delay );
86+
87+ $ attempts = $ this ->attempts ();
88+ $ this ->pubsub ->republish (
89+ $ this ->job ,
90+ $ this ->queue ,
91+ ['attempts ' => (string ) $ attempts ],
92+ $ delay
93+ );
94+ }
95+ }
0 commit comments