66use App \User ;
77use Illuminate \Bus \Queueable ;
88use Illuminate \Contracts \Queue \ShouldQueue ;
9+ use Illuminate \Contracts \Redis \LimiterTimeoutException ;
910use Illuminate \Foundation \Bus \Dispatchable ;
1011use Illuminate \Queue \InteractsWithQueue ;
1112use Illuminate \Queue \SerializesModels ;
1213use Illuminate \Support \Arr ;
14+ use Illuminate \Support \Carbon ;
15+ use Illuminate \Support \Facades \Redis ;
1316use Illuminate \Support \Fluent ;
1417
1518class SendDeploymentNotificationJob implements ShouldQueue
@@ -38,35 +41,60 @@ public function __construct(User $user, array $deploymentInfo)
3841 $ this ->deploymentInfo = $ deploymentInfo ;
3942 }
4043
44+ /**
45+ * Get the tags that should be assigned to the job.
46+ *
47+ * @return array
48+ */
49+ public function tags (): array
50+ {
51+ return [
52+ 'deployment-notification ' ,
53+ 'user: ' .$ this ->user ->id ,
54+ ];
55+ }
56+
4157 /**
4258 * Execute the job.
4359 *
4460 * @return void
61+ *
62+ * @throws LimiterTimeoutException
4563 */
4664 public function handle (): void
4765 {
48- $ server = new Fluent (Arr::get ($ this ->deploymentInfo , 'server ' ));
49- $ site = new Fluent (Arr::get ($ this ->deploymentInfo , 'site ' ));
50- $ commit = new Fluent ([
51- 'hash ' => Arr::get ($ this ->deploymentInfo , 'commit_hash ' ),
52- 'url ' => Arr::get ($ this ->deploymentInfo , 'commit_url ' ),
53- 'author ' => Arr::get ($ this ->deploymentInfo , 'commit_author ' ),
54- 'message ' => Arr::get ($ this ->deploymentInfo , 'commit_message ' ),
55- ]);
66+ // From official documentation (https://core.telegram.org/bots/faq):
67+ // The API will not allow bulk notifications to more than ~30 users per second.
68+ // Also note that your bot will not be able to send more than 20 messages per minute to the same group.
69+ Redis::throttle ('telegram-api ' )
70+ ->allow (25 )
71+ ->every (1 )
72+ ->then (function () {
73+ $ server = new Fluent (Arr::get ($ this ->deploymentInfo , 'server ' ));
74+ $ site = new Fluent (Arr::get ($ this ->deploymentInfo , 'site ' ));
75+ $ commit = new Fluent ([
76+ 'hash ' => Arr::get ($ this ->deploymentInfo , 'commit_hash ' ),
77+ 'url ' => Arr::get ($ this ->deploymentInfo , 'commit_url ' ),
78+ 'author ' => Arr::get ($ this ->deploymentInfo , 'commit_author ' ),
79+ 'message ' => Arr::get ($ this ->deploymentInfo , 'commit_message ' ),
80+ ]);
5681
57- $ message = [
58- '*Deployment complete!* ' ,
59- "*Server:* [ {$ server ->name }](https://forge.laravel.com/servers/ {$ server ->id }) " ,
60- "*Site:* [ {$ site ->name }](https://forge.laravel.com/servers/ {$ server ->id }/sites/ {$ site ->id }) " ,
61- "*Status:* {$ this ->getStatus ()}" ,
62- "*Commit author:* {$ commit ->author }" ,
63- "*Commit hash:* [ $ commit ->hash ]( $ commit ->url ) " ,
64- "*Commit message:* {$ commit ->message }" ,
65- ];
82+ $ message = [
83+ '*Deployment complete!* ' ,
84+ "*Server:* [ {$ server ->name }](https://forge.laravel.com/servers/ {$ server ->id }) " ,
85+ "*Site:* [ {$ site ->name }](https://forge.laravel.com/servers/ {$ server ->id }/sites/ {$ site ->id }) " ,
86+ "*Status:* {$ this ->getStatus ()}" ,
87+ "*Commit author:* {$ commit ->author }" ,
88+ "*Commit hash:* [ $ commit ->hash ]( $ commit ->url ) " ,
89+ "*Commit message:* {$ commit ->message }" ,
90+ ];
6691
67- OutboundMessage::make ($ this ->user , implode ("\n" , $ message ))
68- ->parseMode (OutboundMessage::PARSE_MODE_MARKDOWN )
69- ->send ();
92+ OutboundMessage::make ($ this ->user , implode ("\n" , $ message ))
93+ ->parseMode (OutboundMessage::PARSE_MODE_MARKDOWN )
94+ ->send ();
95+ }, function () {
96+ $ this ->release (10 );
97+ });
7098 }
7199
72100 /**
@@ -89,4 +117,14 @@ protected function getStatus(): string
89117 return $ status ;
90118 }
91119 }
120+
121+ /**
122+ * Determine the time at which the job should timeout.
123+ *
124+ * @return Carbon
125+ */
126+ public function retryUntil (): Carbon
127+ {
128+ return now ()->addMinutes (10 );
129+ }
92130}
0 commit comments