<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: flovntp</title>
    <description>The latest articles on Forem by flovntp (@flovntp).</description>
    <link>https://forem.com/flovntp</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1126217%2F3b009658-4f64-4edb-924f-3f984c6e0c5c.jpeg</url>
      <title>Forem: flovntp</title>
      <link>https://forem.com/flovntp</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/flovntp"/>
    <language>en</language>
    <item>
      <title>Seamless user experience during Upsun deployment</title>
      <dc:creator>flovntp</dc:creator>
      <pubDate>Wed, 31 Jul 2024 13:38:24 +0000</pubDate>
      <link>https://forem.com/upsun/how-to-get-notifications-when-an-upsun-infrastructure-event-occurs-2257</link>
      <guid>https://forem.com/upsun/how-to-get-notifications-when-an-upsun-infrastructure-event-occurs-2257</guid>
      <description>&lt;p&gt;As speed is critical to success, it’s essential to deploy as often as needed—even on Fridays. But what exactly happens during a deployment? In particular, what happens during deployment with the &lt;a href="https://upsun.com/" rel="noopener noreferrer"&gt;Upsun&lt;/a&gt; PaaS? A clearer picture of the deployment process, and more control over it, can significantly improve the developer experience—and allow them to move through it faster.&lt;/p&gt;

&lt;p&gt;During a deployment—when a new commit is pushed—&lt;a href="https://upsun.com/" rel="noopener noreferrer"&gt;Upsun&lt;/a&gt; triggers a new build of your application. Once the build phase is done, the container is deployed and the &lt;code&gt;deploy&lt;/code&gt; script is executed. Any pending requests are withheld during that phase and replayed when the application is available. However, downtime inevitably creeps in any migration or any other action that takes too long, causing significant frustration among editors and customers.&lt;/p&gt;

&lt;p&gt;Fortunately, the &lt;a href="https://docs.upsun.com/define-routes/cache.html" rel="noopener noreferrer"&gt;Upsun cache mechanism&lt;/a&gt; still serves read requests, facilitating zero downtime for your front end. However, write requests will still result in the dreaded 5xx errors if the deployment exceeds the 30 seconds HTTP timeout.&lt;/p&gt;

&lt;p&gt;These are the key questions: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How to prevent/pause your final user executing write requests during this deployment timeframe and avoid them being frustrated with HTTP 5xx errors?&lt;/li&gt;
&lt;li&gt;On the application side, how to get automatic notifications when a deployment is in progress? &lt;/li&gt;
&lt;li&gt;Who needs to listen to these deploy events? &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://upsun.com/" rel="noopener noreferrer"&gt;Upsun&lt;/a&gt; provides infrastructure events that can be automatically sent to an external application — referred to as a &lt;em&gt;&lt;code&gt;status-holder&lt;/code&gt; application&lt;/em&gt; — using a webhook integration. This feature is gold but is hugely under-used and it’s time to change that. &lt;/p&gt;

&lt;p&gt;Upsun’s versatile webhook integration empowers a &lt;code&gt;status-holder&lt;/code&gt; application to monitor the status of your applications seamlessly. For all of you multi-application managers, you may have encountered this problem: a dedicated application for managing your &lt;code&gt;main&lt;/code&gt; application’s status flag faltering in a multi-application architecture where each application undergoes redeployment. &lt;/p&gt;

&lt;p&gt;This article explores a workaround that allows your application to listen to deployment signals and prevent write requests from occurring at the wrong time—let’s dive in!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;🚨 &lt;em&gt;Please note&lt;/em&gt;: This article is not meant to explain, as a dev, how to get notified and dig into your deployment failure logs nor how to get notifications if Upsun is performing routine maintenance on a region. &lt;br&gt;
For such cases, please refer to the &lt;a href="https://docs.upsun.com/development/troubleshoot.html#check-for-errors-in-the-logs" rel="noopener noreferrer"&gt;corresponding doc page&lt;/a&gt; or please subscribe to &lt;a href="https://status.upsun.com" rel="noopener noreferrer"&gt;https://status.upsun.com&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;The most effective infrastructure event workflow&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;A &lt;a href="https://docs.upsun.com/integrations/activity.html" rel="noopener noreferrer"&gt;webhook integration&lt;/a&gt; is a way to create an infrastructure event listener (kind of) that will send requested infrastructure events to an endpoint of &lt;a href="https://docs.upsun.com/administration/cli/reference.html#integrationadd" rel="noopener noreferrer"&gt;your choice&lt;/a&gt; (parameter &lt;code&gt;--type&lt;/code&gt;; URL, script, slack channel, etc.). &lt;/p&gt;

&lt;p&gt;By using a webhook integration, &lt;a href="https://upsun.com/" rel="noopener noreferrer"&gt;Upsun&lt;/a&gt; can send infrastructure events that occur along the way on your &lt;code&gt;main&lt;/code&gt; application directly to the &lt;code&gt;status-holder&lt;/code&gt; application.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;status-holder&lt;/code&gt; application, responsible for storing the status of your &lt;code&gt;main&lt;/code&gt; application, cannot be on the same infrastructure (a.k.a. &lt;a href="https://docs.upsun.com/create-apps/multi-app.html" rel="noopener noreferrer"&gt;multiple applications&lt;/a&gt; in the same project). This is because the deploy process will redeploy each of the applications within your environment, so, the &lt;code&gt;status-holder&lt;/code&gt; application won’t be available, just like your &lt;code&gt;main&lt;/code&gt; application during any deployment. That’s why this &lt;code&gt;status-holder&lt;/code&gt; application needs to be hosted outside of your &lt;code&gt;main&lt;/code&gt; project—on another Upsun project, perhaps.&lt;/p&gt;

&lt;p&gt;On the frontend of your &lt;code&gt;main&lt;/code&gt; application, as HTML interfaces are already loaded in the browser, you can use a Javascript banner (a.k.a. a &lt;code&gt;Website Banner&lt;/code&gt; on the workflow diagram) that will be able to request the actual infrastructure status of your &lt;code&gt;main&lt;/code&gt; application every so many seconds.&lt;/p&gt;

&lt;p&gt;As soon as the main status changes, the &lt;code&gt;status-holder&lt;/code&gt; application will receive the event and store the &lt;code&gt;main&lt;/code&gt; application status. On the next count, the website banner will receive the new status and display a message to your visitors, asking them to hold on for a few, until your application is back. When deployment is complete, the website banner will automatically disappear. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx2f3g3t9sat6yg6nwdfq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx2f3g3t9sat6yg6nwdfq.png" alt="Workflow diagram" width="775" height="425"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;🚨 &lt;em&gt;Please note&lt;/em&gt;: of course, displaying a website banner is an effective way to utilize this helpful infrastructure events feature but we always encourage our customers to devise their own mitigation strategy tailored to their needs.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;How to set up an infrastructure events workflow&lt;/em&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;Create a &lt;code&gt;status-holder&lt;/code&gt; project&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;This &lt;a href="https://github.com/gmoigneu/upsun-project-status" rel="noopener noreferrer"&gt;upsun-status-project GitHub repository&lt;/a&gt; contains a basic &lt;a href="https://laravel.com/" rel="noopener noreferrer"&gt;Laravel&lt;/a&gt; application designed to receive infrastructure notifications, cache them (using Redis), and has an &lt;code&gt;/api&lt;/code&gt; root to request specific environment status (format &lt;code&gt;/api/&amp;lt;projectId&amp;gt;/&amp;lt;environment&amp;gt;&lt;/code&gt;).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;🚨 &lt;em&gt;Please note&lt;/em&gt;: for this tutorial, we use a Laravel application for the &lt;code&gt;status-holder&lt;/code&gt; app but feel free to reuse the business logic and come up with a solution using any other framework you like.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The source code in this GitHub repository is already Upsun-ready, so you can fork it in your own GitHub organization and start using it as is. Then, you just need to &lt;a href="https://docs.upsun.com/get-started/here/create-project.html" rel="noopener noreferrer"&gt;create an empty Upsun project&lt;/a&gt; and &lt;a href="https://docs.upsun.com/integrations/source/github.html" rel="noopener noreferrer"&gt;add a source integration&lt;/a&gt; to sync your Upsun project with your new GitHub repository. Use the 4 following command lines from the root of your &lt;code&gt;status-holder&lt;/code&gt; source code to do so:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

git clone git@github.com:&amp;lt;OWNER/REPOSITORY&amp;gt;
cd &amp;lt;REPOSITORY&amp;gt;
upsun project:create
upsun integration:add --project &amp;lt;PROJECT_ID&amp;gt; --type github --repository &amp;lt;OWNER/REPOSITORY&amp;gt; --token &amp;lt;GITHUB_ACCESS_TOKEN&amp;gt; 


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;PROJECT_ID&amp;gt;&lt;/code&gt; is the ID of your new &lt;code&gt;status-holder&lt;/code&gt; Upsun project that you get after executing the &lt;code&gt;upsun project:create&lt;/code&gt; command line.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;OWNER/REPOSITORY&amp;gt;&lt;/code&gt; is your forked repository in GitHub.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;GITHUB_ACCESS_TOKEN&amp;gt;&lt;/code&gt; is your &lt;a href="https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens" rel="noopener noreferrer"&gt;generated GitHub API Token&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For more information, please refer to the &lt;a href="https://docs.upsun.com/integrations/source/github.html#2-enable-the-integration" rel="noopener noreferrer"&gt;enable integration documentation page&lt;/a&gt;. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;🚨 &lt;em&gt;Please note&lt;/em&gt;: as soon as your &lt;code&gt;status-holder&lt;/code&gt; project is deployed, pull the corresponding environment URL aside, &lt;code&gt;&amp;lt;STATUS_HOLDER_URL&amp;gt;&lt;/code&gt;, for later steps. To get this corresponding URL outside of the deployment logs, use the following command from the root of your &lt;code&gt;status-holder&lt;/code&gt; project: &lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

upsun environment:url --primary --pipe


&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;How to integrate the &lt;code&gt;status-holder&lt;/code&gt; project with your &lt;code&gt;main&lt;/code&gt; project&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;These steps will explain how to connect your &lt;code&gt;main&lt;/code&gt; project infrastructure events with the &lt;code&gt;status-holder&lt;/code&gt; project, and then display the website banner when a deployment is triggered. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;🚨 &lt;em&gt;Please note&lt;/em&gt;: if you don’t have an existing Upsun &lt;code&gt;main&lt;/code&gt; project, take a look at this &lt;a href="https://upsun.com/blog/upsun-and-running-with-symfony-demo/" rel="noopener noreferrer"&gt;simple tutorial&lt;/a&gt; on how to host a Symfony Demo application on Upsun and then use this Symfony application for testing.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;1. Add a webhook integration&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;Your &lt;code&gt;main&lt;/code&gt; application needs to send any infrastructure events that are triggered to the &lt;code&gt;status-holder&lt;/code&gt; project. To do so, please execute the following CLI command, from the root of your &lt;code&gt;main&lt;/code&gt; project, to create a webhook integration:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

upsun integration:add --type=webhook --url=&amp;lt;STATUS_HOLDER_URL&amp;gt;/api/ --states=in_progress,complete --environments main --events \*


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;🚨 &lt;em&gt;Please note&lt;/em&gt;: &lt;code&gt;&amp;lt;STATUS_HOLDER_URL&amp;gt;&lt;/code&gt; is the URL of your &lt;code&gt;status-holder&lt;/code&gt; project, created during the create a &lt;code&gt;status-holder&lt;/code&gt; project step above. You can also use &lt;code&gt;&amp;lt;STATUS_HOLDER_URL&amp;gt;&lt;/code&gt; and &lt;code&gt;/api/&lt;/code&gt; as a suffix to use the &lt;code&gt;status-holder&lt;/code&gt; API route that will store the current status of your &lt;code&gt;main&lt;/code&gt; application.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;2. Integrate the built-in JavaScript banner&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;Good news, we already designed a &lt;a href="https://github.com/upsun/infrastucture-notif" rel="noopener noreferrer"&gt;JavaScript banner script&lt;/a&gt; using &lt;a href="https://dev.tourl"&gt;Notiflix&lt;/a&gt; so you don’t have to. This &lt;code&gt;website-banner.js&lt;/code&gt; script displays a JavaScript banner to your visitors as soon as something is happening on the infrastructure side and informs them to hold on for a moment until the application is back up and running. &lt;/p&gt;

&lt;p&gt;You need to display this banner where critical actions may happen on your website, like on the edit interface (admin) of an article or on your basket interface (ecommerce websites) where customers may approve their baskets (=writing into the database) or on the registration page.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;🚨 &lt;em&gt;Please note&lt;/em&gt;: This script is not needed on static pages as &lt;a href="https://upsun.com/" rel="noopener noreferrer"&gt;Upsun&lt;/a&gt; will continue serving cached pages during the process.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can download this JavaScript banner file for your Symfony project, using the following: &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

curl -L https://raw.githubusercontent.com/upsun/snippets/main/src/website-banner.js &amp;gt; assets/js/website-banner.js
git add assets/js/website-banner.js &amp;amp;&amp;amp; git commit -m "Add website-banner.js"


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;🚨 &lt;em&gt;Please note&lt;/em&gt;: at the beginning of this &lt;code&gt;website-banner.js&lt;/code&gt; script, you need to customize &lt;code&gt;projectId&lt;/code&gt;, &lt;code&gt;environment&lt;/code&gt;, &lt;code&gt;statusHolderEndpoint&lt;/code&gt; and &lt;code&gt;interval&lt;/code&gt; variables with your own information, and please also customize the default message on &lt;a href="https://github.com/upsun/snippets/blob/main/src/website-banner.js#L33" rel="noopener noreferrer"&gt;line 33&lt;/a&gt;. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Then, you need to import this script into your application. In your Symfony Demo application, you just need to add this Javascript command at the beginning of the import section of your &lt;code&gt;assets/admin.js&lt;/code&gt; file:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import …
import './js/website-banner.js';


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;🚨 &lt;em&gt;Please note&lt;/em&gt;: If you’re not using a Symfony application, please refer to the corresponding official stack guide on how to include a Javascript file into your application. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;3. Include Notiflix assets in your HTML layout&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;The &lt;a href="https://notiflix.github.io/" rel="noopener noreferrer"&gt;Notiflix JS library&lt;/a&gt; needs some assets, so you need to include the following instruction in your HTML layout, within the HTML &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; section: &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://cdn.jsdelivr.net/npm/notiflix@3.2.7/dist/notiflix-aio-3.2.7.min.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://cdn.jsdelivr.net/npm/notiflix@3.2.7/src/notiflix.min.css"&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;🚨 &lt;em&gt;Please note&lt;/em&gt;: I randomly chose the &lt;a href="https://notiflix.github.io/documentation" rel="noopener noreferrer"&gt;Notiflix JS library&lt;/a&gt; to display the message. This JavaScript banner is displayed on top of everything, preventing the end user from doing anything until the application is back. Feel free to customize this message in whatever way you want. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;4. Commit and deploy&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;You then need to commit your files and deploy your application, using the following command: &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

git add . &amp;amp;&amp;amp; git commit -m "Adding notification banner"
upsun push


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;5. Test it&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;As soon as the notification banner script is deployed on your &lt;code&gt;main&lt;/code&gt; application, you can test it by going to your admin website and using the following command lines: &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

upsun project:clear-build-cache
upsun redeploy


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This will redeploy your application (shorter than a deployment with source modification) and display the banner. You should see something like this on top of all interfaces that include this notification banner script.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx5z5rf4r5vr5deksg6ni.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx5z5rf4r5vr5deksg6ni.gif" alt="Result Banner" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Et voilà, you’re all set and will now automatically inform your visitors on production to wait a few moments while maintenance of your application is happening and prevent displaying those annoying 5xx HTTP errors.&lt;/p&gt;

</description>
      <category>upsun</category>
      <category>javascript</category>
      <category>maintenance</category>
      <category>banner</category>
    </item>
  </channel>
</rss>
