fix: notifications
feat: add app stopped notification
This commit is contained in:
		
							parent
							
								
									98d057a2ac
								
							
						
					
					
						commit
						802ef03013
					
				| @ -2,6 +2,7 @@ | |||||||
| 
 | 
 | ||||||
| namespace App\Console; | namespace App\Console; | ||||||
| 
 | 
 | ||||||
|  | use App\Jobs\InstanceApplicationsStatusJob; | ||||||
| use App\Jobs\CheckResaleLicenseJob; | use App\Jobs\CheckResaleLicenseJob; | ||||||
| use App\Jobs\InstanceAutoUpdateJob; | use App\Jobs\InstanceAutoUpdateJob; | ||||||
| use App\Jobs\ProxyCheckJob; | use App\Jobs\ProxyCheckJob; | ||||||
| @ -16,12 +17,14 @@ class Kernel extends ConsoleKernel | |||||||
|     { |     { | ||||||
|         if (isDev()) { |         if (isDev()) { | ||||||
|             $schedule->command('horizon:snapshot')->everyMinute(); |             $schedule->command('horizon:snapshot')->everyMinute(); | ||||||
|  |             $schedule->job(new InstanceApplicationsStatusJob)->everyMinute(); | ||||||
|             $schedule->job(new ProxyCheckJob)->everyFiveMinutes(); |             $schedule->job(new ProxyCheckJob)->everyFiveMinutes(); | ||||||
|             // $schedule->job(new CheckResaleLicenseJob)->hourly();
 |             // $schedule->job(new CheckResaleLicenseJob)->hourly();
 | ||||||
|             // $schedule->job(new DockerCleanupJob)->everyOddHour();
 |             // $schedule->job(new DockerCleanupJob)->everyOddHour();
 | ||||||
|             // $schedule->job(new InstanceAutoUpdateJob(true))->everyMinute();
 |             // $schedule->job(new InstanceAutoUpdateJob(true))->everyMinute();
 | ||||||
|         } else { |         } else { | ||||||
|             $schedule->command('horizon:snapshot')->everyFiveMinutes(); |             $schedule->command('horizon:snapshot')->everyFiveMinutes(); | ||||||
|  |             $schedule->job(new InstanceApplicationsStatusJob)->everyMinute(); | ||||||
|             $schedule->job(new CheckResaleLicenseJob)->hourly(); |             $schedule->job(new CheckResaleLicenseJob)->hourly(); | ||||||
|             $schedule->job(new ProxyCheckJob)->everyFiveMinutes(); |             $schedule->job(new ProxyCheckJob)->everyFiveMinutes(); | ||||||
|             $schedule->job(new DockerCleanupJob)->everyTenMinutes(); |             $schedule->job(new DockerCleanupJob)->everyTenMinutes(); | ||||||
|  | |||||||
| @ -4,6 +4,7 @@ namespace App\Http\Livewire\Application; | |||||||
| 
 | 
 | ||||||
| use App\Jobs\ApplicationContainerStatusJob; | use App\Jobs\ApplicationContainerStatusJob; | ||||||
| use App\Models\Application; | use App\Models\Application; | ||||||
|  | use App\Notifications\Notifications\Application\ApplicationStoppedNotification; | ||||||
| use Livewire\Component; | use Livewire\Component; | ||||||
| use Visus\Cuid2\Cuid2; | use Visus\Cuid2\Cuid2; | ||||||
| 
 | 
 | ||||||
| @ -54,6 +55,7 @@ class Heading extends Component | |||||||
|         ); |         ); | ||||||
|         $this->application->status = 'stopped'; |         $this->application->status = 'stopped'; | ||||||
|         $this->application->save(); |         $this->application->save(); | ||||||
|  |         $this->application->environment->project->team->notify(new ApplicationStoppedNotification($this->application)); | ||||||
|     } |     } | ||||||
|     protected function setDeploymentUuid() |     protected function setDeploymentUuid() | ||||||
|     { |     { | ||||||
|  | |||||||
| @ -4,6 +4,7 @@ namespace App\Jobs; | |||||||
| 
 | 
 | ||||||
| use App\Models\Application; | use App\Models\Application; | ||||||
| use App\Models\ApplicationPreview; | use App\Models\ApplicationPreview; | ||||||
|  | use App\Notifications\Notifications\Application\ApplicationStoppedNotification; | ||||||
| use Illuminate\Bus\Queueable; | use Illuminate\Bus\Queueable; | ||||||
| use Illuminate\Contracts\Queue\ShouldBeUnique; | use Illuminate\Contracts\Queue\ShouldBeUnique; | ||||||
| use Illuminate\Contracts\Queue\ShouldQueue; | use Illuminate\Contracts\Queue\ShouldQueue; | ||||||
| @ -32,8 +33,14 @@ class ApplicationContainerStatusJob implements ShouldQueue, ShouldBeUnique | |||||||
|     } |     } | ||||||
|     public function handle(): void |     public function handle(): void | ||||||
|     { |     { | ||||||
|  |         ray('checking status of ' . $this->container_name); | ||||||
|         try { |         try { | ||||||
|             $status = get_container_status(server: $this->application->destination->server, container_id: $this->container_name, throwError: false); |             $status = get_container_status(server: $this->application->destination->server, container_id: $this->container_name, throwError: false); | ||||||
|  |             ray($this->application->status, $status); | ||||||
|  |             if ($this->application->status === 'running' && $status !== 'running') { | ||||||
|  |                 $this->application->environment->project->team->notify(new ApplicationStoppedNotification($this->application)); | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|             if ($this->pull_request_id) { |             if ($this->pull_request_id) { | ||||||
|                 $preview = ApplicationPreview::findPreviewByApplicationAndPullId($this->application->id, $this->pull_request_id); |                 $preview = ApplicationPreview::findPreviewByApplicationAndPullId($this->application->id, $this->pull_request_id); | ||||||
|                 $preview->status = $status; |                 $preview->status = $status; | ||||||
|  | |||||||
| @ -12,6 +12,8 @@ use App\Models\GitlabApp; | |||||||
| use App\Models\Server; | use App\Models\Server; | ||||||
| use App\Models\StandaloneDocker; | use App\Models\StandaloneDocker; | ||||||
| use App\Models\SwarmDocker; | use App\Models\SwarmDocker; | ||||||
|  | use App\Notifications\Notifications\Application\DeployedSuccessfullyNotification; | ||||||
|  | use App\Notifications\Notifications\Application\DeployedWithErrorNotification; | ||||||
| use App\Traits\ExecuteRemoteCommand; | use App\Traits\ExecuteRemoteCommand; | ||||||
| use Illuminate\Bus\Queueable; | use Illuminate\Bus\Queueable; | ||||||
| use Illuminate\Contracts\Queue\ShouldQueue; | use Illuminate\Contracts\Queue\ShouldQueue; | ||||||
| @ -221,6 +223,12 @@ class ApplicationDeploymentJob implements ShouldQueue | |||||||
|             ]); |             ]); | ||||||
|         } |         } | ||||||
|         queue_next_deployment($this->application); |         queue_next_deployment($this->application); | ||||||
|  |         if ($status === ApplicationDeploymentStatus::FINISHED->value) { | ||||||
|  |             $this->application->environment->project->team->notify(new DeployedSuccessfullyNotification($this->application, $this->deployment_uuid, $this->preview)); | ||||||
|  |         } | ||||||
|  |         if ($status === ApplicationDeploymentStatus::FAILED->value) { | ||||||
|  |             $this->application->environment->project->team->notify(new DeployedWithErrorNotification($this->application, $this->deployment_uuid, $this->preview)); | ||||||
|  |         } | ||||||
|     } |     } | ||||||
|     private function start_by_compose_file() |     private function start_by_compose_file() | ||||||
|     { |     { | ||||||
|  | |||||||
							
								
								
									
										35
									
								
								app/Jobs/InstanceApplicationsStatusJob.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								app/Jobs/InstanceApplicationsStatusJob.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,35 @@ | |||||||
|  | <?php | ||||||
|  | 
 | ||||||
|  | namespace App\Jobs; | ||||||
|  | 
 | ||||||
|  | use App\Models\Application; | ||||||
|  | use Illuminate\Bus\Queueable; | ||||||
|  | use Illuminate\Contracts\Queue\ShouldBeUnique; | ||||||
|  | use Illuminate\Contracts\Queue\ShouldQueue; | ||||||
|  | use Illuminate\Foundation\Bus\Dispatchable; | ||||||
|  | use Illuminate\Queue\InteractsWithQueue; | ||||||
|  | use Illuminate\Queue\SerializesModels; | ||||||
|  | 
 | ||||||
|  | class InstanceApplicationsStatusJob implements ShouldQueue, ShouldBeUnique | ||||||
|  | { | ||||||
|  |     use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||||||
|  | 
 | ||||||
|  |     public $applications; | ||||||
|  |     public function __construct() | ||||||
|  |     { | ||||||
|  |         $this->applications = Application::all(); | ||||||
|  |     } | ||||||
|  |     public function handle(): void | ||||||
|  |     { | ||||||
|  |         try { | ||||||
|  |             foreach ($this->applications as $application) { | ||||||
|  |                 dispatch(new ApplicationContainerStatusJob( | ||||||
|  |                     application: $application, | ||||||
|  |                     container_name: generate_container_name($application->uuid), | ||||||
|  |                 )); | ||||||
|  |             } | ||||||
|  |         } catch (\Exception $e) { | ||||||
|  |             ray($e->getMessage()); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,82 @@ | |||||||
|  | <?php | ||||||
|  | 
 | ||||||
|  | namespace App\Notifications\Notifications\Application; | ||||||
|  | 
 | ||||||
|  | use App\Models\Application; | ||||||
|  | use App\Models\ApplicationPreview; | ||||||
|  | use App\Notifications\Channels\EmailChannel; | ||||||
|  | use App\Notifications\Channels\DiscordChannel; | ||||||
|  | use Illuminate\Bus\Queueable; | ||||||
|  | use Illuminate\Contracts\Queue\ShouldQueue; | ||||||
|  | use Illuminate\Notifications\Messages\MailMessage; | ||||||
|  | use Illuminate\Notifications\Notification; | ||||||
|  | use Illuminate\Support\Str; | ||||||
|  | 
 | ||||||
|  | class ApplicationStoppedNotification extends Notification implements ShouldQueue | ||||||
|  | { | ||||||
|  |     use Queueable; | ||||||
|  |     public Application $application; | ||||||
|  | 
 | ||||||
|  |     public string $application_name; | ||||||
|  |     public string|null $application_url = null; | ||||||
|  |     public string $project_uuid; | ||||||
|  |     public string $environment_name; | ||||||
|  |     public string $fqdn; | ||||||
|  | 
 | ||||||
|  |     public function __construct(Application $application) | ||||||
|  |     { | ||||||
|  |         $this->application = $application; | ||||||
|  |         $this->application_name = data_get($application, 'name'); | ||||||
|  |         $this->project_uuid = data_get($application, 'environment.project.uuid'); | ||||||
|  |         $this->environment_name = data_get($application, 'environment.name'); | ||||||
|  |         $this->fqdn = data_get($application, 'fqdn'); | ||||||
|  |         if (Str::of($this->fqdn)->explode(',')->count() > 1) { | ||||||
|  |             $this->fqdn = Str::of($this->fqdn)->explode(',')->first(); | ||||||
|  |         } | ||||||
|  |         $this->application_url =  base_url() . "/project/{$this->project_uuid}/{$this->environment_name}/application/{$this->application->uuid}"; | ||||||
|  |     } | ||||||
|  |     public function via(object $notifiable): array | ||||||
|  |     { | ||||||
|  |         $channels = []; | ||||||
|  |         $isEmailEnabled = data_get($notifiable, 'smtp.enabled'); | ||||||
|  |         $isDiscordEnabled = data_get($notifiable, 'discord.enabled'); | ||||||
|  |         $isSubscribedToEmailDeployments = data_get($notifiable, 'smtp_notifications.deployments'); | ||||||
|  |         $isSubscribedToDiscordDeployments = data_get($notifiable, 'discord_notifications.deployments'); | ||||||
|  | 
 | ||||||
|  |         if ($isEmailEnabled && $isSubscribedToEmailDeployments) { | ||||||
|  |             $channels[] = EmailChannel::class; | ||||||
|  |         } | ||||||
|  |         if ($isDiscordEnabled && $isSubscribedToDiscordDeployments) { | ||||||
|  |             $channels[] = DiscordChannel::class; | ||||||
|  |         } | ||||||
|  |         return $channels; | ||||||
|  |     } | ||||||
|  |     public function toMail(): MailMessage | ||||||
|  |     { | ||||||
|  |         // $mail = new MailMessage();
 | ||||||
|  |         // $pull_request_id = data_get($this->preview, 'pull_request_id', 0);
 | ||||||
|  |         // $fqdn = $this->fqdn;
 | ||||||
|  |         // if ($pull_request_id === 0) {
 | ||||||
|  |         //     $mail->subject("✅New version is deployed of {$this->application_name}");
 | ||||||
|  |         // } else {
 | ||||||
|  |         //     $fqdn = $this->preview->fqdn;
 | ||||||
|  |         //     $mail->subject("✅ Pull request #{$pull_request_id} of {$this->application_name} deployed successfully");
 | ||||||
|  |         // }
 | ||||||
|  |         // $mail->view('emails.application-deployed-successfully', [
 | ||||||
|  |         //     'name' => $this->application_name,
 | ||||||
|  |         //     'fqdn' => $fqdn,
 | ||||||
|  |         //     'deployment_url' => $this->deployment_url,
 | ||||||
|  |         //     'pull_request_id' => $pull_request_id,
 | ||||||
|  |         // ]);
 | ||||||
|  |         // return $mail;
 | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public function toDiscord(): string | ||||||
|  |     { | ||||||
|  |         $message = '⛔ ' . $this->application_name . ' has been stopped. | ||||||
|  |              | ||||||
|  | '; | ||||||
|  |         $message .= '[Application URL](' . $this->application_url . ')'; | ||||||
|  |         return $message; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -62,7 +62,7 @@ class DeployedSuccessfullyNotification extends Notification implements ShouldQue | |||||||
|         $pull_request_id = data_get($this->preview, 'pull_request_id', 0); |         $pull_request_id = data_get($this->preview, 'pull_request_id', 0); | ||||||
|         $fqdn = $this->fqdn; |         $fqdn = $this->fqdn; | ||||||
|         if ($pull_request_id === 0) { |         if ($pull_request_id === 0) { | ||||||
|             $mail->subject("✅ New version is deployed of {$this->application_name}"); |             $mail->subject("✅New version is deployed of {$this->application_name}"); | ||||||
|         } else { |         } else { | ||||||
|             $fqdn = $this->preview->fqdn; |             $fqdn = $this->preview->fqdn; | ||||||
|             $mail->subject("✅ Pull request #{$pull_request_id} of {$this->application_name} deployed successfully"); |             $mail->subject("✅ Pull request #{$pull_request_id} of {$this->application_name} deployed successfully"); | ||||||
| @ -79,15 +79,19 @@ class DeployedSuccessfullyNotification extends Notification implements ShouldQue | |||||||
|     public function toDiscord(): string |     public function toDiscord(): string | ||||||
|     { |     { | ||||||
|         if ($this->preview) { |         if ($this->preview) { | ||||||
|             $message = '✅ Pull request #' . $this->preview->pull_request_id . ' of **' . $this->application_name . '** deployed successfully: '; |             $message = '✅ New PR' . $this->preview->pull_request_id . ' version successfully deployed of ' . $this->application_name . '  | ||||||
|  |              | ||||||
|  | '; | ||||||
|             if ($this->preview->fqdn) { |             if ($this->preview->fqdn) { | ||||||
|                 $message .= '[Application Link](' . $this->preview->fqdn . ') |'; |                 $message .= '[Open Application](' . $this->preview->fqdn . ') | '; | ||||||
|             } |             } | ||||||
|             $message .= '[Deployment logs](' . $this->deployment_url . ')'; |             $message .= '[Deployment logs](' . $this->deployment_url . ')'; | ||||||
|         } else { |         } else { | ||||||
|             $message = '✅ A new version has been deployed of **' . $this->application_name . '**: '; |             $message = '✅ New version successfully deployed of ' . $this->application_name . '  | ||||||
|  |              | ||||||
|  | '; | ||||||
|             if ($this->fqdn) { |             if ($this->fqdn) { | ||||||
|                 $message .= '[Application Link](' . $this->fqdn . ') |'; |                 $message .= '[Open Application](' . $this->fqdn . ') | '; | ||||||
|             } |             } | ||||||
|             $message .= '[Deployment logs](' . $this->deployment_url . ')'; |             $message .= '[Deployment logs](' . $this->deployment_url . ')'; | ||||||
|         } |         } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user