<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Patrick O&#39;Neill on Medium]]></title>
        <description><![CDATA[Stories by Patrick O&#39;Neill on Medium]]></description>
        <link>https://medium.com/@patrickjamesoneill?source=rss-10f568f004af------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*3IQ47ZGHTeekc19mSctmnA.png</url>
            <title>Stories by Patrick O&amp;#39;Neill on Medium</title>
            <link>https://medium.com/@patrickjamesoneill?source=rss-10f568f004af------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 01:31:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@patrickjamesoneill/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Rebranding TravelPerk]]></title>
            <link>https://builders.perk.com/rebranding-travelperk-4fb632aad903?source=rss-10f568f004af------2</link>
            <guid isPermaLink="false">https://medium.com/p/4fb632aad903</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[travel]]></category>
            <category><![CDATA[rebranding]]></category>
            <category><![CDATA[design]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Patrick O'Neill]]></dc:creator>
            <pubDate>Tue, 02 Sep 2025 10:36:51 GMT</pubDate>
            <atom:updated>2025-09-03T09:37:48.689Z</atom:updated>
            <content:encoded><![CDATA[<p>I remember finding out about the rebrand in a meeting towards the end of 2024 and learning the deadline was the 31st March 2025. As is usually a good idea in these situations, I pushed at that to see if it was really the deadline and how much wiggle room we might have and it turned out there wasn’t any…</p><p>We had a hard deadline and needed to come up with a plan of not only updating Suitcase components (our design system and component library) but also giving teams the tools and documentation to update to the new components.</p><h3>🖌️ Design System</h3><p>Using a design system and component library streamlines changes by centralising them. The design system uses design tokens which define our colours, spacing and typography. Components are also part of the design system and they allow reuse across the application ensuring consistency and reducing duplication of effort.</p><h3>🛠️ Preparation</h3><p>While this may sound obvious, if suitcase components and design tokens were not being used when they could have been, they would not benefit from the new components. Also if suitcase components had their styles modified there was a risk of them not sticking to the new design system.</p><p>We also had a mixture of 3 colour systems: hex codes, design tokens and design tokens as CSS variables and wanted to migrate to using only the CSS variables option so we could update in one place and the changes would propagate everywhere. In future this would make adding new themes (dark mode, high contrast, colour blind) extremely simple by just swapping out design tokens.</p><p>So using an Abstract Syntax Tree to analyse the code in the frontend we flagged components to be reviewed and see if suitcase offered a suitable alternative, flagged when suitcase components were being styled so those styles could be removed and flagged when CSS variables weren’t being used.</p><p>This meant before starting the rebrand work the adoption of suitcase components was increased and we were using one color system meaning less work for the teams down the line.</p><h3>⭐️ Philosophy and Goals</h3><p>Now that preparation was complete we could move on to creating the rebranded suitcase components but before starting we wanted some goals that could help us make decisions along the way.</p><blockquote><em>1. Make the new components stick rigidly to the new design system — if we don’t we will have an inconsistent UI and a lot of the work will have been pointless.</em></blockquote><blockquote><em>2. Minimise the amount of work needed by developers to use the new components.</em></blockquote><blockquote><em>3. Decrease our tech debt by using this as an opportunity to replace deprecated libraries for example.</em></blockquote><blockquote><em>4. Improve the code quality and accessibility of components.</em></blockquote><h3>✏️ A rewrite?</h3><p>Something that was ruled out early on was a complete rewrite of the frontend as this would be a huge undertaking, we had tight time constraints and during working on the rebranding teams would still want to release new features.</p><h3>🗒️ The Plan</h3><p>So we had our constraints and now was time to make a plan. We needed a way to be able to switch between an original and rebranded version of the React application.</p><p>The solution we came up with was to wrap the whole application in a way which let the React app know whether is was in rebrand mode or not.</p><p>As well as providing this the Provider would also switch out the design tokens to use the rebranded values and also attached functions to the window allowing users to toggle the rebrand on and off via the console.</p><p>The current rebrand value could be used by any component by using this hook:</p><pre>const { isRebrandActive } = useRebrand()</pre><p>Behind the scenes in suitcase components we could use this hook to return a Rebranded version of the component rather than the original:</p><pre>// Conditionally render rebranded component using hook value<br>function SuitcaseComponent(props: Props){<br>    const {isRebrandActive} = useRebrand()<br>    if(isRebrandActive){<br>      return &lt;RebrandSuitcaseComponent {...props} /&gt;<br>    }<br>    // old component logic<br>}</pre><p>This meant that:</p><ul><li>Developers could use the hook in their own components to change things based on what the isRebrandActive value was. For example adjusting layout, spacing and colors for new page designs.</li><li>By using the functions (turnOn / turnOff ) we had attached to the window we could toggle between the original and rebranded components and toggle the isRebrandActive value. This allowed developers to toggle back and forth to see their changes and check they haven’t broken anything in the original UI without having to rebuild the application saving lots of time (30s — 1min for every switch).</li><li>We could pass new props that would be forwarded on to the rebranded component when needed without affecting the original component.</li><li>Rebranded components could be “released” in a low stakes environment. They were not being used by our customers but by the developers consuming the components and they could find bugs and report them back to the suitcase team</li></ul><p>Press enter or click to view image in full size</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*LeYTjAuo6XezZFaq.gif" /></figure><p>Toggling between versions using the console</p><h3>💅 Beyond a Reskin</h3><p>This “low stakes” environment enabled us to do more than a simple reskin. Reskinning the components would have been the easy option but this was a huge opportunity for us to improve the suitcase component library.</p><p>Some of the components hadn’t been touched for years and needed some love. Normally updating a component is a difficult process as they are used in lots of different places in different ways so any update requires a lot of manual testing to check no edge case bugs have been introduced. As the rebranded components wouldn’t be getting shipped straight to customers this was a great chance to make improvements and have internal testers flag any issues.</p><p>This also allowed us to remove troublesome dependencies that were for example blocking an update to a newer version of React.</p><h3>🔒 Tightening up component APIs</h3><p>A lot of our components had loose APIs which left them open to abuse — mainly editing styles — and lead to inconsistencies in the UI.</p><p>Here is a common example when using Icons:</p><pre>// open api<br>// the user is free to set the color and size of the icon and not match the design system<br>&lt;IconAndText<br>  icon={&lt;PlaneIcon size={36} color=&quot;blue&quot; /&gt;} <br>  text=&quot;This is an open example&quot;<br>/&gt;</pre><pre>// tighter api<br>// in this case we just tell the component which icon and suitcase <br>// takes control of size and color ensuring consistency<br>&lt;IconAndText<br>  iconType={PlaneIcon} <br>  text=&quot;This is an open example&quot;<br>/&gt;</pre><h3>🧪 Beta Testing &amp; Codemods</h3><p>At this point we had completed most of the components but not yet released them so we still had the opportunity to change things. With the help of a fellow engineer we tested updating 2 pages of their MFE so we could find migration pain points and change them before release giving an easier migration.</p><p>During this we found that having a new prop for things that may change between the original and rebrand was a better pattern than some kind of mapping under the hood or using a ternary.</p><pre>// single prop<br>&lt;SuitcaseComponent<br>  textColor={isRebrandActive ? &#39;textSecondary&#39; : &#39;textTertiary&#39;}<br>/&gt;</pre><pre>// two props<br>&lt;SuitcaseComponent<br>  textColor=&quot;textTertiary&quot;<br>  rebrandTextColor=&quot;textSecondary&quot;<br>/&gt;</pre><p>There were several advantages to this approach:</p><ul><li>We wouldn’t need to call the hook and add extra boilerplate.</li><li>We didn’t need to use mappings under the hood which would be a form of tech debt and give further work when removing the mappings.</li><li>We could use codemods saving developers work (from our goals: Minimise the amount of work needed by developers to use the new components.)</li><li>We could use a codemod to generate a sensible value for rebrandTextColor based on the value of textColor and then set that</li><li>Post rebrand we could write another codemod to remove the old prop and potentially rename the new prop.</li><li>We could write a script to highlight when a required rebrand prop had not been set, allowing developers to quickly find and update these.</li></ul><p>The codemods were a big success and saved lots of developer hours (one of the codemods was for our FormattedText component which has 6000+ uses)</p><h3>💞 Deprecation Visibility (Pink Strategy)</h3><p>As part of the rebrand some of our color design tokens were being deprecated, so to make this obvious when switching to rebrand any deprecated color token was set to a beautiful hot pink.</p><p>This was another win allowing the use of old design tokens to be easily spotted and then removed in the rebranded UI. This means they can be safely removed post rebrand and not have to hang around and be tech debt.</p><p>Press enter or click to view image in full size</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*G3xpMiGqe5loHqN7.png" /></figure><p>Deprecated colours showing as pink</p><h3>📖 Component Release &amp; Rebrand Guide</h3><p>Although we hadn’t finished all the components it was time to release what we had done so that the squads could begin to use them. This could have been considered early but we wanted squads to rebrand their pages so they could be tested and issues with the components fixed.</p><p>To release the components we produced a Rebrand Guide which contained everything the squads would need to know to toggle the rebrand, run the codemods, a guide for updating each component usage and suggested patterns for updating layouts.</p><h3>🔍 Testing Strategy</h3><p>At this stage we had told the squads to focus on updating their components but there was trouble on the horizon.</p><p>At the moment all our tests passed because they only ever tested the old UI however for the rebrand to go live we would toggle the value of isRebrandActive to be a default of true.</p><p>At this point all the tests would run using the rebranded components. Although some tests would pass there were a number of components that had big enough differences to make tests fail.</p><p>The first part of the solution to this was to have two pipelines and original and a rebrand pipeline which have the values of isRebrandActive set to false and true respectively. This was done using some Vite bundler tricks to inject the value at build time.</p><p>There would be some tests that passed in both pipelines which was great and we didn’t need to do anything with those.</p><p>For tests that failed in the rebrand pipeline they could:</p><ul><li>Be updated so they pass in both pipelines</li><li>Split into an original and rebrand test</li></ul><p>Once these updates had been done the testing pipelines for the original and rebranded UI were green meaning we could toggle the value of isRebrandActive to true and the testing pipeline would pass and we would be able to deploy the web app.</p><blockquote><em>This system also meant we maintained the same amount of testing from before to after the rebrand and maintained confidence in our code despite the </em><strong><em>1000’s</em></strong><em> of changes.</em></blockquote><h3>👭 More Testing! Internal QA</h3><p>The main difference between a normal release and going live with the rebrand was the amount of internal QA. Normally PRs have unit tests and are manually tested by the author and reviewer.</p><p>However for the rebrand we had numerous QA sessions both within builders and the wider TravelPerk community. These sessions were instrumental in testing the product and raising issues that needed to be fixed by the squads and the suitcase team.</p><p>The suitcase team would like to thank all who participated in these sessions as they were extremely valuable and allowed us to ship a polished product on release day.</p><h3>⚡️ Going Live</h3><p>Press enter or click to view image in full size</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*oMskedphoUIurQv7.png" /></figure><p>The Control Room</p><p>On the 31st March all the required people gathered in the Control Room and it was time to go live.</p><p>To roll out the rebrand we used a cookie which could quickly be changed in Cloudfront allowing us to quickly change back in the event of problems and not rely on the frontend pipeline which could take 15+ minutes to revert.</p><p>The rebrand was turned on and after a few minutes of frantic refreshing we started to see the rebranded version of the site, confirmed everything was working and monitored dashboards for errors.</p><p>Later in the morning we then merged a PR where the value of isRebrandActive was set to true and we were done.</p><p>It went extremely smoothly (it almost felt like an anticlimax) and we could now sit back, relax and take a breath!</p><p><em>We’re hiring! Explore our job opportunities </em><a href="https://www.travelperk.com/careers"><em>here</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4fb632aad903" width="1" height="1" alt=""><hr><p><a href="https://builders.perk.com/rebranding-travelperk-4fb632aad903">Rebranding TravelPerk</a> was originally published in <a href="https://builders.perk.com/">The Builders</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Rebranding TravelPerk]]></title>
            <link>https://patrickjamesoneill.medium.com/rebranding-travelperk-b742a640bc5a?source=rss-10f568f004af------2</link>
            <guid isPermaLink="false">https://medium.com/p/b742a640bc5a</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[rebranding]]></category>
            <category><![CDATA[travel]]></category>
            <category><![CDATA[design]]></category>
            <dc:creator><![CDATA[Patrick O'Neill]]></dc:creator>
            <pubDate>Tue, 02 Sep 2025 10:02:31 GMT</pubDate>
            <atom:updated>2025-09-02T10:34:25.751Z</atom:updated>
            <content:encoded><![CDATA[<p>I remember finding out about the rebrand in a meeting towards the end of 2024 and learning the deadline was the 31st March 2025. As is usually a good idea in these situations, I pushed at that to see if it was really the deadline and how much wiggle room we might have and it turned out there wasn’t any…</p><p>We had a hard deadline and needed to come up with a plan of not only updating Suitcase components (our design system and component library) but also giving teams the tools and documentation to update to the new components.</p><h3>🖌️ Design System</h3><p>Using a design system and component library streamlines changes by centralising them. The design system uses design tokens which define our colours, spacing and typography. Components are also part of the design system and they allow reuse across the application ensuring consistency and reducing duplication of effort.</p><h3>🛠️ Preparation</h3><p>While this may sound obvious, if suitcase components and design tokens were not being used when they could have been, they would not benefit from the new components. Also if suitcase components had their styles modified there was a risk of them not sticking to the new design system.</p><p>We also had a mixture of 3 colour systems: hex codes, design tokens and design tokens as CSS variables and wanted to migrate to using only the CSS variables option so we could update in one place and the changes would propagate everywhere. In future this would make adding new themes (dark mode, high contrast, colour blind) extremely simple by just swapping out design tokens.</p><p>So using an Abstract Syntax Tree to analyse the code in the frontend we flagged components to be reviewed and see if suitcase offered a suitable alternative, flagged when suitcase components were being styled so those styles could be removed and flagged when CSS variables weren’t being used.</p><p>This meant before starting the rebrand work the adoption of suitcase components was increased and we were using one color system meaning less work for the teams down the line.</p><h3>⭐️ Philosophy and Goals</h3><p>Now that preparation was complete we could move on to creating the rebranded suitcase components but before starting we wanted some goals that could help us make decisions along the way.</p><blockquote>1. Make the new components stick rigidly to the new design system — if we don’t we will have an inconsistent UI and a lot of the work will have been pointless.</blockquote><blockquote>2. Minimise the amount of work needed by developers to use the new components.</blockquote><blockquote>3. Decrease our tech debt by using this as an opportunity to replace deprecated libraries for example.</blockquote><blockquote>4. Improve the code quality and accessibility of components.</blockquote><h3>✏️ A rewrite?</h3><p>Something that was ruled out early on was a complete rewrite of the frontend as this would be a huge undertaking, we had tight time constraints and during working on the rebranding teams would still want to release new features.</p><h3>🗒️ The Plan</h3><p>So we had our constraints and now was time to make a plan. We needed a way to be able to switch between an original and rebranded version of the React application.</p><p>The solution we came up with was to wrap the whole application in a way which let the React app know whether is was in rebrand mode or not.</p><p>As well as providing this the Provider would also switch out the design tokens to use the rebranded values and also attached functions to the window allowing users to toggle the rebrand on and off via the console.</p><p>The current rebrand value could be used by any component by using this hook:</p><pre>const { isRebrandActive } = useRebrand()</pre><p>Behind the scenes in suitcase components we could use this hook to return a Rebranded version of the component rather than the original:</p><pre>// Conditionally render rebranded component using hook value<br>function SuitcaseComponent(props: Props){<br>    const {isRebrandActive} = useRebrand()<br>    if(isRebrandActive){<br>      return &lt;RebrandSuitcaseComponent {...props} /&gt;<br>    }<br>    // old component logic<br>}</pre><p>This meant that:</p><ul><li>Developers could use the hook in their own components to change things based on what the isRebrandActive value was. For example adjusting layout, spacing and colors for new page designs.</li><li>By using the functions (turnOn / turnOff ) we had attached to the window we could toggle between the original and rebranded components and toggle the isRebrandActive value. This allowed developers to toggle back and forth to see their changes and check they haven’t broken anything in the original UI without having to rebuild the application saving lots of time (30s — 1min for every switch).</li><li>We could pass new props that would be forwarded on to the rebranded component when needed without affecting the original component.</li><li>Rebranded components could be “released” in a low stakes environment. They were not being used by our customers but by the developers consuming the components and they could find bugs and report them back to the suitcase team</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*49wkVghawz4nGyQ4A3MQUg.gif" /><figcaption>Toggling between versions using the console</figcaption></figure><h3>💅 Beyond a Reskin</h3><p>This “low stakes” environment enabled us to do more than a simple reskin. Reskinning the components would have been the easy option but this was a huge opportunity for us to improve the suitcase component library.</p><p>Some of the components hadn’t been touched for years and needed some love. Normally updating a component is a difficult process as they are used in lots of different places in different ways so any update requires a lot of manual testing to check no edge case bugs have been introduced. As the rebranded components wouldn’t be getting shipped straight to customers this was a great chance to make improvements and have internal testers flag any issues.</p><p>This also allowed us to remove troublesome dependencies that were for example blocking an update to a newer version of React.</p><h3>🔒 Tightening up component APIs</h3><p>A lot of our components had loose APIs which left them open to abuse — mainly editing styles — and lead to inconsistencies in the UI.</p><p>Here is a common example when using Icons:</p><pre>// open api<br>// the user is free to set the color and size of the icon and not match the design system<br>&lt;IconAndText<br>  icon={&lt;PlaneIcon size={36} color=&quot;blue&quot; /&gt;} <br>  text=&quot;This is an open example&quot;<br>/&gt;<br><br>// tighter api<br>// in this case we just tell the component which icon and suitcase <br>// takes control of size and color ensuring consistency<br>&lt;IconAndText<br>  iconType={PlaneIcon} <br>  text=&quot;This is an open example&quot;<br>/&gt;<br></pre><h3>🧪 Beta Testing &amp; Codemods</h3><p>At this point we had completed most of the components but not yet released them so we still had the opportunity to change things. With the help of a fellow engineer we tested updating 2 pages of their MFE so we could find migration pain points and change them before release giving an easier migration.</p><p>During this we found that having a new prop for things that may change between the original and rebrand was a better pattern than some kind of mapping under the hood or using a ternary.</p><pre>// single prop<br>&lt;SuitcaseComponent<br>  textColor={isRebrandActive ? &#39;textSecondary&#39; : &#39;textTertiary&#39;}<br>/&gt;<br><br>// two props<br>&lt;SuitcaseComponent<br>  textColor=&quot;textTertiary&quot;<br>  rebrandTextColor=&quot;textSecondary&quot;<br>/&gt;</pre><p>There were several advantages to this approach:</p><ul><li>We wouldn’t need to call the hook and add extra boilerplate.</li><li>We didn’t need to use mappings under the hood which would be a form of tech debt and give further work when removing the mappings.</li><li>We could use codemods saving developers work (from our goals: Minimise the amount of work needed by developers to use the new components.)</li><li>We could use a codemod to generate a sensible value for rebrandTextColor based on the value of textColor and then set that</li><li>Post rebrand we could write another codemod to remove the old prop and potentially rename the new prop.</li><li>We could write a script to highlight when a required rebrand prop had not been set, allowing developers to quickly find and update these.</li></ul><p>The codemods were a big success and saved lots of developer hours (one of the codemods was for our FormattedText component which has 6000+ uses)</p><h3>💞 Deprecation Visibility (Pink Strategy)</h3><p>As part of the rebrand some of our color design tokens were being deprecated, so to make this obvious when switching to rebrand any deprecated color token was set to a beautiful hot pink.</p><p>This was another win allowing the use of old design tokens to be easily spotted and then removed in the rebranded UI. This means they can be safely removed post rebrand and not have to hang around and be tech debt.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/881/1*J2yr8VKFE2NRZulRoRcyGQ.png" /><figcaption>Deprecated colours showing as pink</figcaption></figure><h3>📖 Component Release &amp; Rebrand Guide</h3><p>Although we hadn’t finished all the components it was time to release what we had done so that the squads could begin to use them. This could have been considered early but we wanted squads to rebrand their pages so they could be tested and issues with the components fixed.</p><p>To release the components we produced a Rebrand Guide which contained everything the squads would need to know to toggle the rebrand, run the codemods, a guide for updating each component usage and suggested patterns for updating layouts.</p><h3>🔍 Testing Strategy</h3><p>At this stage we had told the squads to focus on updating their components but there was trouble on the horizon.</p><p>At the moment all our tests passed because they only ever tested the old UI however for the rebrand to go live we would toggle the value of isRebrandActive to be a default of true.</p><p>At this point all the tests would run using the rebranded components. Although some tests would pass there were a number of components that had big enough differences to make tests fail.</p><p>The first part of the solution to this was to have two pipelines and original and a rebrand pipeline which have the values of isRebrandActive set to false and true respectively. This was done using some Vite bundler tricks to inject the value at build time.</p><p>There would be some tests that passed in both pipelines which was great and we didn’t need to do anything with those.</p><p>For tests that failed in the rebrand pipeline they could:</p><ul><li>Be updated so they pass in both pipelines</li><li>Split into an original and rebrand test</li></ul><p>Once these updates had been done the testing pipelines for the original and rebranded UI were green meaning we could toggle the value of isRebrandActive to true and the testing pipeline would pass and we would be able to deploy the web app.</p><blockquote>This system also meant we maintained the same amount of testing from before to after the rebrand and maintained confidence in our code despite the <strong>1000’s</strong> of changes.</blockquote><h3>👭 More Testing! Internal QA</h3><p>The main difference between a normal release and going live with the rebrand was the amount of internal QA. Normally PRs have unit tests and are manually tested by the author and reviewer.</p><p>However for the rebrand we had numerous QA sessions both within builders and the wider TravelPerk community. These sessions were instrumental in testing the product and raising issues that needed to be fixed by the squads and the suitcase team.</p><p>The suitcase team would like to thank all who participated in these sessions as they were extremely valuable and allowed us to ship a polished product on release day.</p><h3>⚡️ Going Live</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*XVwy-OXDZam0rXiTu2FwPQ.png" /><figcaption>The Control Room</figcaption></figure><p>On the 31st March all the required people gathered in the Control Room and it was time to go live.</p><p>To roll out the rebrand we used a cookie which could quickly be changed in Cloudfront allowing us to quickly change back in the event of problems and not rely on the frontend pipeline which could take 15+ minutes to revert.</p><p>The rebrand was turned on and after a few minutes of frantic refreshing we started to see the rebranded version of the site, confirmed everything was working and monitored dashboards for errors.</p><p>Later in the morning we then merged a PR where the value of isRebrandActive was set to true and we were done.</p><p>It went extremely smoothly (it almost felt like an anticlimax) and we could now sit back, relax and take a breath!</p><p><em>We’re hiring! Explore our job opportunities </em><a href="https://www.travelperk.com/careers"><em>here</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b742a640bc5a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why betting accumulators aren’t as good as they seem]]></title>
            <link>https://patrickjamesoneill.medium.com/why-betting-accumulators-arent-as-good-as-they-seem-d449de75cb9f?source=rss-10f568f004af------2</link>
            <guid isPermaLink="false">https://medium.com/p/d449de75cb9f</guid>
            <dc:creator><![CDATA[Patrick O'Neill]]></dc:creator>
            <pubDate>Sun, 13 Feb 2022 07:08:27 GMT</pubDate>
            <atom:updated>2022-02-13T07:08:27.913Z</atom:updated>
            <content:encoded><![CDATA[<p>Every Saturday thousands of people will put an accumulator on that day’s matches, the appeal of accumulators being that the odds soon rack up and large sums can be made from small stakes. The bookies are constantly pushing and promoting them even offering offer such as money back if you are one leg off. Are they a good idea though?</p><h3>Assumptions</h3><p>We will start with some assumptions. Firstly the bookies odds are not off for whatever reason (they are generally very good and if not will be corrected by the <a href="https://en.wikipedia.org/wiki/Wisdom_of_the_crowd">Wisdom of the Crowd</a>). Secondly that you are betting on a market where you can bet on opposing outcomes (1x2, BTTS, Under/Over etc) which keeps the bookies honest as wisdom of the crowd will correct it.</p><h3>Bookmaker’s Margin</h3><p>The first thing we need to do is calculate the <a href="https://help.smarkets.com/hc/en-gb/articles/214180145-How-to-calculate-betting-margins">bookmakers margin</a>.</p><p>I will use Manchester United vs Crystal Palace on the 5th December 2021 as an example using odds from Paddy Power.</p><p>For 1x2 the edge is 7.14%, BTTS is 5.92%, Over/Under 2.5% is 8.25%.</p><p>From this we can see the bookies margin is usually around 7% give or take for the different markets. (Other games this day are Leeds vs Brentford 1x2 = 6.3%, Tottenham vs Norwich 1x2 = 7.5% and Aston Villa vs Leicester City 1x2 = 7.1%).</p><h3>Bookies Odds vs Fair Odds</h3><p>What does this mean though? Well it means you are getting from the bookmakers are worse than the actual odds of the event occurring, which is to be expected as this is how bookmakers make their money.</p><p>The odds and fair odds for the Manchester United vs Crystal Palace match mentioned above are in the table below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/427/1*AYxLJetyFANT9lstMvnQ_g.png" /><figcaption>Odds vs Fair Odds</figcaption></figure><p>What does this mean for accumulators? As the odds for accumulators rack up so does the 7% bookmaker’s margin much like compound interest payments.</p><h3>Accumulator Legs vs Margin</h3><p>On a single match you are getting 7% worse odds than the actual real odds of that happening. By the time you get to a six leg accumulator if the bookies are giving you odds of 51.00 (50/1) the fair odds would actually be 76.00 (75/1).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/206/1*QpnY4hyCq_IfeI4EBBcgiQ.png" /><figcaption>Bookmaker’s Margin vs Legs</figcaption></figure><p>So you can see why the bookies are so keen for you to do accumulators as it increases their margin.</p><h3>Boosted bets</h3><p>If you go onto bookmaker’s websites you will commonly see boosted accumulators so lets have a look at one.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/330/1*Cz4Nr_NoT_r7GfaByBVP0w.png" /><figcaption>Example boost</figcaption></figure><p>We can see here the odds have been increased from 4.00 (3/1) to 4.33 (10/3) although boosts are always good what we want is something to get over the bookies margin. Assuming the odds are good in the first place the odds are actually only 8% better but we know with trebles the bookies margin is up around 22.5% so we are still losing out!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/961/1*M7uAY7mNixa8YsSoXqOWhg.png" /><figcaption>Another boost</figcaption></figure><p>However on the same day we can see this boost from 4.5 (7/2) to 7.0 (6/1) which is over 50% boost in odds and comfortable overcomes the 22.5% bookmaker’s margin. Unsurprisingly the bet is limited to £10!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d449de75cb9f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Boosted Odds Bet Analysis]]></title>
            <link>https://patrickjamesoneill.medium.com/boosted-odds-bet-analysis-9d0bc7cb63fa?source=rss-10f568f004af------2</link>
            <guid isPermaLink="false">https://medium.com/p/9d0bc7cb63fa</guid>
            <dc:creator><![CDATA[Patrick O'Neill]]></dc:creator>
            <pubDate>Sun, 31 Oct 2021 09:09:43 GMT</pubDate>
            <atom:updated>2021-10-31T11:23:05.953Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/342/1*uWuCpr7Uo1vN-kjrMfdwqw.png" /></figure><p>Today SkyBet has Raphinha boosted from 2/1 to 4/1 to have 2+ shots on target and I want to investigate how good of a deal this is and if it offers potential value.</p><p>To do this I’m going to use my site <a href="https://patsstats.co.uk/">Pat’s Stats</a> and dig into the numbers.</p><p>I’m in the process of working on player specific pages and here are the current stats from the Raphinha page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/945/1*-QhWcD0P3Byh60Uo2BR6Zw.png" /></figure><p>The link for this page is <a href="https://patsstats.co.uk/player/player">here</a> (although is may change in future).</p><p>Leeds will be away at Norwich and we can see that in the last 5 away games, this seasons away games and a rolling seasons away games he is getting 2 shots+ on target per game in 20%, 33% and 23.5% of games respectively.</p><p>I feel one of the important things here is to compare apples with apples, often when promoting a shot on target bet the bookies will say 10 shots in targets in the last 8 games. However this is not what you are betting on if the player had 2 shots on target in 5 games then your bet on 1 shot on target would have only come in 5/8 times (62.5%).</p><p>Not taking into account the strength of the opposition we can see that Raphinha is at least 20% likely to get 2+ shots on targets. 20% probability would convert to fair odds of 4/1.</p><p>Now lets look at the opposition strength.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/812/1*LwzFLOHB3SVyLVCpupLpHw.png" /></figure><p>When at home Norwich are conceding the 3rd most amount of shots on target this season, (although it’s still early in the season so not a great sample size) conceding 36.5% more than average shots on target.</p><p>When combining these two factors you can see that there is probably some value in the bet for Raphinha to have 2+ shots on target but that the initial odds of 2/1 would have been pretty terrible.</p><p>Pat’s Stats is a work in progress but please check it out and all feedback is welcome.</p><p>Thanks!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9d0bc7cb63fa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Reducing API calls for Next.js SSG]]></title>
            <link>https://patrickjamesoneill.medium.com/reducing-api-calls-for-next-js-ssg-d48ca1251e7d?source=rss-10f568f004af------2</link>
            <guid isPermaLink="false">https://medium.com/p/d48ca1251e7d</guid>
            <category><![CDATA[nextjs]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[static-site-generator]]></category>
            <dc:creator><![CDATA[Patrick O'Neill]]></dc:creator>
            <pubDate>Fri, 05 Mar 2021 15:35:28 GMT</pubDate>
            <atom:updated>2021-05-09T06:23:51.300Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/508/1*ZIC6XfhJD2A63OKjlX-KNg.png" /></figure><p>I have been using Next.js to create a statically generated site based upon data from an external API. I’ve been having a lot of problems with repeated calls to the same api route (which returns the same data) each time.</p><p>The reason this is a bit of a headache for me is that:</p><ul><li>I am paying for the api and I only have a fixed number of calls per day</li><li>If I hit the api too rapidly the api won’t return the data to me due to rate limiting.</li><li>It’s a waste of time and bandwidth hitting the same route again and again when I should already have that data.</li></ul><p>I tried a few things to start with.</p><p>In the _app.js component I tried to use getInitialProps as this would then share the props to all components in the app which for my use case would have been fine. It did do that however it seemed that whenever a new static page was generated a new _app.js was being fired up and therefore was doing the same amount of api calls just from a different place in the code.</p><p>I also tried using react context and wrapping the App component in a Context.Provider but I think the same thing as above was happening where the _app.js was fired up for everypage so there was no reduction in api calls.</p><p>The solution I came to was to cache the responses locally myself as .json files.</p><pre>import fs from &#39;fs&#39;</pre><pre>export const fileExists = (path: string) =&gt; {<br>  return fs.existsSync(path)<br>}</pre><pre>export const writeFile = async (path: string, data: any) =&gt; {<br>  const dataToWrite = JSON.stringify(data)<br>  await fs.writeFile(path, dataToWrite, (err) =&gt; { err ?     console.error(err) : console.log(&#39;files saved to json: &#39;, path) } )<br>}</pre><pre>export const readFile = async (path: string) =&gt; {<br>  return new Promise((resolve, reject) =&gt; {<br>    fs.readFile(path, &#39;utf8&#39;, (error, data) =&gt; {<br>      if (error) {<br>        reject(error);<br>      }<br>      resolve(JSON.parse(data));<br>    });<br>  });<br>}</pre><pre>export const routeStringToPathName = (route: string) =&gt; {<br>  const baseUrlRemoved = route.slice(34)<br>  const fileName = baseUrlRemoved.replace(&#39;/&#39;, &#39;_&#39;)<br>  const pathName = &#39;./json/&#39; + fileName + &#39;.json&#39;<br>  return pathName<br>}</pre><p>The above was created in a separate .js file.</p><p>There are four functions one to check if the file already exists (ie have I tried to hit this api endpoint before), another that writes to a .json file, another that reads from a .json file and another that converts a url so a file name and path.</p><p>I then import and use these functions as below (this can be done in a separate .js file and then imported and used in getStaticProps) :</p><pre>export const fetchApiData = async (param1, param2) =&gt; {<br>  const myHeaders = new Headers();<br>  myHeaders.append(&#39;api-key&#39;, &#39;123456&#39;);</pre><pre>  const requestString = `https://data-api.io/data?  param1=${param1}&amp;param2=${param2}`;</pre><pre>  const myRequest = new Request(requestString, {<br>    method: &#39;GET&#39;,<br>    headers: myHeaders,<br>    mode: &#39;cors&#39;,<br>    cache: &#39;default&#39;,<br>  })</pre><pre>  const pathName = routeStringToPathName(requestString)</pre><pre>  let timeout = await new Promise(resolve =&gt; setTimeout(resolve, 1000))</pre><pre>  if(fileExists(pathName)){<br>    const fileReturned = await readFile(pathName)<br>    return await fileReturned<br>  } else {<br>    let res = await fetch(myRequest)<br>    let result = await res.json()<br>    await writeFile(pathName, result)<br>    return result<br>  }<br>}</pre><p>I’ve included params and an api key example but this would need editing for the particular api and end point you are using.</p><p>The api fetch is set up and then the requestString is passed into the routeStringToPathName function where it is returned in the format ./json/filename.json (ie saved to the json folder).</p><p>I have included a timeout as I think that Next.js can fire out the static pages so quickly it can be hitting the api route again before it has returned the first time and the result written to json.</p><p>Next it looks at the pathName and checks if the file exists (ie the endpoint has been hit, returned data and a file created.) If it does exist it will return the contents of the file and if it doesn’t it will carry out the fetch and then right the result to a file.</p><p>While this is far from perfect and there are undoubtedly many improvements to be made it works and reduced the number of api calls for me by a lot.</p><p>I was given lots of suggestions of things that might work by people but most seemed not to (or would in SSR but not SSG). This also feels like it must be a common problem and will probably be solved in a future version of Next.</p><p>One last caveat is to be careful if the data you are fetching from the api will change over time and you are caching the end result as it will become out of date. I solved this by editing my package.json and adding a deploy script that will remove the json folder and create a new empty one before building and exporting the SSG site.</p><pre>&quot;deploy&quot;: &quot;rm -rf json &amp;&amp; mkdir json &amp;&amp; npm run build &amp;&amp; npm run export&quot;</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d48ca1251e7d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Daily, nightly, hourly etc deploys in Netlify using IFTTT Webhooks]]></title>
            <link>https://patrickjamesoneill.medium.com/daily-nightly-hourly-etc-deploys-in-netlify-using-ifttt-webhooks-b2694c1d2ac1?source=rss-10f568f004af------2</link>
            <guid isPermaLink="false">https://medium.com/p/b2694c1d2ac1</guid>
            <category><![CDATA[automated-deployment]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[webhooks]]></category>
            <category><![CDATA[netlify]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Patrick O'Neill]]></dc:creator>
            <pubDate>Fri, 05 Feb 2021 13:17:44 GMT</pubDate>
            <atom:updated>2021-02-05T13:17:44.003Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/565/1*tb4Q4Wc8azumKwRlkqDdvA.png" /></figure><p>Go to your site settings in Netlify and then select Build &amp; Deploy.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/946/1*3rlrA5WXEg9XQxy7CGW0LQ.png" /></figure><p>If your site is not already linked to a GitHub (or other) repo do so now by selecting Link site to Git and follow the instructions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*ziRyUY4BYJS8Y_1DIL37tw.png" /></figure><p>When adding the site make sure the build command and output directory are correct. (Next.js uses out/ as the build folder by default for example.)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/953/1*Hp2h4436RtKiX8Sp_Psa2A.png" /></figure><p>Now in the Build &amp; Deploy menu you should be able to scroll down to Build Hooks and we want to click Add build hook.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/943/1*_yDZWskR81oNp7vDRsspNA.png" /></figure><p>Call it whatever you want and save (I have called mine nightly as the builds will be occurring at night when I will have the data to update my site for the next day).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/947/1*FMKXHHE_I9hHbQX-Zlbtjg.png" /></figure><p>Once you have clicked save you will receive a URL. Keep this handy we will need it later!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/873/1*VllpfjxB3fXg7jPsjAn5_A.png" /></figure><p>Now go to <a href="https://ifttt.com/">ifttt</a> and if you don’t have an account already sign up and log in! Once you are logged in select to create a new applet and on the next screen where it says If This click Add.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vMRmduEPhA_Cn8ncrvUuIA.png" /></figure><p>Now in the applets search for Date &amp; Time and select it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dWzOmHdRRe6GlKJmXmCU1A.png" /></figure><p>Decide how often you would like your build to be triggered and choose a relevant option. Again I want to build once a day at night so I will be selecting Every day at.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/733/1*xeagNNsVlsYuo1SDlkOQyw.png" /></figure><p>Select the time you would like to build at and click Create trigger!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/822/1*PjxQ3aTTBUJYQI165BwyLg.png" /></figure><p>You will now be taken back to the previous page and next to Then That click Add.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/926/1*KEZvAg4vjt8ZGuT2egp4vg.png" /></figure><p>Search for and then select the Webhooks service.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/652/1*kpPQ8Xz6381Upqe6d2g-pw.png" /></figure><p>Select Make a web request.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/733/1*arifJXx-w7BKa6f5BCNagQ.png" /></figure><p>Enter the URL created before in Netlify and then in Method select POST, Content Type select application/x-www-form-urlencoded and in the body a pair or curly braces {}</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/883/1*xEjN6t9xM4XZtqhiEG9svQ.png" /></figure><p>The next screen should then look like this and that’s you pretty much done. Click on Continue.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/927/1*RjDq9U9mvJVfcsBEB5VReg.png" /></figure><p>Click Finish and everything should be in place for your automated daily deploys!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b2694c1d2ac1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[404 Not Found with Docker, React Router and Nginx]]></title>
            <link>https://patrickjamesoneill.medium.com/404-not-found-with-docker-react-router-and-nginx-21fdce02c5?source=rss-10f568f004af------2</link>
            <guid isPermaLink="false">https://medium.com/p/21fdce02c5</guid>
            <category><![CDATA[404]]></category>
            <category><![CDATA[nginx]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[Patrick O'Neill]]></dc:creator>
            <pubDate>Tue, 25 Aug 2020 10:28:44 GMT</pubDate>
            <atom:updated>2020-08-25T10:28:44.777Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IDkd865gmnc1cOE-bEuXug.png" /></figure><p>I had found other guides for dockerising a React project using React Router and served using Nginx. However I kept on coming upon this error when going to an address that was not the root (ie /users or /posts). I could navigate through my React SPA and the address bar would update but when going directly to a route I would get a 404 error.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/251/1*JYRfzZnGrognK7LjWyMQYA.png" /><figcaption>Arghhhhh!</figcaption></figure><pre># Build Environment<br>FROM node:14.8.0-alpine as build<br>WORKDIR /app<br>COPY package*.json ./<br>RUN npm ci --silent<br># Have a .dockerignore file ignoring node_modules and build<br>COPY . ./<br>RUN npm run build</pre><pre># Production<br>FROM nginx:stable-alpine<br>COPY --from=build /app/build /usr/share/nginx/html<br>COPY nginx.conf /etc/nginx/nginx.conf<br>CMD [&quot;nginx&quot;, &quot;-g&quot;, &quot;daemon off;&quot;]</pre><p>This was my Dockerfile and all seemed to make sense the local nginx.conf file (in the same folder as the Dockerfile) was overwriting the one in the container and it should use mine instead but this wasn’t happening.</p><pre>server {<br>  listen 80;<br>  location / {<br>    root   /usr/share/nginx/html;<br>    index  index.html index.htm;<br>    try_files $uri $uri/ /index.html;<br>  }<br>}</pre><p>This needs to be in your nginx.conf file and the important bit is the try_files which is it can’t find the route you are looking for will then try index.html as we want to.</p><p>The problem was my in my nginx.conf file I had a line:</p><pre> include /etc/nginx/conf.d/*.conf;</pre><p>This was telling it to include the default config files which seemed to be overwriting my own. Commenting out or deleting this line then building and running the images meant the end of the 404 errors.</p><p>I had created my original nginx.conf file by going into the container using:</p><pre>docker exec -it container_name /bin/sh</pre><p>From there I navigated to the /etc/nginx directory and used:</p><pre>cat nginx.conf</pre><p>To print out the contents of the file and then copied and pasted it into VS Code which left the include default config files in.</p><p>Here is the corrected nginx.conf file:</p><pre># nginx.conf</pre><pre>user  nginx;<br>worker_processes  auto;</pre><pre>error_log  /var/log/nginx/error.log warn;</pre><pre>pid        /var/run/nginx.pid;</pre><pre>events {<br>  worker_connections  1024;<br>}</pre><pre>http {<br>  include       /etc/nginx/mime.types;<br>  default_type  application/octet-stream;<br>  log_format  main  &#39;$remote_addr - $remote_user [$time_local] &quot;$request&quot; &#39;<br>  &#39;$status $body_bytes_sent &quot;$http_referer&quot; &#39;<br>  &#39;&quot;$http_user_agent&quot; &quot;$http_x_forwarded_for&quot;&#39;;</pre><pre>  access_log  /var/log/nginx/access.log  main;</pre><pre>  sendfile        on;<br>  #tcp_nopush     on;<br>  <br>  keepalive_timeout  65;</pre><pre>  #gzip  on;</pre><pre>  #include /etc/nginx/conf.d/*.conf;</pre><pre>server {<br>  listen 80;<br>  location / {<br>    root   /usr/share/nginx/html;<br>    index  index.html index.htm;<br>    try_files $uri $uri/ /index.html;<br>  }<br>}</pre><pre>}</pre><p>I hope this can help some people out and save some head scratching!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=21fdce02c5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dockerising everything]]></title>
            <link>https://patrickjamesoneill.medium.com/dockerising-everything-a1da40d51bf1?source=rss-10f568f004af------2</link>
            <guid isPermaLink="false">https://medium.com/p/a1da40d51bf1</guid>
            <dc:creator><![CDATA[Patrick O'Neill]]></dc:creator>
            <pubDate>Wed, 15 Jul 2020 20:04:49 GMT</pubDate>
            <atom:updated>2020-12-18T10:16:26.288Z</atom:updated>
            <content:encoded><![CDATA[<p>In my previous <a href="https://medium.com/@patrickjamesoneill/dockerising-and-deploying-a-flask-app-fd075ddd26e8">blog</a> I took my Flask backend made it into a Docker image and deployed it onto an AWS EC2 instance.</p><p>This time I’m going to talk about how I used Docker to get my entire stack running on one server (in reality this probably isn’t ideal but I wanted to see if I could do it and I only have one free EC2 instance). My application consists of a React frontend, a Flask backend with a Celery worker, a Redis server for messaging and a MySQL server for the database.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MlUN3QxItAgeZlE2XCRf6A.png" /></figure><p>I have created the Docker image for the Flask backend so this covers the Gunicorn web server and the Celery Worker which handles emails. I don’t need to create a Docker image file for the Redis messaging server or the MySQL database as I can use the standard images for these available from Docker hub and then configure them to be set up the way I would like.</p><p>That leaves me with the frontend which I will create my own Docker image for. I decided to use Nginx as the web server for my frontend. The first thing I needed to do was go to the folder containing my project and run:</p><pre>npm run build</pre><p>This will create the html, css and js and other files that are needed to serve the website and put them in the build folder.</p><pre>FROM nginx:stable-alpine<br>ADD ./build /usr/share/nginx/html<br>CMD [&quot;nginx&quot;, &quot;-g&quot;, &quot;daemon off;&quot;]</pre><p>The above Dockerfile will grab the latest nginx running on Alpine Linux (a small linux distribution which helps keep image sizes down). Next I copied all the files I just created from the build process and copied them into /usr/share/nginx/html which is the folder that nginx uses to store the data it is serving.</p><p>Next I ran the three commands below which will build the image and push it to a dockerhub repository.</p><pre>docker image build -t dncfrontend .<br>docker tag dncfrontend paddyjoneill/dncfrontend<br>docker push paddyjoneill/dncfrontend</pre><h3><strong>Docker Networking</strong></h3><p>Now I had all the docker images I needed it was time to deploy them and get everything networked so the containers could communicate. To do this I created a docker bridge network.</p><p>In a docker bridge network the different containers are able to communicate with each other either by the ip assigned by the docker network or by the containers name using a bridge networks inbuilt dns. In my case I will call the containers: frontend, backend, worker, db and redis.</p><p>I need to give each container a name during the docker run command otherwise Docker randomly assigns a name which I won’t know until it is running. If I assign a name to the container I know what it will be and will be able to call that name from within other containers. During the run command I also need to assign the container a network. For example:</p><pre>docker run -p 5000:5000 --network dnc-net --name backend paddyjoneill/dnc</pre><p>This will start the container, give it the name backend and place it on the dnc-net network. (The -p 5000:5000 maps port 5000 to port 5000 internally and I am able to hit my api with requests.)</p><p>Next I will spin up the MySQL server:</p><pre>docker run -p 3306:3306 -e MYSQL_DATABASE=&quot;db&quot; -e MYSQL_USER=&quot;user&quot; -e MYSQL_PASSWORD=&quot;password&quot; -v /var/lib/mysql:/var/lib/mysql --network dnc-net --name db mysql</pre><p>This is pretty similar to the previous run command apart from here I’m setting environment variables that will be used within the docker container using the -e tag. The -v tag mounts the var/lib/mysql folder from the system to the same folder in the container so that when the container saves data it is saved to the system and not in the container. This way if the container was deleted I do not lose any data that would have been stored within that container. (If the database is currently hosted elsewhere you can export the data from there and import the data into this new database using MySQLWorkbench).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/945/1*oJKIm_Gl9qROljRifr1MUA.jpeg" /><figcaption>Just one more thing</figcaption></figure><p>There is just one more thing I need to do before this “should work” in our backend I will need to change the link pointing to the old database to the new dockerised database at mysql://db:3306/ so I did this rebuilt and pushed my updated docker image and then ran it with the same command as previously.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/383/1*vhlRwGSpnLEBN8q5GOHaIw.png" /><figcaption>Json results back from my api!</figcaption></figure><p>I was now able to hit my api at <a href="http://(ec2/">http://(ec2</a> ip address):5000/api (remember to open port 5000 in the AWS security group!)and get results proving that my dockerised backend was connected to my docker database!</p><p>I then did a similar process to get the redis server up and running and again needed to update my backend to point to the redis container.</p><p>The last thing to get the backend up and running was to run the worker which is the same image as the backend but run with a different command to run the celery worker rather than the flask server.</p><pre>docker run -it — name worker — network daddy-net paddyjoneill/daddynappychange celery -A dnc.celery worker — uid=1</pre><p>The “celery -A dnc.celery worker — uid=1” after the image name tells docker to use the image but replace the run command with this.</p><p>I then tested the set up by creating a new user which would add a user to the database and send an email to the user that they had signed up. I was able to check the database using MySQLWorkbench (port 3306 needs to be open in AWS security group) and I added myself as a user and sure enough I received an email in my inbox!</p><h3>Docker Compose</h3><p>As we can see the docker run commands can get pretty verbose and a bit of a pain. Also bringing up a full stack using these takes a bit of time and is prone to errors. This is where a docker-compose file comes in, this is a yaml file which contains all the information to get your docker-containers up and running and configured correctly. (Docker compose is installed separately from Docker and instructions to install it on Ubuntu are <a href="https://docs.docker.com/compose/install/">here</a>).</p><pre>version: &quot;3.8&quot;</pre><pre>services:<br>  redis:<br>    image: redis:alpine<br>    restart: always<br>    networks:<br>      - dnc-net</pre><pre>backend:<br>  image: paddyjoneill/dnc<br>  restart: always<br>  ports:<br>    - &quot;5000:5000&quot;<br>  networks:<br>    - dnc-net<br>  depends_on:<br>    - redis<br>    - db<br>  command: gunicorn -b :5000 --access-logfile - dnc:app</pre><pre>worker:<br>  image: paddyjoneill/dnc<br>  restart: always<br>  command: celery -A dnc.celery worker --uid=1<br>  networks:<br>    - dnc-net<br>  depends_on:<br>    - redis<br>    - db</pre><pre>frontend:<br>  image: paddyjoneill/dncfrontend<br>  restart: always<br>  ports:<br>    - &quot;80:80&quot;<br>  networks:<br>    - dnc-net<br>  depends_on:<br>    - backend</pre><pre>db:<br>  image: mysql:8.0<br>  restart: always<br>  environment:<br>    MYSQL_DATABASE: &#39;db&#39;<br>    MYSQL_USER: &#39;user&#39;<br>    MYSQL_PASSWORD: &#39;password&#39;<br>    MYSQL_ROOT_PASSWORD: &#39;password&#39;<br>  networks:<br>    - dnc-net<br>  ports:<br>    - &#39;3306:3306&#39;<br>  volumes:<br>    - /var/lib/mysql:/var/lib/mysql</pre><pre>networks:<br>  dnc-net:</pre><p>As you can see the docker-compose.yml file is a lot more readable than the different run commands whilst still conveying the same information. The main differences here are the restart policy which means if a container fails and exits docker-compose will spin it back up. The other new thing we see is the depends_on, this allows you to tailor the order in which your stack is brought up. In this case I bring up the MySQL and Redis servers, I then bring up the backend and the worker which depend on these two services and finally once the backend is up I bring up the frontend.</p><p>Then in the terminal rather than the multitude of different run commands we can now just:</p><pre>docker-compose up</pre><p>You will then see all the containers spin up! If you want to stop running all the containers there are two options:</p><pre>docker-compose stop<br>docker-compose down</pre><p>Stop will simply stop all the containers and down will stop all the containers and then delete them and any networks.</p><p>I was then able to load frontend in my browser using the ip of the ec2 instance which loaded up and was hitting my api and getting results back. Happy Days!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*ConOF0Ppt45JQIhDVw8L5w.jpeg" /><figcaption>Aaaayyy</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a1da40d51bf1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dockerising and deploying a Flask app]]></title>
            <link>https://patrickjamesoneill.medium.com/dockerising-and-deploying-a-flask-app-fd075ddd26e8?source=rss-10f568f004af------2</link>
            <guid isPermaLink="false">https://medium.com/p/fd075ddd26e8</guid>
            <dc:creator><![CDATA[Patrick O'Neill]]></dc:creator>
            <pubDate>Tue, 07 Jul 2020 11:18:29 GMT</pubDate>
            <atom:updated>2020-07-15T20:08:30.718Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QjLjVP9y0vg3fWfmTQQF7g.png" /></figure><p>In my <a href="https://medium.com/@patrickjamesoneill/asynchronous-task-management-with-celery-c328d7bc99cb">previous blog</a> I implemented the use of Celery to manage asynchronous tasks (in my case sending emails).</p><p>The next avenue I’m looking to explore is containerisation with the long term goal of implementing continuous deployment. For my exploration of containers I will be using Docker which is the most popular containerisation solution. However first a bit about containers from Docker “A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.”</p><p>So rather than having separate virtual machines for each application, containers can run alongside other containers on one operating system. This uses much less resources and is a more efficient use of processing power allowing you to do more “work” rather than using that processing power on running many virtual machines.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a5ncwIK2kIyO3ef4tfuNmQ.png" /></figure><p>There are probably much better places to read about the benefits of containers so I will move on to what I did…</p><h3>What I did</h3><h3>Created a Dockerfile</h3><p>The first step of the process is to create a Dockerfile which is a text file which contains all of the commands you would use to make the image from the command line.</p><pre>FROM python:3.8-buster<br>WORKDIR /server<br>ADD . /server<br>RUN pip install -r requirements.txt<br>CMD [&quot;python&quot;, &quot;app.py&quot;]</pre><p>This Dockerfile takes a base Python 3.8 image and then says our working directory will be server and copies the python files to this directory. Then it runs pip to install our required packages in the image. The last line CMD tells the image what command to run if none is given when the image is run.</p><h3>Built the image</h3><p>The next stage was to use the Dockerfile to build the docker image file, the -t tags the image with daddynappychange in this case. (To have access to the command line tools you need to have Docker Desktop installed and running on MacOS).</p><pre>docker image build -t daddynappychange .</pre><h3>Tested the image locally</h3><p>Next up was to test the image locally so it needed to be run. The -p does some port forwarding, I chose 80 so I could just type localhost into the browser and didn’t need to specify a port and this is mapped to port 5000 inside the container which is the port used by the Flask server.</p><pre>docker run -p 80:5000 daddynappychange</pre><p>I had some problems here and a solution I found was to change the ip of the Flask app like below:</p><pre>if __name__ == &#39;__main__&#39;:<br>    app.run(debug=True, host=&#39;0.0.0.0&#39;)</pre><p>Once the image had spun up I was able to hit up my api with requests and get responses that proved it was all working. Tidy.</p><h3>Tagged the image</h3><p>Now that I had the image built and working locally the next step in the process was to upload the image to the dockerhub repository. This would make it easy for my to download my image to wherever it is being hosted. The first step was to tag the daddynappychange image I had created with user/image in this case paddyjoneill/daddynappychange</p><pre>docker tag daddynappychange paddyjoneill/daddynappychange</pre><h3>Pushed image to Dockerhub</h3><p>Then I pushed the image to the dockerhub repository using the below command. You will have to log in to dockerhub in the terminal by using the command docker login and provide your credentials.</p><pre>docker push paddyjoneill/daddynappychange</pre><h3>Deploying to an AWS EC2 instance</h3><p>The first thing to do in the next process is to start an EC2 instance, I chose to create an Ubuntu instance as I have been using Ubuntu recently.</p><p>When you create the instance towards the end of the process it will ask you about a key pair and you will need to download and save the file you will need it shortly.</p><p>Now you need to edit the security groups to allow ssh and http access from ports 22 and 80 respectively.</p><p>Now you should be ready to ssh into your EC2, you will be able to get the address of your EC2 instance from your account and then use the below command to ssh in (replacing ./daddynappychange.pem to point to your .pem file). If you chose a different EC2 image you will need to replace ubuntu with the relevant user for your image.</p><pre>ssh -i ./daddynappychange.pem <a href="/service/mailto:ubuntu@ec2-35-179-93-3.eu-west-2.compute.amazonaws.com">ubuntu@ec1–23–123–12–3.eu-west-2.compute.amazonaws.com</a></pre><p>If it has all gone smoothly you should now be connected to your EC2 and your command line should read ubuntu@ip-123–12–123–12:-$</p><h3>Installing Docker</h3><p>To install docker onto your Ubuntu EC2 follow the instructions linked here, they are very straightforward and don’t think I can add anything. <a href="https://docs.docker.com/engine/install/ubuntu/">https://docs.docker.com/engine/install/ubuntu/</a></p><h3><strong>Pull the image from Dockerhub</strong></h3><p>Now we have docker install it is time to pull our image file from dockerhub. To do this run the docker login command and follow the prompts asking for your username and password (this step is only necessary if you have your repository set to private. Then pull your image using your username/image.</p><pre>docker login</pre><pre>docker pull paddyjoneill/daddynappychange</pre><h3>Run your Docker container</h3><p>The last step is to run your container on your EC2 instance! This is the same as when we ran it locally but in this case I have some extra commands to run the production gunicorn web server rather than the flask development server. The :5000 tells the server to run on port 5000 and dnc:app will depend on the structure of your app.</p><pre>docker run -p 80:5000 paddyjoneill/daddynappychange gunicorn -b :5000 dnc:app</pre><h3>Test it out</h3><p>You should now be able to go to your EC2 instance in your browser (eg http://ec1–23–123–12–3.eu-west-2.compute.amazonaws.com) and see it working!</p><p>In the next <a href="https://medium.com/@patrickjamesoneill/dockerising-everything-a1da40d51bf1">blog</a> I use Docker and docker-compose to containerise and orchestrate the whole application.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fd075ddd26e8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Asynchronous Task Management with Celery]]></title>
            <link>https://patrickjamesoneill.medium.com/asynchronous-task-management-with-celery-c328d7bc99cb?source=rss-10f568f004af------2</link>
            <guid isPermaLink="false">https://medium.com/p/c328d7bc99cb</guid>
            <dc:creator><![CDATA[Patrick O'Neill]]></dc:creator>
            <pubDate>Mon, 08 Jun 2020 17:09:04 GMT</pubDate>
            <atom:updated>2020-06-08T17:17:25.456Z</atom:updated>
            <content:encoded><![CDATA[<p>Previous blog <a href="https://medium.com/@patrickjamesoneill/object-relational-mapping-89702764f5e2">here</a>.</p><p>After getting SQLAlchemy up and running in my project I then worked on creating the restful routes in the back-end and the fetches and forms to use them in the front-end.</p><p>So the next interesting and new to me piece of the puzzle was asynchronous task management and the reason for using it in my project is that I want to send emails.</p><p>When creating a user account I want to be able to send a welcome email to the new user. The whole process involves database operations to see if the username/email exists already, creating a new user and adding them to the database and <strong>then</strong> sending an email to the new user. Running these functions synchronously (one after the other) would take a long time (relatively, people are impatient) to get back to the user and confirm that they are registered.</p><p>The solution I will use for this problem is to make the email part of these functions asynchronous. Once the user has been created in the database I will then call the email function asynchronously and without having to wait for that to finish return a success message to the front-end that the user has been created.</p><p>I will implement this by using a message queue, this is where messages are sent to a server and stored in a queue until they can be processed and then deleted. Message queue server examples include <a href="https://www.rabbitmq.com/">RabbitMQ</a>, <a href="https://redis.io/">Redis</a> and <a href="https://aws.amazon.com/sqs/">Amazon SQS</a>.</p><p>To use the message queue server I will be using <a href="https://docs.celeryproject.org/en/stable/index.html">Celery</a> which is a Python package for managing this whole process. I’ll break it down into three stages, getting everything in place and setting up celery, sending a task to the message queue and picking up and executing a task from the message queue.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*A6-cIJskwYkBPlQPYripYA.png" /><figcaption>Celery</figcaption></figure><p><strong>Setting it up…</strong></p><p>First from the command line you will need to install the package.</p><pre>pip install celery</pre><p>Then in your Python program you will need to import Celery and then create a celery instance.</p><pre>from celery import Celery<br>...</pre><pre>celery = Celery( app.name, backend=[&#39;REDIS_URL&#39;], broker=[&#39;REDIS_URL&#39;])</pre><p>To get a Redis server up and running <a href="https://medium.com/@petehouston/install-and-config-redis-on-mac-os-x-via-homebrew-eb8df9a4f298">here</a> is an article with instructions or as I am currently hosting on <a href="http://www.heroku.com/">Heroku </a>I used the <a href="https://elements.heroku.com/addons/heroku-redis">Heroku Redis add on</a> and got the server address from there.</p><p>The next thing we need to do it tell Python which functions can be Celery tasks. We do this by using a @celery.task decorator.</p><pre>@celery.task<br>def example_function():<br>    return &quot;test&quot;</pre><p><strong>Sending a task to the MQ…</strong></p><p>The last thing we need to do is when we call the example_function is to use the .delay() method. Any arguments passed into to the delay method will be passed to the example_function.</p><pre>example_function.delay()</pre><p>At this stage we have set up Celery and we should have sent a message to our Redis server but it is currently just sat there waiting and twiddling its thumbs.</p><p><strong>Setting up the Worker…</strong></p><p>We now need something to pick up and crunch the numbers on the tasks we have sent to our message server. We need to create another process that will do that and in Celery that is called a worker. To do this we can use the command below.</p><pre>celery -A app.celery worker --loglevel=info</pre><p>If in another terminal you now run your Python app and call the example_function you should see that in the Celery terminal it picks up and executes the task!</p><p>So I now had the building blocks in place, I was able to go and implement Celery and decorate my function that sends the welcome email to the new user!</p><p>As I was hosting on Heroku I also needed to change my procfile and then turn on the second dyno and run the celery worker as well as the flask application.</p><pre>web: gunicorn app:app<br>worker: celery -A app.celery worker</pre><p>If you’re interested in looking at my code click <a href="https://github.com/paddyjoneill/daddynappychange">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c328d7bc99cb" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>