<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Guilherme Garnier</title>
    <atom:link href="https://blog.guilhermegarnier.com/feed.xml" rel="self" type="application/rss+xml"/>
    <link>https://blog.guilhermegarnier.com/</link>
    <description>My random thoughts on software</description>
    <pubDate>Wed, 10 Jan 2024 21:07:29 +0000</pubDate>
    
    
    
      



      <item>
        <title>Please stop calling the pandemic a &quot;remote work experiment&quot; | Guilherme Garnier</title>
        
        <image>
          <url>https://blog.guilhermegarnier.com/images/hannah-wei-aso6SYJZGps-unsplash-resized.jpg</url>
          <title>Please stop calling the pandemic a "remote work experiment"</title>
          <link>https://blog.guilhermegarnier.com/2021/04/please-stop-calling-the-pandemic-a-remote-experiment/</link>
        </image>
        
        <link>https://blog.guilhermegarnier.com/2021/04/please-stop-calling-the-pandemic-a-remote-experiment/</link>
        <guid isPermaLink="true">https://blog.guilhermegarnier.com/2021/04/please-stop-calling-the-pandemic-a-remote-experiment/</guid>
        <description>&lt;figure style=&quot;text-align: center&quot;&gt;
  &lt;img src=&quot;/images/hannah-wei-aso6SYJZGps-unsplash-resized.jpg&quot; alt=&quot;Silhouette of a person sitting in front of a laptop&quot; style=&quot;width: 720px&quot; /&gt;
  &lt;figcaption&gt;Photo by &lt;a href=&quot;https://unsplash.com/@herlifeinpixels?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText&quot;&gt;Hannah Wei&lt;/a&gt; on &lt;a href=&quot;https://unsplash.com/?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText&quot;&gt;Unsplash&lt;/a&gt;&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Since the Covid 19 pandemic started, we’ve seen the whole world start “accepting” remote work - I put accepting in quotes because most companies don’t really embrace it, but don’t have an alternative. With that, I’ve also seen some studies analyzing the results of this “experiment” - stuff like “people who started working remotely in 2020 are x% more/less productive”. Please don’t do that.&lt;/p&gt;

&lt;p&gt;Experiments should, by definition, be prepared to run in a controlled environment. Bringing to the software development world, it’s like using a containerized environment: an ephemeral, isolated process, running in its own namespaces. This avoids interferences from outside - like dependencies with different software versions and port conflicts.&lt;/p&gt;

&lt;p&gt;We’re living a pandemic. This is the complete opposite of an experiment. If there’s one thing we don’t have any more is control. We don’t control where we can go, what we can do, and we have no idea when this is going to end. &lt;a href=&quot;https://www.wired.com/story/mental-health-coronavirus-pandemic-tips/&quot;&gt;And many of us are suffering with mental health issues&lt;/a&gt;. That means we can’t compare the productivity of remote workers now with office workers before.&lt;/p&gt;

&lt;p&gt;Most people worked physically in their companies offices, close to their co-workers, easily separating work and personal life: you leave home in the morning and start your work; you get back home at the end of the day and you’re back to your personal life. And most of these people work for companies that weren’t prepared for remote work - that means much more than simply closing the offices and sending employers home. All the work experience must change - communication, interactions, meetings, feedbacks. I’m not saying it’s &lt;em&gt;better&lt;/em&gt; or &lt;em&gt;worse&lt;/em&gt;, it’s just different. So &lt;a href=&quot;https://m.signalvnoise.com/remote-work-is-a-platform/&quot;&gt;if you try to reproduce the office environment remotely&lt;/a&gt;, you’ll probably fail.&lt;/p&gt;

&lt;p&gt;My conclusion is that trying to compare productivity from office workers before the pandemic with remote workers during it doesn’t make sense, for two main reasons.&lt;/p&gt;

&lt;p&gt;The first one is that an experiment should have a preparation step. If a company decides to start a remote work experiment, it should study how remote companies work, and how they should change their dynamics. And we didn’t have time for this when Covid 19 pandemic started. Some of these people may not have a decent work environment at home; maybe, they live with noisy relatives; maybe they have kids, and now have to handle remote teaching for their kids and adapt them too, changing their lives completely. And besides all that, they still have to be productive at work!&lt;/p&gt;

&lt;p&gt;The second reason is that, anywhere you work, this pandemic is messing with everyone’s lives. People are worried, anxious, depressed. People lost their relatives and friends, and may be worried with what could happen to them. And nobody could ever be fully productive with this situation. If you’re suffering with any of these, as most of us are, you should look for professional help, and not blame remote work.&lt;/p&gt;

&lt;p&gt;All this shows that, to focus on work, people need to handle their mental health, and it’s hard. &lt;a href=&quot;https://edition.cnn.com/2021/04/02/success/linkedin-paid-week-off/index.html&quot;&gt;Some&lt;/a&gt; &lt;a href=&quot;https://www.linkedin.com/pulse/icare-como-o-ifood-tem-cuidado-das-pessoas-neste-um-ano-lucas-lopes/&quot;&gt;companies&lt;/a&gt; have realized this, and are taking good care of their employees - including &lt;a href=&quot;https://www.jusbrasil.com.br&quot;&gt;Jusbrasil&lt;/a&gt;, where I work. But others are more worried about losing productivity when working remotely. Please don’t be one of the latter.&lt;/p&gt;
</description>
        <pubDate>Tue, 13 Apr 2021 00:00:00 +0000</pubDate>
      </item>
    
      



      <item>
        <title>How to check open ports by process | Guilherme Garnier</title>
        
        <image>
          <url>https://blog.guilhermegarnier.com/images/doors.png</url>
          <title>How to check open ports by process</title>
          <link>https://blog.guilhermegarnier.com/2019/10/how-to-check-open-ports-by-process/</link>
        </image>
        
        <link>https://blog.guilhermegarnier.com/2019/10/how-to-check-open-ports-by-process/</link>
        <guid isPermaLink="true">https://blog.guilhermegarnier.com/2019/10/how-to-check-open-ports-by-process/</guid>
        <description>&lt;p&gt;&lt;img src=&quot;/images/doors.png&quot; alt=&quot;Hallway with doors&quot; /&gt;&lt;/p&gt;

&lt;p&gt;When debugging a server problem, you may need to know which ports a specific process opened - or which process is listening on a specific port. There are many ways to do this, using different tools. It may be useful to know more than one, because you may not have every tool available (specially in containerized environments).&lt;/p&gt;

&lt;h2 id=&quot;checking-if-a-port-is-open&quot;&gt;Checking if a port is open&lt;/h2&gt;

&lt;p&gt;The first step is checking if a port is open. The easiest way to do this is using &lt;a href=&quot;http://netcat.sourceforge.net/&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;netcat&lt;/code&gt;&lt;/a&gt;. In this example, there’s an HTTP server running on port 8000:&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;nc &lt;span class=&quot;nt&quot;&gt;-zv&lt;/span&gt; localhost 8000
Connection to localhost 8000 port &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;tcp/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; succeeded!

&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;nc &lt;span class=&quot;nt&quot;&gt;-zv&lt;/span&gt; localhost 8001
nc: connect to localhost port 8001 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;tcp&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; failed: Connection refused
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You can also add a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-u&lt;/code&gt; flag to check UDP ports.&lt;/p&gt;

&lt;p&gt;Another tool you can use to check for open ports is &lt;a href=&quot;https://en.wikipedia.org/wiki/Telnet&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;telnet&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;telnet localhost 8000
Trying 127.0.0.1...
Connected to localhost.
Escape character is &lt;span class=&quot;s1&quot;&gt;'^]'&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt;
^]
telnet&amp;gt; quit
Connection closed.

&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;telnet localhost 8001
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Telnet can’t be used to test UDP ports.&lt;/p&gt;

&lt;h3 id=&quot;testing-http-ports&quot;&gt;Testing HTTP ports&lt;/h3&gt;

&lt;p&gt;In the specific case of an HTTP server, you can use &lt;a href=&quot;https://curl.haxx.se&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;curl&lt;/code&gt;&lt;/a&gt; to make requests - &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-v&lt;/code&gt; flags makes the output verbose:&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;curl &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt; localhost:8000/check
&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;   Trying 127.0.0.1:8000...
&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; TCP_NODELAY &lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; Connected to localhost &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;127.0.0.1&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; port 8000 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;c&quot;&gt;#0)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; GET /check HTTP/1.1
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; Host: localhost:8000
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; User-Agent: curl/7.65.3
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; Accept: &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; Mark bundle as not supporting multiuse
&amp;lt; HTTP/1.1 200 OK
&amp;lt; Content-Type: text/html&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;charset&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;utf-8
&amp;lt; Content-Length: 2
&amp;lt; ETag: W/&lt;span class=&quot;s2&quot;&gt;&quot;2-4KoCHiHd29bYzs7HHpz1ZA&quot;&lt;/span&gt;
&amp;lt; Date: Fri, 13 Sep 2019 21:11:22 GMT
&amp;lt; Connection: keep-alive
&amp;lt;
&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; Connection &lt;span class=&quot;c&quot;&gt;#0 to host localhost left intact&lt;/span&gt;
OK
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;When the port isn’t open, you get a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Connection refused&lt;/code&gt; message:&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;curl &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt; localhost:8001/check
&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;   Trying 127.0.0.1:8001...
&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; TCP_NODELAY &lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; connect to 127.0.0.1 port 8001 failed: Connection refused
&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; Failed to connect to localhost port 8001: Connection refused
&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; Closing connection 0
curl: &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;7&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; Failed to connect to localhost port 8001: Connection refused
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You can also make this same request with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;telnet&lt;/code&gt;, if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;curl&lt;/code&gt; is unavailable:&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;telnet localhost 8000
Trying 127.0.0.1...
Connected to localhost.
Escape character is &lt;span class=&quot;s1&quot;&gt;'^]'&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt;
GET /check

HTTP/1.1 200 OK
Content-Type: text/html&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;charset&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;utf-8
Content-Length: 2
ETag: W/&lt;span class=&quot;s2&quot;&gt;&quot;2-4KoCHiHd29bYzs7HHpz1ZA&quot;&lt;/span&gt;
Date: Fri, 13 Sep 2019 21:16:53 GMT
Connection: close

OKConnection closed by foreign host.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;checking-open-ports-by-process&quot;&gt;Checking open ports by process&lt;/h2&gt;

&lt;p&gt;Sometimes you need to check which ports a specific process is listening to. First you need to find out the process id (pid) using &lt;a href=&quot;https://en.wikipedia.org/wiki/Ps_(Unix)&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ps&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;ps aux | &lt;span class=&quot;nb&quot;&gt;grep &lt;/span&gt;python
ggarnier  8762  0.6  0.0  94900 14928 pts/2    S+   17:31   0:00 python server.py
ggarnier  8789  0.0  0.0  21536  1092 pts/11   S+   17:31   0:00 &lt;span class=&quot;nb&quot;&gt;grep &lt;/span&gt;python
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The process id is the second column - in this example it’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;8762&lt;/code&gt;. Now you can use &lt;a href=&quot;https://en.wikipedia.org/wiki/Lsof&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lsof&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;lsof &lt;span class=&quot;nt&quot;&gt;-Pan&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; 8762 &lt;span class=&quot;nt&quot;&gt;-i&lt;/span&gt;
COMMAND  PID     USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
python  8762 ggarnier    3u  IPv4 13254927      0t0  TCP &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;:8000 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;LISTEN&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In the example above, process &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;8762&lt;/code&gt; is listening on port TCP &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;8000&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;An alternative command is &lt;a href=&quot;http://man7.org/linux/man-pages/man8/ss.8.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ss&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;ss &lt;span class=&quot;nt&quot;&gt;-tulpn&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;grep&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;pid=8762&quot;&lt;/span&gt;
tcp    LISTEN   0        5                                      0.0.0.0:8000                  0.0.0.0:&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;            &lt;span class=&quot;nb&quot;&gt;users&lt;/span&gt;:&lt;span class=&quot;o&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;python&quot;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;8762,fd&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;3&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You may need to run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lsof&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ss&lt;/code&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sudo&lt;/code&gt; if the process is owned by another user.&lt;/p&gt;

&lt;h2 id=&quot;checking-which-process-is-listening-on-a-port&quot;&gt;Checking which process is listening on a port&lt;/h2&gt;

&lt;p&gt;Finally, if a port is open and you don’t know with process did it, use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lsof&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;lsof &lt;span class=&quot;nt&quot;&gt;-i&lt;/span&gt; :8000
COMMAND  PID     USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
python  8762 ggarnier    3u  IPv4 13254927      0t0  TCP &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;:8000 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;LISTEN&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Netstat&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;netstat&lt;/code&gt;&lt;/a&gt; can also be used. It doesn’t support filtering by port or process, but you can use &lt;a href=&quot;https://en.wikipedia.org/wiki/Grep&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;grep&lt;/code&gt;&lt;/a&gt; to filter its output:&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;netstat &lt;span class=&quot;nt&quot;&gt;-ntlp&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;grep &lt;/span&gt;8000
tcp        0      0 0.0.0.0:8000            0.0.0.0:&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;               LISTEN      8762/python
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In both commands, use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sudo&lt;/code&gt; if you aren’t the process owner.&lt;/p&gt;
</description>
        <pubDate>Tue, 29 Oct 2019 00:00:00 +0000</pubDate>
      </item>
    
      



      <item>
        <title>Build a print server with a Raspberry Pi | Guilherme Garnier</title>
        
        <image>
          <url>https://blog.guilhermegarnier.com/images/pi-printer.png</url>
          <title>Build a print server with a Raspberry Pi</title>
          <link>https://blog.guilhermegarnier.com/2019/02/building-a-printing-server-with-a-raspberry-pi/</link>
        </image>
        
        <link>https://blog.guilhermegarnier.com/2019/02/building-a-printing-server-with-a-raspberry-pi/</link>
        <guid isPermaLink="true">https://blog.guilhermegarnier.com/2019/02/building-a-printing-server-with-a-raspberry-pi/</guid>
        <description>&lt;p&gt;&lt;img src=&quot;/images/pi-printer.png&quot; alt=&quot;Raspberry Pi printer server&quot; /&gt;&lt;/p&gt;

&lt;p&gt;A few months ago, my wife needed to print a couple of documents. I had an old HP printer (Officejet 4355) sitting around, but I couldn’t use it in her laptop - it’s a &lt;a href=&quot;https://www.google.com/chromebook/&quot;&gt;Chromebook&lt;/a&gt;, which doesn’t support printer drivers. It can only print over the network, and my printer doesn’t have network connection, just USB. The only way I could make that work was installing the printer in another computer and making it share the printer over the network. But I’d have to keep the other computer on and connected to the printer every time she wanted to print something, which isn’t convenient.&lt;/p&gt;

&lt;p&gt;So I decided to remove the dust from an old &lt;a href=&quot;https://www.raspberrypi.org/&quot;&gt;Raspberry Pi&lt;/a&gt; (the original model B) I hadn’t used for a while, and use it as a print server! Now I can print from any device, including her Chromebook and my Android phone.&lt;/p&gt;

&lt;h2 id=&quot;installation&quot;&gt;Installation&lt;/h2&gt;

&lt;p&gt;First install &lt;a href=&quot;https://www.raspbian.org/&quot;&gt;Raspbian&lt;/a&gt; (without graphical interface, as you’ll use it only as a server), and set a static IP for it. Then connect the printer to one of its USB ports and install the driver - for my case, I found &lt;a href=&quot;https://developers.hp.com/hp-linux-imaging-and-printing&quot;&gt;HP’s Linux Imaging and Printing software (HPLIP)&lt;/a&gt;. It’s an official HP driver for Linux that supports many old printers, including mine. To install it:&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-get &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;hplip
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The next step is installing &lt;a href=&quot;https://www.cups.org/&quot;&gt;CUPS&lt;/a&gt; and adding your user (default user is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pi&lt;/code&gt;) to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lpadmin&lt;/code&gt; group:&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-get &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;cups
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;usermod &lt;span class=&quot;nt&quot;&gt;-a&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-G&lt;/span&gt; lpadmin pi
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To add a printer to CUPS, open its web interface in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;http://&amp;lt;pi IP address&amp;gt;:631/admin&lt;/code&gt; and click &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Add Printer&lt;/code&gt;. Your printer should be discovered automatically. After selecting it, you can add a meaningful name to it. Also, don’t forget to check &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Share This Printer&lt;/code&gt; before continuing.&lt;/p&gt;

&lt;p&gt;With the print server fully configured, you need to setup the printer in each device. In a laptop running Ubuntu, my “new” printer could be automatically discovered. If you have any problem with it, check &lt;a href=&quot;https://wiki.ubuntu.com/DebuggingPrintingProblems&quot;&gt;Debugging Printing Problems&lt;/a&gt;. In Chromebook, I installed &lt;a href=&quot;https://chrome.google.com/webstore/detail/ipp-cups-printing-for-chr/lkhfeoafdgbaecajkdbioenncjopbpmk?hl=en-GB&quot;&gt;IPP/CUPS printing&lt;/a&gt; extension. And in Android, I’m using &lt;a href=&quot;https://play.google.com/store/apps/details?id=io.github.benoitduffez.cupsprint&quot;&gt;CUPS printing&lt;/a&gt; app. They all work great!&lt;/p&gt;
</description>
        <pubDate>Thu, 07 Feb 2019 00:00:00 +0000</pubDate>
      </item>
    
      



      <item>
        <title>The anatomy of a code review | Guilherme Garnier</title>
        
        <image>
          <url>https://blog.guilhermegarnier.com/images/ant-anatomy.png</url>
          <title>The anatomy of a code review</title>
          <link>https://blog.guilhermegarnier.com/2018/05/the-anatomy-of-a-code-review/</link>
        </image>
        
        <link>https://blog.guilhermegarnier.com/2018/05/the-anatomy-of-a-code-review/</link>
        <guid isPermaLink="true">https://blog.guilhermegarnier.com/2018/05/the-anatomy-of-a-code-review/</guid>
        <description>&lt;p&gt;Code review is an amazing tool to improve code quality. It brings many benefits, to the reviewer, to the reviewee and to the team.&lt;/p&gt;

&lt;h2 id=&quot;how-we-do-code-reviews&quot;&gt;How we do code reviews&lt;/h2&gt;

&lt;p&gt;To add code reviews to your team workflow, first you need to use a version control tool. If you don’t, start with that.&lt;/p&gt;

&lt;p&gt;Basically, the idea is never committing directly to the master branch - or whatever other branch you choose as the main one, but usually it’s the master branch. To start any changes to the source code, be it a new feature, refactorings or bugfixes, you need to create a new branch. You can also create conventions for the branch names, but this is not required.&lt;/p&gt;

&lt;p&gt;After you finish working on your own branch, you can trigger the code review process. If you use a repository manager like &lt;a href=&quot;https://github.com/&quot;&gt;GitHub&lt;/a&gt; or &lt;a href=&quot;https://about.gitlab.com/&quot;&gt;GitLab&lt;/a&gt;, they already provide tools to help on this process: GitHub calls them pull requests, and GitLab, merge requests - to make it simpler, I’ll call them change requests from now on. You can add some comments explaining the purpose of the proposed changes, and maybe links to other related change requests or issues. These are very important to make the objective of that change request clear for everyone that might review your code. When in doubt, be as specific and clear as you can. Here’s an example from &lt;a href=&quot;https://github.com/tsuru/tsuru&quot;&gt;tsuru&lt;/a&gt;, the project I currently work in: &lt;a href=&quot;https://github.com/tsuru/tsuru/pull/1889&quot;&gt;a pull request with a detailed description&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/tsuru/tsuru/pull/1889&quot; target=&quot;_blank&quot;&gt;
  &lt;img src=&quot;/images/tsuru-pr-example1.png&quot; alt=&quot;A pull request with a detailed description&quot; style=&quot;width: 700px&quot; /&gt;
&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As soon as a change request is created, it’s available for any other member of the team to start reviewing it. You can also assign it to a specific person, if you think his/her opinion is important - maybe when you change a specific and obscure part of the project that not everyone dominates.&lt;/p&gt;

&lt;p&gt;As a reviewer, you job is to check many aspects of the code. Here’s a non exaustive reference list:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;accuracy and completeness:&lt;/strong&gt; does the code do what it’s supposed to do?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;bug free:&lt;/strong&gt; doesn’t it introduce new bugs (like not handling corner cases)?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;expressiveness:&lt;/strong&gt; is it clear on its intentions? Can a reader clearly understand the coder’s objectives?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;scalability:&lt;/strong&gt; could it handle the expected load?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;conventions and standards:&lt;/strong&gt; does it follow the agreed conventions for code style, file and directory structure, etc?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;tests:&lt;/strong&gt; do the tests cover all the use cases, including important corner cases? Is there a specific use case that deserves more tests?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After checking the code, the reviewer can basically accept or reject it - GitHub also allows you to add review comments without explicitly approving or rejecting. When you approve, that means you think those changes could be promptly merged to the main branch. But when you reject, that could mean you doesn’t agree with that change at all (e.g. you don’t think this software should have the proposed feature), or that you’re requesting some changes to the code. That’s what review comments are meant for.&lt;/p&gt;

&lt;p&gt;Usually a review comment can be added to a specific line or code block. That could be a typo, a function you think should be named differently, a bug you spotted or a missing test or documentation. The comments should make this clear to the reviewee, who can reply to the comments or make requested changes. Then, this process repeats until the change request gets approved.&lt;/p&gt;

&lt;p&gt;When the reviewer accepts the proposed changes, the reviewee has two options: merge/rebase the changes to the main branch and finish the code review process, or ask for another developer review. This is the case when the change is very complex and you’re insecure you may be missing something. When this happens, the review process doesn’t finish until all reviewers accept the changes.&lt;/p&gt;

&lt;p&gt;To illustrate, here’s another example of code review from &lt;a href=&quot;https://github.com/tsuru/tsuru&quot;&gt;tsuru&lt;/a&gt;: in &lt;a href=&quot;https://github.com/tsuru/tsuru/pull/1881&quot;&gt;this pull request&lt;/a&gt;, I received a couple of comments with request changes. For most of them I changed the code according the suggestions, and &lt;a href=&quot;https://github.com/tsuru/tsuru/pull/1881#pullrequestreview-82810505&quot;&gt;one of them&lt;/a&gt; generated a small discussion. What’s best is that the discussion is documented in the pull request, for anyone to read and participate.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/tsuru/tsuru/pull/1881#pullrequestreview-82810505&quot; target=&quot;_blank&quot;&gt;
  &lt;img src=&quot;/images/tsuru-pr-example2.png&quot; alt=&quot;A pull request discussion&quot; style=&quot;width: 700px&quot; /&gt;
&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;benefits-of-the-code-review&quot;&gt;Benefits of the code review&lt;/h2&gt;

&lt;p&gt;A code review process brings a lot of benefits to the reviewer, to the reviewee and to the team as a whole:&lt;/p&gt;

&lt;h3 id=&quot;for-the-reviewer&quot;&gt;For the reviewer&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;learn about the changes:&lt;/strong&gt; if someone commits directly on the main branch, only the author would know the details about it. When you review, you also get to know what those changes do, how and why they were written. And if you don’t understand, you have the opportunity to ask for more information&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;learn about the project:&lt;/strong&gt; if you are new in the team, code review is a great tool to obtain knowledge about the project. Even if you still don’t understand the project, go ahead and review everyone’s code, just to start understanding it a little better&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;learn about the technologies involved (languages, frameworks, libraries):&lt;/strong&gt; just like when you’re new to the team, if you are getting started with the tech stack used in the project, you’ll benefit even more from reviewing people’s code. You’ll learn about the language’s features, new libraries to solve common problems and features of the frameworks the project uses&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;develop a critical view of someone else’s code:&lt;/strong&gt; if you are an inexperienced developer, you probably have a hard time trying to spot problems in someone else’s code. After code reviewing a couple of times (and checking other developer’s reviews), you’ll learn how to be more critic. And what’s more important, you’ll learn that criticizing someone else’s code is not offensive, it’s actually a good thing, an opportunity for them to learn. And you’ll learn with the other’s mistakes. &lt;a href=&quot;https://pagefault.blog/2018/04/08/why-junior-devs-should-review-seniors-commits/&quot;&gt;That’s why junior devs should review seniors’ commits&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;for-the-reviewee&quot;&gt;For the reviewee&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;learn about the project:&lt;/strong&gt; just like the reviewer, a great way to start in a new project is asking for feedback from the more experienced in the team. Here’s &lt;a href=&quot;https://github.com/tsuru/tsuru/pull/1973&quot;&gt;another example&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;learn about the technologies involved (languages, frameworks, libraries):&lt;/strong&gt; the same comments from the reviewer part are worth here. Here’s &lt;a href=&quot;https://github.com/tsuru/tsuru/pull/1969#discussion_r175858044&quot;&gt;one more example&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;learn other ways to solve problems:&lt;/strong&gt; when you face a bug, you may already have the complete solution in your head; that solution could really work after you implement it. But that doesn’t mean this is the only way to solve the problem. Probably there are other solutions out there, and they may be simpler, clearer, safer or perform better. Other developers could show these other solutions in their reviews&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;learn to accept critics:&lt;/strong&gt; sometimes we get so attached to the code we wrote that we could get offended when someone criticizes it. Code reviews help us learn to get over this, because we are explicitly asking for people to give feedback on our code, and they’ll answer. Over time you’ll learn these feedbacks are great ways of learning&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;for-the-team&quot;&gt;For the team&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;shared code ownership:&lt;/strong&gt; now two or more people are responsible for the code one of them wrote - by the way, that could also be reached with pair programming. Shared code ownership is great after a bug is released into production: the developer who wrote the code is no longer the only responsible for that. This also helps reaching a &lt;a href=&quot;https://medium.com/zendesk-engineering/blameless-culture-21662ab9118c&quot;&gt;blameless culture&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;helps keeping standards:&lt;/strong&gt; if the team agreed on specific conventions, like code style rules, tests for every line of code or document all the APIs, code review is a great way to make people monitor each other on these&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;common-problems-and-concerns&quot;&gt;Common problems and concerns&lt;/h2&gt;

&lt;h3 id=&quot;wont-this-slow-me-down&quot;&gt;Won’t this slow me down?&lt;/h3&gt;

&lt;p&gt;When we suggest that our team starts working with code reviews, people usually get apprehensive about this process making the team slower. If they’re used to finish their work, push the code and deploy to production, they may have this concern.&lt;/p&gt;

&lt;p&gt;In fact, you won’t have that fast dev-to-production cycle. But that is a small downside against the many benefits presented above. It’s a trade-off, like every decision you make in your project.&lt;/p&gt;

&lt;p&gt;One common concern is having to wait for a long time for another dev to start the code review process. If people still don’t see value in this process, they indeed may not prioritize this. But as soon as you get to be on the other side - waiting for other people’s reviews -, you’ll start giving it more attention. It’s an organic process that usually regulates itself.&lt;/p&gt;

&lt;h3 id=&quot;how-to-handle-large-change-requests&quot;&gt;How to handle large change requests?&lt;/h3&gt;

&lt;p&gt;Another problem is when the change request is very large. This will require more time from the reviewers, and it may be harder to analyze: you may lose focus during the review.&lt;/p&gt;

&lt;p&gt;Just like on the previous item, this should be self regulated. If you start making very large change requests, you’ll learn they aren’t much productive when someone else asks for you to review a large change request.&lt;/p&gt;

&lt;p&gt;There isn’t an ideal size of change request. But you should make them as small as possible. If you’re working on a complex feature, you may try splitting it in a couple of small change requests, independent from each other. But if you can’t make them independent, an alternative is starting with a branch forked from the main one - you may call it &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;feature-something&lt;/code&gt;, then creating other branches from it. As you finish each part, make the change requests to merge to you feature branch, not the main branch. And only when you finish every “sub-feature” change request, only then you merge your feature branch to the main one.&lt;/p&gt;

&lt;h3 id=&quot;when-not-to-use-code-reviews&quot;&gt;When not to use code reviews?&lt;/h3&gt;

&lt;p&gt;Finally, another common question is: are there exceptions to the “never commit to the main branch” rule? If the change is very simple and small, won’t the code review process be just a formality?&lt;/p&gt;

&lt;p&gt;In fact, there are a couple of situations where you may bypass the code review process. One example is updating a dependency version. But I still think it’s worth opening the change request, to make other team members aware of the changes. But this kind of decision is up to the team, and should be accorded among them.&lt;/p&gt;

&lt;p&gt;To finish, I suggest reading this great post about &lt;a href=&quot;https://insidedigitalocean.com/how-to-conduct-effective-code-reviews-18b823a07123&quot;&gt;how to conduct effective code reviews&lt;/a&gt;.&lt;/p&gt;
</description>
        <pubDate>Tue, 08 May 2018 00:00:00 +0000</pubDate>
      </item>
    
      



      <item>
        <title>Increasing productivity in tmux with a single prefix key | Guilherme Garnier</title>
        
        <image>
          <url>https://blog.guilhermegarnier.com/images/keyboard.jpg</url>
          <title>Increasing productivity in tmux with a single prefix key</title>
          <link>https://blog.guilhermegarnier.com/2017/12/increasing-productivity-in-tmux-with-a-single-prefix-key/</link>
        </image>
        
        <link>https://blog.guilhermegarnier.com/2017/12/increasing-productivity-in-tmux-with-a-single-prefix-key/</link>
        <guid isPermaLink="true">https://blog.guilhermegarnier.com/2017/12/increasing-productivity-in-tmux-with-a-single-prefix-key/</guid>
        <description>&lt;p&gt;&lt;a href=&quot;https://github.com/tmux/tmux&quot;&gt;Tmux&lt;/a&gt; is a fantastic tool for improving productivity when working with a terminal. One of the first things people configure when start using tmux is changing the prefix key. The default value is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;control+b&lt;/code&gt;, which is not very confortable to press with a single hand. And as you’ll end up pressing it a lot, for every tmux command, the most common used configuration is changing it to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;control+a&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is much better, but you still need to press two keys simultaneously before typing any tmux command. After using this configuration for some time, I decided to change it to a single key, to make it even easier.&lt;/p&gt;

&lt;p&gt;I though about changing the prefix to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;caps lock&lt;/code&gt;. Besides being rarely used, it’s very well positioned. However, you can’t set &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;caps lock&lt;/code&gt; as prefix in tmux. An alternative solution is mapping the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;caps lock&lt;/code&gt; key to something else. In OSX, you can set it to another modifier key, like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;control&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;shift&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esc&lt;/code&gt;: go to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;System Preferences&lt;/code&gt; =&amp;gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Keyboard&lt;/code&gt; =&amp;gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Modifier keys&lt;/code&gt;. First I tried mapping it to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esc&lt;/code&gt;, and setting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esc&lt;/code&gt; as tmux prefix. It works, but this setup brought another problem: as a vim user, I use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esc&lt;/code&gt; key a lot (to alternate between vim modes), so now I had to type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esc&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;caps lock&lt;/code&gt; twice to send the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esc&lt;/code&gt; key to vim. It was ok, but not ideal.&lt;/p&gt;

&lt;p&gt;Then I tried another solution: I installed &lt;a href=&quot;https://github.com/tekezo/Karabiner-Elements&quot;&gt;Karabiner-Elements&lt;/a&gt;, a Mac app which allows you to completely customize your keyboard. So I mapped the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;caps lock&lt;/code&gt; key to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Home&lt;/code&gt; (which doesn’t exist in Mac keyboard), and changed tmux prefix key to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Home&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-g&lt;/span&gt; prefix Home
unbind C-b
bind-key Home send-prefix
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now I have a great configuration: I use a single key (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;caps lock&lt;/code&gt;) as prefix, and without losing any key functionality.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;UPDATE:&lt;/strong&gt; to do this same configuration in Linux, you just need to open the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/share/X11/xkb/symbols/pc&lt;/code&gt; file and change the line that starts with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;key &amp;lt;CAPS&amp;gt;&lt;/code&gt; to this:&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;key &amp;lt;CAPS&amp;gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;        &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; Home          &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;       &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</description>
        <pubDate>Tue, 05 Dec 2017 00:00:00 +0000</pubDate>
      </item>
    
      



      <item>
        <title>Cheap and reliable archiving with Amazon Glacier | Guilherme Garnier</title>
        
        <image>
          <url>https://blog.guilhermegarnier.com/images/glacier.jpg</url>
          <title>Cheap and reliable archiving with Amazon Glacier</title>
          <link>https://blog.guilhermegarnier.com/2017/07/cheap-and-reliable-archiving-with-amazon-glacier/</link>
        </image>
        
        <link>https://blog.guilhermegarnier.com/2017/07/cheap-and-reliable-archiving-with-amazon-glacier/</link>
        <guid isPermaLink="true">https://blog.guilhermegarnier.com/2017/07/cheap-and-reliable-archiving-with-amazon-glacier/</guid>
        <description>&lt;p&gt;Incidents like &lt;a href=&quot;https://en.wikipedia.org/wiki/WannaCry_ransomware_attack&quot;&gt;WannaCry ransomware&lt;/a&gt; expose the importance of doing backups, which is usually forgotten by many people.&lt;/p&gt;

&lt;p&gt;When someone talks about backing up our personal files, we usually think about services like &lt;a href=&quot;https://www.dropbox.com&quot;&gt;Dropbox&lt;/a&gt; and &lt;a href=&quot;https://drive.google.com&quot;&gt;Google Drive&lt;/a&gt;. But they have a reasonable cost if you have more than a couple of GB of data. There are a lot of other solutions available. Most of them are cheaper, but not always reliable - imagine if you backup all your personal data to a small and unknown service, and a few months later, the company breaks. Or a security flaw exposes all your personal data! Of course this could also happen with Dropbox and Google Drive, but it’s much less likely, being two large and serious companies.&lt;/p&gt;

&lt;p&gt;One alternative to them is &lt;a href=&quot;https://aws.amazon.com/glacier/&quot;&gt;Amazon Glacier&lt;/a&gt;. It’s a not so popular Amazon service for data archiving. You should notice it works differently from the usual backup solutions. When you sign up to Dropbox, for instance, you can install an app to your computer or mobile phone, or use the web interface to instantly access your files and upload new ones. Glacier is much more low level. It doesn’t have a web interface, app or even command line tool! There’s only an API, which you use to check your files, download or upload.&lt;/p&gt;

&lt;p&gt;And there’s more: the download and upload rates are very slow. And to download a file, you first have to ask for a file retrieval job; the download will be available in a couple of hours!!!&lt;/p&gt;

&lt;p&gt;This seems like a terrible service, so why use it? Because it’s very, very cheap! You only pay US$ 0.004 per GB per month for storage, besides &lt;a href=&quot;https://aws.amazon.com/glacier/pricing/&quot;&gt;additional costs&lt;/a&gt; for requests. And even being slow and hard to use, it’s a service offered by Amazon, which gives you confidence it won’t suddenly disappear.&lt;/p&gt;

&lt;p&gt;Having said that, Glacier isn’t a service to keep data you may need immediately. But it’s ideal for something you probably won’t need to access anytime soon. Think about your family pictures: when you want to access them, you probably doesn’t need them right away; you’re fine waiting a couple of hours for that.&lt;/p&gt;

&lt;p&gt;Glacier is also a great option for “backups of backups”. If you want to be neurotic about backups (and you should!), you can archive a copy of your backups there.&lt;/p&gt;

&lt;h2 id=&quot;usage&quot;&gt;Usage&lt;/h2&gt;

&lt;p&gt;The easiest way to use Glacier is with a third party client. I like &lt;a href=&quot;https://github.com/uskudnik/amazon-glacier-cmd-interface&quot;&gt;amazon-glacier-cmd-interface&lt;/a&gt;. After setting up the basic configuration, you can create a vault and upload you files:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ glacier-cmd mkvault my-disaster-backup
$ glacier-cmd upload my-disaster-backup my-file1 my-file2 ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To list archives in a vault:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ glacier-cmd inventory &amp;lt;vaultname&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The inventory retrieval job takes a couple of hours to be processed. You can check its status with:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ glacier-cmd listjobs &amp;lt;vaultname&amp;gt;
+------------------------------------------------------+---------------+--------------+--------------------+--------------------------+------------+
|                      VaultARN                        |    Job ID     | Archive ID   |       Action       |        Initiated         |   Status   |
+------------------------------------------------------+---------------+--------------+--------------------+--------------------------+------------+
| arn:aws:glacier:us-west-2:483413266890:vaults/backup | QYqdvM4k8q... |    None      | InventoryRetrieval | 2017-07-24T15:47:48.310Z | InProgress |
+------------------------------------------------------+---------------+--------------+--------------------+--------------------------+------------+
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;When the job status change to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Succeeded&lt;/code&gt;, run the inventory command again to check the archive list.&lt;/p&gt;

&lt;p&gt;To download an archive, first you need to check its id in the inventory:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ glacier-cmd inventory &amp;lt;vaultname&amp;gt;
Inventory of vault: arn:aws:glacier:us-west-2:483413266890:vaults/backup
Inventory Date: 2017-07-05T11:22:15Z

Content:
+---------------+---------------------+----------------------+------------------+------------+
|  Archive ID   | Archive Description |       Uploaded       | SHA256 tree hash |    Size    |
+---------------+---------------------+----------------------+------------------+------------+
| uFg2FE_guu... | file1.tar.gz        | 2017-03-31T14:29:17Z | b41922e1a2...    | 1342622251 |
| 43Wjk63Dcu... | file2.tar.gz        | 2017-03-31T17:18:28Z | 2346170d22...    | 2347810677 |
+---------------+---------------------+----------------------+------------------+------------+
This vault contains 2 items, total size 2.5 GB.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then, create an archive retrieval job using the archive id:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ glacier-cmd getarchive &amp;lt;vaultname&amp;gt; &amp;lt;archive id&amp;gt;
+-----------+---------------+
|   Header  |    Value      |
+-----------+---------------+
|   JobId   | Xa17IAadQG... |
| RequestId | cPcomv_vTf... |
+-----------+---------------+
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;When the download is available (you can check its status with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;glacier-cmd listjobs &amp;lt;vaultname&amp;gt;&lt;/code&gt;), download it with:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ glacier-cmd download &amp;lt;vaultname&amp;gt; &amp;lt;archive id&amp;gt; --outfile &amp;lt;filename&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</description>
        <pubDate>Wed, 12 Jul 2017 00:00:00 +0000</pubDate>
      </item>
    
      



      <item>
        <title>SporTV Play front-end architecture for the Olympic Games | Guilherme Garnier</title>
        
        <image>
          <url>https://blog.guilhermegarnier.com/images/bff.png</url>
          <title>SporTV Play front-end architecture for the Olympic Games</title>
          <link>https://blog.guilhermegarnier.com/2017/04/sportv_play_front_end_architecture_for_the_olympic_games/</link>
        </image>
        
        <link>https://blog.guilhermegarnier.com/2017/04/sportv_play_front_end_architecture_for_the_olympic_games/</link>
        <guid isPermaLink="true">https://blog.guilhermegarnier.com/2017/04/sportv_play_front_end_architecture_for_the_olympic_games/</guid>
        <description>&lt;p&gt;&lt;a href=&quot;http://globosatplay.globo.com/&quot;&gt;Globosat Play&lt;/a&gt; is a video product for pay TV subscribers, where you can catch up programs you missed on TV. It’s like an umbrella for a couple of different channels. One of the most popular of them is &lt;a href=&quot;http://sportv.globo.com/&quot;&gt;SporTV&lt;/a&gt;, one of the largest sports channels in Brazil.&lt;/p&gt;

&lt;p&gt;Last year we had the Olympic Games, a major sports event here in Rio. As SporTV channel was going to have a large coverage for the event, we decided to rethink the user experience for &lt;a href=&quot;http://globosatplay.globo.com/sportv/&quot;&gt;SporTV Play&lt;/a&gt; (SporTV channel offers inside Globosat Play). In this case, we were going to focus in improving the live TV experience, which is responsible for most of the audience.&lt;/p&gt;

&lt;p&gt;Besides rethinking the user experience, we decided to also rethink our front-end architecture, and address a couple of the issues we had.&lt;/p&gt;

&lt;p&gt;I’ve already written about &lt;a href=&quot;/2016/09/globosat-play-architecture-overview/&quot;&gt;Globosat Play architecture&lt;/a&gt; before. Its front-end is basically a couple of Rails apps using regular erb templates. These apps share components through a component library called globotv-ui. It’s about 4 years old, way before newer component technologies arised and got popular.&lt;/p&gt;

&lt;p&gt;As descripted in &lt;a href=&quot;/2016/09/globosat-play-architecture-overview/&quot;&gt;the previous post&lt;/a&gt;, this component library solution allowed us to avoid rewriting the same components again and again for each app. We were able to share our components, but the main problem is that this was an in-house solution. We defined our components structure to attend our needs, so it was really hard to share them outside our product.&lt;/p&gt;

&lt;p&gt;Also, we had a few problems with our front-end architecture. When we updated a component that was used on pages served by different apps, we needed to “synchronize” the deploys. If we deployed one app and took too long before deploying another, in the mean time, both would have different versions of that component. That could generate an inconsistency for our product, and it was one of the issues we wanted to address with the new solution.&lt;/p&gt;

&lt;p&gt;In late 2015, we started a couple of technical discussions and some proofs of concept, and decided to adopt &lt;a href=&quot;https://facebook.github.io/react/&quot;&gt;React&lt;/a&gt; in our front-end. A couple of arguments led the way to our decision:&lt;/p&gt;

&lt;h3 id=&quot;standardize-components-structure&quot;&gt;Standardize components structure&lt;/h3&gt;

&lt;p&gt;Even after creating a dozen components in globotv-ui, we still could find some differences among them. That’s because the structure is loose and not well documented. Usually we start a new component looking at another one, and replicate that structure. But a lot of different developers worked on that library, each one with his own preferences. So we really didn’t have a pattern for components. They were well organized and tested, but in the end, they were just a couple of JS and CSS files, sometimes with a template for generating the HTML (in &lt;a href=&quot;http://handlebarsjs.com/&quot;&gt;Handlebars&lt;/a&gt; for client-side rendering, or ERB templates with Rails helpers for server-side).&lt;/p&gt;

&lt;p&gt;The clear and well known React component structure helps keeping a pattern among components. The &lt;a href=&quot;https://facebook.github.io/react/docs/react-component.html#the-component-lifecycle&quot;&gt;component lifecycle&lt;/a&gt; lets us manage how our components should behave. It allows us to not only open source a couple of generic components, but also search for ready components instead of recreating everything we needed.&lt;/p&gt;

&lt;p&gt;As an example of this, we wanted to keep our header sticky after the user scrolls down the page. Instead of implementing this behavior, we used &lt;a href=&quot;https://github.com/KyleAMathews/react-headroom&quot;&gt;react-headroom&lt;/a&gt;. Problem solved!&lt;/p&gt;

&lt;h3 id=&quot;declarative-programming-model&quot;&gt;Declarative programming model&lt;/h3&gt;

&lt;p&gt;Another benefit that React brings is its declarative programming model, instead of the traditional imperative model. &lt;a href=&quot;http://chibicode.com/react-js-introduction-for-people-who-know-just-enough-jquery-to-get-by/&quot;&gt;Here&lt;/a&gt; is a simple example: a text area with a “Tweet” button, which should be disabled while the text area field is empty. Here is the imperative implementation using jQuery:&lt;/p&gt;

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Initially disable the button&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;disabled&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// When the value of the text area changes...&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;textarea&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;on&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// If there's at least one character...&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Enable the button.&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;disabled&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Else, disable the button.&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;disabled&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now the React version:&lt;/p&gt;

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;TweetBox&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Component&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&quot;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;nx&quot;&gt;handleChange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;setState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;nx&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;textarea&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;onChange&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;handleChange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;sr&quot;&gt;/textarea&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;gt;
&lt;/span&gt;        &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;button&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;disabled&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Tweet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;sr&quot;&gt;/button&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;gt;
&lt;/span&gt;      &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;sr&quot;&gt;/div&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;gt;
&lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;globo-play-release&quot;&gt;Globo Play release&lt;/h3&gt;

&lt;p&gt;The third strong argument in favor of React was the release of &lt;a href=&quot;https://globoplay.globo.com/&quot;&gt;Globo Play&lt;/a&gt;, in late 2015. It’s another video product developed here at &lt;a href=&quot;http://www.globo.com/&quot;&gt;Globo.com&lt;/a&gt;. It’s very similar to Globosat Play, and it already used React. So when we started developing the new interface for SporTV Play at the Olympic Games, the team that developed Globo Play already had a great experience with it to help our adoption.&lt;/p&gt;

&lt;h3 id=&quot;the-new-architecture&quot;&gt;The new architecture&lt;/h3&gt;

&lt;p&gt;As descripted in &lt;a href=&quot;/2016/09/globosat-play-architecture-overview/&quot;&gt;a previous post&lt;/a&gt;, our architecture was already microservices-based:&lt;/p&gt;

&lt;figure style=&quot;text-align: center&quot;&gt;
  &lt;img src=&quot;/images/globotv-architecture2.png&quot; alt=&quot;Globosat Play original architecture&quot; style=&quot;width: 800px&quot; /&gt;
  &lt;figcaption&gt;Globosat Play original architecture&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;In the front-end, we had a couple of different apps to serve different pages in our product, with an &lt;a href=&quot;http://nginx.org/&quot;&gt;nginx&lt;/a&gt; server in front of them. The nginx server proxies the requests to each app, according to the request path. Our idea was adding a new rule to it, to forward all requests to SporTV Play home and live signals pages to a new app. This new project was going to use React, and ideally share components with Globo Play.&lt;/p&gt;

&lt;p&gt;The first step was thinking about the API for this new front-end app. We already had a couple of APIs serving our current apps, so we didn’t need to create a new API. We could just use what we already had, but we decided to follow the &lt;a href=&quot;http://samnewman.io/patterns/architectural/bff/&quot;&gt;Back-end for Front-end pattern&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The APIs we already had served well many of our apps, and some of them are legacy. They have a lot of services, and most of them are fine-grained. To serve the new front-end app, we would probably need to make many requests to group all the data we needed.&lt;/p&gt;

&lt;p&gt;Also, we thought it was a good idea to separate the new app from the old ones. Doing this, we would have less chance of coupling old and new apps. Suppose both of them consumed the same services; that would create a coupling between them. If the new app required a change in this service contract, we wouldn’t be able to do that without the risk of breaking the old app.&lt;/p&gt;

&lt;p&gt;Besides that, we know the mobile consumption is raising, and sometimes we suffer with terrible 3/4g connections. With a new API specifically designed to attend the needs of the new app, we could deliver the smallest possible payload (just the data we were going to use). Also, we could create more specific services, reducing the number of requests to a minimum.&lt;/p&gt;

&lt;p&gt;We followed two principles from the Back-end for Front-end (BFF) pattern: the front-end consumes services from its BFF and nothing more; and the front-end is the only BFF client. They are tightly coupled together, but this is not a problem, because they should both be maintained by the same team. It’s like we are splitting our app in two. The BFF is responsible for orchestrating requests from internal, fine-grained services, apply some business rules and deliver data ready to be consumed by the front-end, which just consumes these coase-grained services and takes care of the presentation layer.&lt;/p&gt;

&lt;figure style=&quot;text-align: center&quot;&gt;
  &lt;img src=&quot;/images/bff.png&quot; alt=&quot;BFF architecture&quot; style=&quot;width: 800px&quot; /&gt;
  &lt;figcaption&gt;BFF architecture&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;One downside of the BFF is code replication. Some of the services we created in the BFF were new and very specific to our new SporTV Play front-end app (like the current schedule and the list of live signals). Others were already available in our internal APIs. But to follow the rule were the front-end can’t access any service outside its BFF, we needed to add a new route to the BFF, and basically make a proxy pass to our internal APIs. As any decision in software engineering, it’s a tradeoff. There is no perfect solution for everything, and the benefits overcome this issue.&lt;/p&gt;

&lt;p&gt;For more details and discussions about the BFF pattern, check out &lt;a href=&quot;http://samnewman.io/patterns/architectural/bff/&quot;&gt;Sam Newman&lt;/a&gt;’s and &lt;a href=&quot;http://philcalcado.com/2015/09/18/the_back_end_for_front_end_pattern_bff.html&quot;&gt;Phil Calçado&lt;/a&gt;’s articles.&lt;/p&gt;

&lt;p&gt;This is the first post of a series. In the next ones, I intend to write a little more about React components, state management, CSS architecture and components sharing.&lt;/p&gt;

</description>
        <pubDate>Tue, 18 Apr 2017 00:00:00 +0000</pubDate>
      </item>
    
      



      <item>
        <title>Handling request cache in Ruby | Guilherme Garnier</title>
        
        <link>https://blog.guilhermegarnier.com/2017/02/handling_request_cache_in_ruby/</link>
        <guid isPermaLink="true">https://blog.guilhermegarnier.com/2017/02/handling_request_cache_in_ruby/</guid>
        <description>&lt;p&gt;Handling HTTP cache is one of the most important aspects when you need to scale a web application. If well used, it can be your best friend; but when badly used, it may be you worst enemy.&lt;/p&gt;

&lt;p&gt;I’m not going to explain the basic aspects of caching here, there are already a lot of great material about it. I’m going to bring a specific problem here.&lt;/p&gt;

&lt;p&gt;On a previous post, I wrote about Globosat Play architecture. As explained there, it evolved to a microservices architecture, and as such, we ended up making a lot of HTTP requests to our internal services. So we needed to manage those requests very well.&lt;/p&gt;

&lt;p&gt;Suppose you access a page like &lt;a href=&quot;http://globosatplay.globo.com/combate/&quot;&gt;Combate channel home page&lt;/a&gt;. To fill up every information in that page, we need to query data from:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;a videos API, to bring up a list of available channels and the latest videos from Combate channel&lt;/li&gt;
  &lt;li&gt;a highlights API, to check the latest highlights selected by an editor&lt;/li&gt;
  &lt;li&gt;an events API, to check a list of previous and next UFC events&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That means a user request could be represented by something like this:&lt;/p&gt;

&lt;figure style=&quot;text-align: center&quot;&gt;
  &lt;img src=&quot;/images/requests-without-cache.png&quot; alt=&quot;Requests without cache&quot; style=&quot;width: 500px&quot; /&gt;
  &lt;figcaption&gt;Requests without cache&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Now imagine one of these services is unavailable. Or very slow. Or giving un unexpected answer. If we didn’t consider these scenarios, we would end up with a brittle application, susceptible to a lot of issues.&lt;/p&gt;

&lt;p&gt;Michael Nygard, in &lt;a href=&quot;https://pragprog.com/book/mnee/release-it&quot;&gt;Release It!&lt;/a&gt;, says we must develop cynical systems:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Enterprise software must be cynical. Cynical software expects bad things to happen and is never surprised when they do. Cynical software doesn’t even trust itself, so it puts up internal barriers to protect itself from failures. It refuses to get too intimate with other systems, because it could get hurt.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That means we shouldn’t trust anybody. Don’t assume a service is up, available, fast and correct. Even if you know and trust the maintainers of this service, consider that it may have problems (and it will, eventually!). One of the defense mechanisms against this is caching.&lt;/p&gt;

&lt;p&gt;At Globosat Play, we decided to implement two levels of cache. We call them performance and stale.&lt;/p&gt;

&lt;h2 id=&quot;performance-cache&quot;&gt;Performance cache&lt;/h2&gt;

&lt;p&gt;The performance cache is meant to avoid a flood of unnecessary requests to a single resource in a short period of time. Going back to Combate home page example, one of the services our back-end requests is a list of next UFC events. This doesn’t change often; only when a new event is created, or when an event finishes, once a couple of weeks. That means, it’s very wasteful to hit that service for every user accessing Combate home page. Suppose the events API response changes once a week; if that page gets 100,000 hits in that period, that means I would make 100,000 requests for that API, when I could just make one and keep the results in cache, which is much faster.&lt;/p&gt;

&lt;p&gt;The solution for this is keeping a performance cache for a specific period of time. Suppose I set my cache for 5 minutes. The decision flow for this would be:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;cache available?&lt;/strong&gt; Respond with cache&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;cache unavailable?&lt;/strong&gt; Make the request, write the response in cache, set its TTL (Time-to-leave) for 5 minutes, respond&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That means I would hit that API only once every 5 minutes, independenly of how many users are accessing my home page right now. I’m not only avoiding wasteful requests, but also protecting my internal services and giving faster responses - it’s much faster to access the cache than making an HTTP request. The diagram below depicts this scenario:&lt;/p&gt;

&lt;figure style=&quot;text-align: center&quot;&gt;
  &lt;img src=&quot;/images/requests-with-cache.png&quot; alt=&quot;Requests with cache&quot; style=&quot;width: 500px&quot; /&gt;
  &lt;figcaption&gt;Requests with cache&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The problem in this scenario is, even if I’m sure that my events API only changes once a week, I can’t set my cache TTL for 1 week. Imagine if I do that and the cache expires a few minutes before a new event is registered. That means I won’t see the new event until the next week! You need to carefully evaluate the performance cache times for each service you depend on.&lt;/p&gt;

&lt;p&gt;Even if you have a service that can’t be cached for that long, you could have a great benefit from caching the request for at least a few seconds. Imagine an application with 10,000 requests/s. If you set the back-end service request cache TTL for 1 second, you are making a single request for your service, instead of 10,000 requests!&lt;/p&gt;

&lt;h2 id=&quot;stale-cache&quot;&gt;Stale cache&lt;/h2&gt;

&lt;p&gt;The second cache level is stale. It’s a safety against problems like network instability or service unavailable. Let’s use the latest videos API as an example. Suppose my application back-end tries to access this service and it gets a 500 HTTP status code. If I have a stale cached version of it, I can use it to give a valid response to its client. The stale data may be outdated by a few minutes or hours, but it’s still better than giving no response at all - of course, that depends on the case. For some kinds of services, an outdated response may not be feasible, like giving the wrong balance when your client accesses his bank account. But for most of the cases, stale cache is a great alternative.&lt;/p&gt;

&lt;p&gt;Usually we set the performance cache time for a few minutes and the stale cache for a few hours. Our standard setup is 5 minutes and 6 hours, respectivelly.&lt;/p&gt;

&lt;h2 id=&quot;implementing-cache-levels-in-ruby&quot;&gt;Implementing cache levels in Ruby&lt;/h2&gt;

&lt;p&gt;To implement performance and stale cache levels in Ruby applications, we created and open sourced a gem called &lt;a href=&quot;https://github.com/globocom/content-gateway-ruby&quot;&gt;Content Gateway&lt;/a&gt;. With it, it’s much easier to manage cache levels.&lt;/p&gt;

&lt;p&gt;After installing it, you need to configure the default request timeout, the performance and stale cache expiration times and the cache backend, besides other optional configurations:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;OpenStruct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;ss&quot;&gt;timeout: &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;seconds&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;ss&quot;&gt;cache_expires_in: &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;minutes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;ss&quot;&gt;cache_stale_expires_in: &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;ss&quot;&gt;cache: &lt;/span&gt;&lt;span class=&quot;no&quot;&gt;ActiveSupport&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Cache&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;lookup_store&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:memory_store&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;gateway&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;ContentGateway&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Gateway&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;My API&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;With this basic configuration, you can start to make HTTP requests. You can also override the default configurations for each request:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Params are added via query string&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;gateway&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;https://www.goodreads.com/search.xml&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;key: &lt;/span&gt;&lt;span class=&quot;no&quot;&gt;YOUR_KEY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;q: &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Ender's Game&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# =&amp;gt; &quot;&amp;lt;?xml version=\&quot;1.0\&quot; encoding=\&quot;UTF-8\&quot;?&amp;gt;\n&amp;lt;GoodreadsResponse&amp;gt;\n  &amp;lt;Request&amp;gt;...&quot;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Specific configuration params are supported, like &quot;timeout&quot; and &quot;skip_cache&quot;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;gateway&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get_json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;https://api.cdnjs.com/libraries/jquery&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;timeout: &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;skip_cache: &lt;/span&gt;&lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# =&amp;gt; {&quot;name&quot;=&amp;gt;&quot;jquery&quot;, &quot;filename&quot;=&amp;gt;&quot;jquery.min.js&quot;, &quot;version&quot;=&amp;gt;&quot;3.1.1&quot;, ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It supports POST, PUT and DELETE as well. For all verbs, there are two methods for making the request: one is simply the name of the verb and the other has &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_json&lt;/code&gt; suffix. The former treats the response body as string, and the latter, as a Hash.&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;gateway&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;post_json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;https://api.dropboxapi.com/2/files/copy&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;headers: &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Authorization&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Bearer ACCESS_TOKEN&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;payload: &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;from_path: &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;path1&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;to_path: &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;path2&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;gateway&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;put_json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;https://a.wunderlist.com/api/v1/list_positions/id&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;payload: &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;values: &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4567&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4568&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9876&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;234&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;revision: &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;123&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;gateway&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;delete&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;https://a.wunderlist.com/api/v1/tasks/id&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You can also make a few other customizations. Check out &lt;a href=&quot;https://github.com/globocom/content-gateway-ruby&quot;&gt;the project page on github&lt;/a&gt; for more information and examples.&lt;/p&gt;
</description>
        <pubDate>Thu, 09 Feb 2017 00:00:00 +0000</pubDate>
      </item>
    
      



      <item>
        <title>Software Developers and Digital Artists | Guilherme Garnier</title>
        
        <link>https://blog.guilhermegarnier.com/2016/12/software-developers-and-digital-artists/</link>
        <guid isPermaLink="true">https://blog.guilhermegarnier.com/2016/12/software-developers-and-digital-artists/</guid>
        <description>&lt;p&gt;&lt;a href=&quot;https://jovemnerd.com.br/nerdcast/&quot;&gt;Nerdcast&lt;/a&gt; is an amazing podcast (in portuguese) about nerdie stuff in general. One of its latest episodes talked about the &lt;a href=&quot;https://jovemnerd.com.br/nerdcast/profissao-artista-digital/&quot;&gt;digital artist profession&lt;/a&gt;. The guests were animators that worked on feature films like &lt;a href=&quot;http://www.imdb.com/title/tt3521164/&quot;&gt;Moana&lt;/a&gt; and &lt;a href=&quot;http://www.imdb.com/title/tt1211837/&quot;&gt;Doctor Strange&lt;/a&gt;. They talked a lot about what they do, and I saw a lot of analogies between their work and software development.&lt;/p&gt;

&lt;p&gt;One of the podcast guests said that we was working on a specific scene for Doctor Strange for about 2 months, and suddenly his boss called him and told the director decided to cut that scene off from the film. Those 2 months of work turned into garbage. The lesson he learned from that is, you shouldn’t get attached to the project you’re working on. It’s not &lt;strong&gt;your project&lt;/strong&gt;, it’s &lt;strong&gt;your company’s project&lt;/strong&gt; and you happen to be working on it, which is very different. The analogy here is very clear, because that happens a lot in software projects: sometimes your client decides the feature you’ve been working on for a few weeks or months is not that important, or even worse, that the whole project shouldn’t be maintained anymore. It’s very hard not to be impacted by these news, but if you don’t work on that, you will frequently get frustrated. The project is meant to deliver value to your client, not you. Always remember that &lt;a href=&quot;http://bravenewgeek.com/you-are-not-paid-to-write-code/&quot;&gt;you are not paid to write code&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you want a project to be really yours, and have total freedom to decide what to do and how to do it, you need a personal project. But it’s important to notice that, if someday you decide to make that a business, turning that into a startup or selling some kind of product or service, you will start to have clients. When that happens, you need to be ready to let go of your ideas; if nobody wants your product, be ready to &lt;a href=&quot;https://en.wikipedia.org/wiki/Lean_startup#Pivot&quot;&gt;pivot&lt;/a&gt; (or even discontinue the whole product). &lt;a href=&quot;https://en.wikipedia.org/wiki/A/B_testing&quot;&gt;A/B tests&lt;/a&gt; are also a great way to learn how to let go of your ideas and beliefs: if a hypothesis is proven worse than the default behavior, just delete it.&lt;/p&gt;

&lt;p&gt;Another important topic that was mentioned in the podcast was: the guests, as artists, have a hard time deciding when to stop improving their work. They start working on a scene and iterate a couple of times to make it better. As perfectionists, they want to keep polishing their work. But sometimes the scene is already looking so good that any improvement won’t be noticed by the audience, so it just &lt;strong&gt;won’t deliver value to the client anymore&lt;/strong&gt;. The problem is not knowing when to stop. We can make an analogy here with refactoring: sometimes we develop a new feature, and even after it’s implemented and well tested, we decide to refactor. The objective could be making the code clearer for anyone that may touch it later - to implement a new feature of fix a bug - or maybe extracting a part of it to remove duplication from a similar feature you already had. In both cases, the refactoring won’t deliver value in the short term, but will on the mid-term or long term: you will have lower maintenance costs. But we may have the same problem as the animators: it’s very hard to know when to stop polishing the code. At some point, the refactoring won’t deliver value anymore, and we just refactor to please ourselves. As said before, you need to remember the project is not yours, and &lt;a href=&quot;http://bravenewgeek.com/you-are-not-paid-to-write-code/&quot;&gt;you are not paid to write code&lt;/a&gt;!&lt;/p&gt;
</description>
        <pubDate>Mon, 12 Dec 2016 00:00:00 +0000</pubDate>
      </item>
    
      



      <item>
        <title>Globosat Play architecture overview | Guilherme Garnier</title>
        
        <image>
          <url>https://blog.guilhermegarnier.com/images/globosatplay-architecture.png</url>
          <title>Globosat Play architecture overview</title>
          <link>https://blog.guilhermegarnier.com/2016/09/globosat-play-architecture-overview/</link>
        </image>
        
        <link>https://blog.guilhermegarnier.com/2016/09/globosat-play-architecture-overview/</link>
        <guid isPermaLink="true">https://blog.guilhermegarnier.com/2016/09/globosat-play-architecture-overview/</guid>
        <description>&lt;p&gt;This post is at least one year late. Since I gave a few &lt;a href=&quot;https://blog.guilhermegarnier.com/talks/&quot;&gt;talks&lt;/a&gt; about &lt;a href=&quot;http://globosatplay.globo.com/&quot;&gt;Globosat Play&lt;/a&gt; architecture (&lt;a href=&quot;https://blog.guilhermegarnier.com/evolucao-arquitetura/&quot;&gt;slides in portuguese&lt;/a&gt;), I intended to write a more detailed post, but always procrastinated about this.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.globo.com/&quot;&gt;Globo.com&lt;/a&gt; is the internet arm of the largest media conglomerate from Brazil, and one of the largest in the world. One of the areas of the company is responsible for our video platform, which includes encoding, distribution and streaming for any website of the group that needs videos.&lt;/p&gt;

&lt;p&gt;About 5 years ago, one of our videos team developed a video product called &lt;a href=&quot;https://web.archive.org/web/20151103032004/http://globotv.globo.com/&quot;&gt;globo.tv&lt;/a&gt; (recently discontinued and replaced with a newer product, &lt;a href=&quot;https://globoplay.globo.com/&quot;&gt;Globo Play&lt;/a&gt;). Its content was focused on Rede Globo (our main broadcast TV network) shows, like news, sports and the famous brazilian telenovelas. Most of globo.tv content were small scenes from these TV shows, open for every user, but it also offered full episodes for paying subscribers.&lt;/p&gt;

&lt;figure style=&quot;text-align: center&quot;&gt;
  &lt;img src=&quot;/images/globotv.png&quot; alt=&quot;globo.tv&quot; style=&quot;width: 800px&quot; /&gt;
  &lt;figcaption&gt;globo.tv home page&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The original architecture was a single monolithic &lt;a href=&quot;http://rubyonrails.org/&quot;&gt;Rails&lt;/a&gt; app, with a &lt;a href=&quot;https://unicorn.bogomips.org/&quot;&gt;Unicorn&lt;/a&gt; application server, a &lt;a href=&quot;https://www.mongodb.com/&quot;&gt;MongoDB&lt;/a&gt; database and a &lt;a href=&quot;http://redis.io/&quot;&gt;Redis&lt;/a&gt; instance for cache, all behind an &lt;a href=&quot;https://nginx.org/&quot;&gt;nginx&lt;/a&gt; HTTP server. This architecture served very well for some time.&lt;/p&gt;

&lt;figure style=&quot;text-align: center&quot;&gt;
  &lt;img src=&quot;/images/globotv-architecture.png&quot; alt=&quot;globo.tv architecture&quot; style=&quot;width: 600px&quot; /&gt;
  &lt;figcaption&gt;globo.tv architecture&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Then came 2012 with new demands for globo.tv. We needed to start offering live streaming of a couple of events, like &lt;a href=&quot;http://www.ufc.com.br/&quot;&gt;UFC&lt;/a&gt;, &lt;a href=&quot;http://gshow.globo.com/realities/bbb/&quot;&gt;Big Brother Brasil&lt;/a&gt;, soccer championships and the Winter Olympic Games. Also, we needed to start offering a collection of videos from &lt;a href=&quot;http://sportv.globo.com/site/combate/&quot;&gt;Combate&lt;/a&gt; channel, focused on MMA sports.&lt;/p&gt;

&lt;p&gt;At this point, we realized the old monolithic architecture wouldn’t serve anymore. So we needed to break it in smaller parts. The first step was identifying smaller subdomains inside videos. The first ones we identified and split were live streaming, for these new live events demand, and VoD (video on demand), for Combate channel videos.&lt;/p&gt;

&lt;p&gt;Clearly these new subdomains deserved their own projects, but the problem is, they needed to share a few business rules and data from the original globo.tv project, and also between them. That meant we needed to extract a few services from globo.tv to its own project: &lt;em&gt;globotv-api&lt;/em&gt; was born!&lt;/p&gt;

&lt;figure style=&quot;text-align: center&quot;&gt;
  &lt;img src=&quot;/images/globotv-architecture2.png&quot; alt=&quot;globo.tv architecture, 2.0&quot; style=&quot;width: 800px&quot; /&gt;
  &lt;figcaption&gt;globo.tv architecture, 2.0&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;At this point, we had:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;em&gt;&lt;strong&gt;globotv-api&lt;/strong&gt;&lt;/em&gt;, which offered a few basic video services, like the most recent videos from a specific program and the most watched programs&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;&lt;strong&gt;globotv&lt;/strong&gt;&lt;/em&gt;, the remain of the original project, which started to consume &lt;em&gt;globotv-api&lt;/em&gt; services. It was responsible for serving the original pages, like home, program page, video page and search&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;&lt;strong&gt;globotv-events&lt;/strong&gt;&lt;/em&gt;, a new project responsible for live streaming of different kinds of events. It also consumed &lt;em&gt;globotv-api&lt;/em&gt; and offered its own API with specific services, like a list of live events happening right now&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;&lt;strong&gt;globotv-vod&lt;/strong&gt;&lt;/em&gt;, another new project, which served the collection of videos for Combate channel. It also consumed &lt;em&gt;globotv-api&lt;/em&gt; and offered its own API with specific services, like a list of competitions and videos from a specific fighter&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The break-up of the monolith was a very important move. If we didn’t split it at that time, we would end up with a larger and larger project, which would soon become a monster - harder to understand, harder to maintain, harder to evolve. It allowed us to share a small part of our domain, which was common among these new requirements and the original project.&lt;/p&gt;

&lt;p&gt;To split the front-end among different projects, we used &lt;a href=&quot;https://nginx.org/&quot;&gt;nginx&lt;/a&gt; as a router. We already used it in front of our application server, but with multiple projects, we created an upstream for each one, and configured a location for each URL pattern, like this:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;upstream globotv {
  server globotv.internal.globo.com;
}

upstream globotv-events {
  server globotv-events.internal.globo.com;
}

upstream globotv-vod {
  server globotv-vod.internal.globo.com;
}

server {
  listen 80;
  server_name globotv.globo.com;

  location ~ (.+)/ao-vivo/ {
    proxy_set_header Host $http_host;
    proxy_pass http://globotv-events;
    break;
  }

  location ~ ^/combate/ {
    proxy_set_header Host $http_host;
    proxy_pass http://globotv-vod;
    break;
  }

  location ~ /  {
    proxy_set_header Host $http_host;
    proxy_pass http://globotv;
    break;
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The internal domains aren’t publicly exposed, the only way to access the projects is through nginx. With a configuration similar to this, nginx routes incoming requests for &lt;em&gt;globotv.globo.com&lt;/em&gt; domain to different projects, according to the URL pattern: URLs that contain “/ao-vivo/” pattern (“live” in portuguese) are routed to &lt;em&gt;globotv-events&lt;/em&gt; project; URLs starting with “/combate/” are forwarded to &lt;em&gt;globotv-vod&lt;/em&gt; project. The last location matches every other URL to the original &lt;em&gt;globotv&lt;/em&gt; project. &lt;em&gt;globotv-api&lt;/em&gt; project doesn’t appear in this configuration, as it isn’t publicly accessible (it’s only accessed from the other projects). This configuration allowed us to serve different pages from different projects transparently for the user.&lt;/p&gt;

&lt;p&gt;Despite many benefits, this architectural change brought new challenges. The first one was keeping a consistent visual identity among pages served from different projects. For example, the video thumb component used in the home page should be just like the video thumb from the search page; the product header should be the same in every page. The split in multiple projects was an architectural decision; the user doesn’t need to know this, because globo.tv was still a single product.&lt;/p&gt;

&lt;p&gt;The solution for this problem was creating a components library, called &lt;em&gt;globotv-ui&lt;/em&gt;. With this solution, we were able to share visual components, comprised of HTML, JS and CSS. They were standardized and documented, which made it very easy to create new components and share them among these projects - as all of them were Ruby on Rails projects, we delivered &lt;em&gt;globotv-ui&lt;/em&gt; library as a Rubygem.&lt;/p&gt;

&lt;figure style=&quot;text-align: center&quot;&gt;
  &lt;img src=&quot;/images/globotv-ui-components.png&quot; alt=&quot;examples of components from globotv-ui library&quot; style=&quot;width: 700px&quot; /&gt;
  &lt;figcaption&gt;examples of components from globotv-ui library&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Fast forward a few years, and a new video product emerged: &lt;a href=&quot;http://globosatplay.globo.com/&quot;&gt;Globosat Play&lt;/a&gt;. It was very similiar to globo.tv on a few aspects - the idea was offering VoD and live streaming from &lt;a href=&quot;http://canaisglobosat.globo.com/&quot;&gt;Globosat&lt;/a&gt; channels (TV channels available only for paying subscribers), but with a few differences: we also needed to offer movies, and now the focus was on subscribers, and not on free users anymore.&lt;/p&gt;

&lt;figure style=&quot;text-align: center&quot;&gt;
  &lt;img src=&quot;/images/globosatplay-homepage.png&quot; alt=&quot;Globosat Play home page&quot; style=&quot;width: 700px&quot; /&gt;
  &lt;figcaption&gt;Globosat Play home page&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The main challenge at that point was how to share components and services between these two products, but without one limiting the other’s evolution and requirements. We needed to re-evaluate our architecture and business domain to solve this issue. We realized that, as both products had many similarities and some differences, we needed to create a common, shared layer of services, but also keep some services specific. The new architecture has become something like this:&lt;/p&gt;

&lt;figure style=&quot;text-align: center&quot;&gt;
  &lt;img src=&quot;/images/globosatplay-architecture.png&quot; alt=&quot;Globosat Play architecture&quot; style=&quot;width: 800px&quot; /&gt;
  &lt;figcaption&gt;Globosat Play architecture&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;This diagram is simplified from the previous ones - the new projects are also Rails apps with their own MongoDB and Redis instances. The green boxes are projects that serve web pages, and the blue ones are APIs. We split our projects in 3 parts: the top one in the image is specific to globo.tv product; the bottom one is specific to Globosat Play; and the middle one represent shared services. You can notice most of the projects created before (&lt;em&gt;globotv-api&lt;/em&gt;, &lt;em&gt;globotv-events&lt;/em&gt; and &lt;em&gt;globotv-vod&lt;/em&gt;) started being shared, as Globosat Play also had those same requirements.&lt;/p&gt;

&lt;p&gt;Besides that, we created a few other projects. Some of them, like &lt;em&gt;movies&lt;/em&gt;, attended a specific subdomain that didn’t exist before; others, like &lt;em&gt;globotv-search&lt;/em&gt;, were extracted from the original &lt;em&gt;globotv&lt;/em&gt; project - these features already existed, but now we needed to share them between globo.tv and Globosat Play. Also, &lt;em&gt;globotv-api&lt;/em&gt; kept being our main source for basic video services.&lt;/p&gt;

&lt;p&gt;This evolution also required a few new configurations on our nginx server:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;upstream movies {
  server movies.internal.globo.com;
}

upstream globotv-search {
  server globotv-search.internal.globo.com;
}

upstream globosat-play {
  server globosatplay.internal.globo.com;
}

upstream globotv-events {
  server globotv-events.internal.globo.com;
}

upstream globotv-vod {
  server globotv-vod.internal.globo.com;
}

server {
  listen 80;
  server_name globosatplay.globo.com;

  location ~ (.+)/ao-vivo/ {
    proxy_set_header Host $http_host;
    proxy_pass http://globotv-events;
    break;
  }

  location ~ ^/telecine/ {
    proxy_set_header Host $http_host;
    proxy_pass http://movies;
    break;
  }

  location ~ ^/busca/ {
    proxy_set_header Host $http_host;
    proxy_pass http://globotv-search;
    break;
  }

  location ~ /  {
    proxy_set_header Host $http_host;
    proxy_pass http://globosat-play;
    break;
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In the end, we realized the microservices architecture brought a lot of advantages:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;smaller, easier to manage projects&lt;/strong&gt;: each subdomain is separated in its own project, which helps keeping the code smaller and easier to understand. Also, this allows different teams to work in different projects&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;faster builds&lt;/strong&gt;: with many small projects, the time needed to build and run test suites for each one gets smaller, which gives faster feedback cycles. That stimulates developers to run the test suites more frequently&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;smaller and less risky deploys&lt;/strong&gt;: each project is responsible for a small part of the product; that means bugs only affect that small subdomain of it. Suppose you introduce a bug in the search project; that would affect only the search page. Your users would still be able to access the home page and watch videos. That gives confidence for the team to deploy to production more frequently, which reduces even more the risk of bugs&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;flexible infrastructure&lt;/strong&gt;: every service is a REST API over HTTP, using JSON format. That means you could write each one in a different language with a different database technology, if you wanted and needed. You could select the best tools for the job&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;easier incremental changes&lt;/strong&gt;: suppose you want to migrate your application server from &lt;a href=&quot;https://unicorn.bogomips.org/&quot;&gt;Unicorn&lt;/a&gt; to &lt;a href=&quot;http://puma.io/&quot;&gt;Puma&lt;/a&gt;. With a monolithic application, you would need to flip the switch all at once. With many small services, you could choose one to try Puma - maybe the less critical one. If the migration is successful, you could continue with this process one project at a time&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But we also had a few disadvantages:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;more complex architecture&lt;/strong&gt;: when a new developer starts in your team, it’s much harder to explain to him how the architecture works and what each project is responsible for&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;harder local environment setup&lt;/strong&gt;: another problem for new developers is setting up the local environment. With many projects, each one with its own requirements, that’s much harder&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;harder to update dependencies, like newer gems&lt;/strong&gt;: when you need to update a dependency, like a gem that fixes a critical security flaw, you need to do that once for each project. With a monolith, you would just need to do that a single time&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;harder to test&lt;/strong&gt;: when each service depends on many others, the setup for the test environment is much harder. You would need to set a complex environment for integration tests, or create fake APIs to replace real ones, or maybe mock API responses, using something like &lt;a href=&quot;https://github.com/vcr/vcr&quot;&gt;VCR&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;heterogeneous environment&lt;/strong&gt;: the flexibility earned with microservices might result in projects in different languages, with different databases and other dependencies. That makes it harder to maintain, because not every developer may understand the whole set of technologies&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;more failure points, harder to debug&lt;/strong&gt;: when a service fails, the problem may be a bug or database failure, for example, but may also be the result of a failure in another service it depends on. The debugging process gets harder and slower&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the end, the migration from monolith to microservices was very successful to us. But the main point here is realizing that microservices aren’t the magical solution to every problem - a messy monolith split would generate many messy microservices, as illustrated in the image below. You should analyze the characteristics of your project before deciding if microservices are the best option.&lt;/p&gt;

&lt;div style=&quot;margin: 0 auto; display: table&quot;&gt;
  &lt;blockquote class=&quot;twitter-tweet&quot;&gt;
    &lt;p lang=&quot;en&quot; dir=&quot;ltr&quot;&gt;Monolithic vs Microservices
      &lt;a href=&quot;https://t.co/EFUpsYkBNu&quot;&gt;pic.twitter.com/EFUpsYkBNu&lt;/a&gt;
    &lt;/p&gt;
    &amp;mdash; Fuckowski (@fuckowski)
    &lt;a href=&quot;https://twitter.com/fuckowski/status/717643893655937024&quot;&gt;April 6, 2016&lt;/a&gt;
  &lt;/blockquote&gt;
  &lt;script async=&quot;&quot; src=&quot;//platform.twitter.com/widgets.js&quot;&gt;&lt;/script&gt;
&lt;/div&gt;
</description>
        <pubDate>Thu, 01 Sep 2016 00:00:00 +0000</pubDate>
      </item>
    
      



      <item>
        <title>Modern-world birthday statistics | Guilherme Garnier</title>
        
        <image>
          <url>https://blog.guilhermegarnier.com/images/happy-birthday.png</url>
          <title>Modern-world birthday statistics</title>
          <link>https://blog.guilhermegarnier.com/2016/02/modern-world-birthday-statistics/</link>
        </image>
        
        <link>https://blog.guilhermegarnier.com/2016/02/modern-world-birthday-statistics/</link>
        <guid isPermaLink="true">https://blog.guilhermegarnier.com/2016/02/modern-world-birthday-statistics/</guid>
        <description>&lt;p&gt;When I was younger, I used to get a lot of phone calls during my birthdays, from my relatives and friends. But now the situation is much different.&lt;/p&gt;

&lt;p&gt;A few days ago I got older, and the “happy birthday” wishes came from many different mediums. So I decided to compile some curious (and completely useless) statistics about this:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/images/happy-birthday.png&quot; class=&quot;post-image-link&quot;&gt;&lt;img src=&quot;/images/happy-birthday.png&quot; alt=&quot;Happy Birthday&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Sometimes technology makes life more complicated, instead of simplifying it.&lt;/p&gt;
</description>
        <pubDate>Thu, 18 Feb 2016 00:00:00 +0000</pubDate>
      </item>
    
      



      <item>
        <title>Software quality isn't optional | Guilherme Garnier</title>
        
        <image>
          <url>https://blog.guilhermegarnier.com/images/fast-food.jpg</url>
          <title>Software quality isn't optional</title>
          <link>https://blog.guilhermegarnier.com/2015/11/software-quality-isnt-optional/</link>
        </image>
        
        <link>https://blog.guilhermegarnier.com/2015/11/software-quality-isnt-optional/</link>
        <guid isPermaLink="true">https://blog.guilhermegarnier.com/2015/11/software-quality-isnt-optional/</guid>
        <description>&lt;p&gt;In the last weekend, I attended to &lt;a href=&quot;http://devday.devisland.com/&quot;&gt;DevDay 2015&lt;/a&gt;, in Belo Horizonte, where I presented a talk about &lt;a href=&quot;https://blog.guilhermegarnier.com/evolucao-arquitetura/&quot;&gt;“evolution of a distributed architecture”&lt;/a&gt; (in portuguese).&lt;/p&gt;

&lt;p&gt;The last talk of the event presented &lt;a href=&quot;http://stackoverflow.com/&quot;&gt;Stack Overflow&lt;/a&gt; architecture. This talk was very controversial, and I felt I needed to write something to show my point of view on this subject.&lt;/p&gt;

&lt;p&gt;A lot is being said in the community about the value of software engineering best practices. For the last decades, people like Uncle Bob, Kent Beck, Martin Fowler and many others have been doing a great effort promoting these practices, through books, posts and talks. These professionals made and still make a great job orienting new developers, so we can have well tested software projects and well designed architectures, focusing on aspects like maintainability, scalability, security and quality.&lt;/p&gt;

&lt;p&gt;Despite this, we know that, in many companies, management makes pressure on developers to deliver as fast as possible, forgetting about software quality and maintainability - even knowing they will be responsible for the project maintenance, at least in short term. This habit goes against every best practice described above, and is very harmful to new developers. These ones that start their career in companies with this idea in mind, even if they’ve read about best practices, end up believing that this is utopian, and that in practice it’s impossible to design a sustainable architecture, refactor legacy code or automate tests, because of pressures for fast delivery of new features. That’s why it’s essential to spread the word about best practices, to show these new developers that it’s not only possible to focus on quality, but also essential for the project evolution and maintenance.&lt;/p&gt;

&lt;p&gt;In the talk that closed &lt;a href=&quot;http://devday.devisland.com/&quot;&gt;DevDay 2015&lt;/a&gt;, &lt;a href=&quot;http://stackoverflow.com/&quot;&gt;Stack Overflow&lt;/a&gt; architecture was presented. The displayed numbers are impressive: it’s one of the 50 top sites in the world, with millions of page views per day, and all that supported by only 9 physical servers, each one working at around 5% load, plus 2 database servers. With this structure, the average load time is only 18 ms. How is this possible?&lt;/p&gt;

&lt;p&gt;The secret, according to this talk, is their obsession with performance. Every new feature must have the best possible performance. When a library or tool they use isn’t considered fast enough, they rewrite it from scratch. As layered architectures are slow, every database query is manually written, and directly in the controller.&lt;/p&gt;

&lt;p&gt;Stack Overflow code has low testability, because you can’t create mocks to replace the database connection, for instance. That’s why the project has very few automated tests (and it was mentioned that some developers don’t even run the tests). As they have a massive number of active and engaged users, any bug that’s introduced after a deploy is quickly found and reported at &lt;a href=&quot;http://meta.stackoverflow.com/&quot;&gt;Meta Stack Overflow&lt;/a&gt;. To update the operating system version, they just remove a server from the pool, apply the update and put the server back live. They assume that, if a new bug arises, users will soon find and report it.&lt;/p&gt;

&lt;p&gt;The speaker let it very clear that modeling, architecture and tests are good stuff, but they’re not for everyone. I personally disagree.&lt;/p&gt;

&lt;p&gt;Stack Overflow is a very particular case. As their audience is made of developers, and they’re very engaged and passionate about the product, bugs in production are considered acceptable, because the main focus is performance. But what’s the point of performance without quality? Would you buy the fastest car in the world even knowing it doesn’t have seat belts and air bags, and that it doesn’t support replacing a flat tire or a defective part? The analogy is very exaggerated - a bug in the site doesn’t involve life risk, but what I mean is, if you focus only and exclusively on performance, you give up other aspects like quality and security. It’s the same line of thought from those managers I mentioned before, who make a lot of pressure for fast delivery, regardless of quality.&lt;/p&gt;

&lt;figure style=&quot;text-align: center&quot;&gt;
  &lt;img src=&quot;/images/fast-food.jpg&quot; alt=&quot;Fast food&quot; style=&quot;width: 400px&quot; /&gt;
  &lt;figcaption&gt;Does every fast food need to be like this?&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;In my opinion, even if your project focuses on performance, quality can’t be abandoned. The other extreme - over-engineering - is also bad; if you have a small application, with only a couple of users, it doesn’t make sense to create a complex architecture, thinking about the possibility of maybe one day it may expand. This would be creating a solution for a problem that doesn’t exist.&lt;/p&gt;

&lt;p&gt;I have a real example for this: I register all my expenses in Google Spreadsheet. I wanted to share these data with my wife, but as the spreadsheet is very large, I created a small app that extracts these data and displays a very simple dashboard, only with information that she is interested in. This app has only two users - me and her -, and there isn’t a chance that this grows up. In this case, it doesn’t make sense for me to think about a scalable architecture. But when we talk about a product with tens of millions of users, the situation is very different. Currently, Stack Overflow doesn’t have a competitor to match, but if one rises with a better user experience or new features, they will have a hard time to follow.&lt;/p&gt;

&lt;p&gt;My main concern while watching this talk was the impact that these ideas can have on the audience. The majority of them were very young, probably students or professionals starting their career. A talk like this, spreading the word about optional software quality, can be very harmful to them. And it seems like &lt;a href=&quot;http://www.darkcoding.net/software/facebooks-code-quality-problem/&quot;&gt;Facebook has a similar issue with code quality&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To wrap up, I want to make it clear that I’m not doing a personal attack against Stack Overflow or the speaker. I admire her courage to take the stage and present such controversial ideas, even though I disagree on them. I’m a Stack Overflow user and will continue being after all.&lt;/p&gt;
</description>
        <pubDate>Thu, 05 Nov 2015 00:00:00 +0000</pubDate>
      </item>
    
      



      <item>
        <title>Lost in translation | Guilherme Garnier</title>
        
        <link>https://blog.guilhermegarnier.com/2015/11/lost-in-translation/</link>
        <guid isPermaLink="true">https://blog.guilhermegarnier.com/2015/11/lost-in-translation/</guid>
        <description>&lt;p&gt;After a few years maintaining this blog, I decided to start writing in english. I feel like I can reach a wider audience with this. Sometimes when I make a comment in a blog post or github issue, or answer a question in Stack Overflow, I want to link to something I wrote in my blog, but as I wrote in portuguese, I can’t do that.&lt;/p&gt;

&lt;p&gt;I don’t think this is going to be a problem for most developers - at least &lt;a href=&quot;/2009/07/livros-tecnicos-traduzidos-nunca-mais/&quot;&gt;I hope so&lt;/a&gt;; if you are a software developer and don’t understand english, you should!&lt;/p&gt;

&lt;p&gt;Eventually I may write something in portuguese again, if I have a reason for that, so I tagged every old post with &lt;a href=&quot;/search/?t=portuguese&quot;&gt;portuguese&lt;/a&gt;. And every post in english will be tagged with &lt;a href=&quot;/search/?t=english&quot;&gt;english&lt;/a&gt;. I also added direct links in the sidebar, for easy access.&lt;/p&gt;
</description>
        <pubDate>Thu, 05 Nov 2015 00:00:00 +0000</pubDate>
      </item>
    
      



      <item>
        <title>Simulando passagem de tempo em testes de Javascript | Guilherme Garnier</title>
        
        <link>https://blog.guilhermegarnier.com/2015/09/simulando-passagem-de-tempo-em-testes-de-javascript/</link>
        <guid isPermaLink="true">https://blog.guilhermegarnier.com/2015/09/simulando-passagem-de-tempo-em-testes-de-javascript/</guid>
        <description>&lt;p&gt;O &lt;a href=&quot;http://jasmine.github.io/&quot;&gt;Jasmine&lt;/a&gt; é uma das ferramentas de teste para Javascript mais usadas atualmente. A sintaxe estilo BDD lembra bastante o &lt;a href=&quot;http://rspec.info/&quot;&gt;RSpec&lt;/a&gt;, o que facilita a vida de quem já tem experiência com este.&lt;/p&gt;

&lt;p&gt;Uma das dificuldades ao realizar testes de Javascript é como simular a passagem do tempo. Existem duas situações básicas onde isso acontece:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Quando o código executa alguma animação, como fade in e slide down, por exemplo&lt;/li&gt;
  &lt;li&gt;Quando definimos uma função que só será executada após um período de tempo determinado&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;O primeiro caso pode ser ilustrado com este exemplo básico:&lt;/p&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;nt&quot;&gt;&amp;lt;button&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;button&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;Show Menu&lt;span class=&quot;nt&quot;&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;menu&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;style=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;display: none&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;Menu&lt;span class=&quot;nt&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

  &lt;span class=&quot;nt&quot;&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;#button&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;click&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;#menu&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;fadeIn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;nx&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Um clique no botão faz com que o menu apareça usando a função &lt;a href=&quot;http://api.jquery.com/fadein/&quot;&gt;jQuery.fadeIn&lt;/a&gt;. O teste para este código, a princípio, poderia ser algo assim:&lt;/p&gt;

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;describe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;example test&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;beforeEach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;

  &lt;span class=&quot;nx&quot;&gt;it&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;shows the menu after clicking the button&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;#button&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;click&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

    &lt;span class=&quot;nx&quot;&gt;expect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;#menu&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;toBeVisible&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;O problema é que, como a animação do fade in leva um pequeno período de tempo para executar (400 ms por padrão), o menu ainda não está visível no momento em que a expectativa é executada. Uma solução inocente, mas pouco eficiente, para este problema seria executar a expectativa num &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setTimeout&quot;&gt;setTimeout&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Neste caso específico, como a animação é feita usando jQuery, há uma propriedade &lt;a href=&quot;https://api.jquery.com/jquery.fx.off/&quot;&gt;jQuery.fx.off&lt;/a&gt; que permite desabilitar todas as animações. Desta forma, todas as transições são feitas instantaneamente, fazendo com que o teste original funcione:&lt;/p&gt;

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;describe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;example test&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;jQueryFxOff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;nx&quot;&gt;beforeEach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;jQueryFxOff&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;fx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;off&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;fx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;off&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;

  &lt;span class=&quot;nx&quot;&gt;afterEach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;fx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;off&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;jQueryFxOff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;

  &lt;span class=&quot;nx&quot;&gt;it&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;shows the menu after clicking the button&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;#button&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;click&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

    &lt;span class=&quot;nx&quot;&gt;expect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;#menu&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;toBeVisible&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note que o valor original da propriedade é armazenado numa variável e restaurado após o teste, para evitarmos que esta configuração afete outros testes que serão executados em sequencia.&lt;/p&gt;

&lt;p&gt;O segundo caso é quando temos algum código que só é executado após um período de tempo - usando &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setTimeout&quot;&gt;setTimeout&lt;/a&gt;, por exemplo:&lt;/p&gt;

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;someRandomCode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;

  &lt;span class=&quot;na&quot;&gt;waitForIt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;setTimeout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;someRandomCode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;waitForIt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;A melhor forma de testar este código é “fakeando” a passagem do tempo, para que o teste não precise aguardar. Uma boa ferramenta para isto são os os &lt;a href=&quot;http://sinonjs.org/docs/#clock&quot;&gt;fake timers&lt;/a&gt; do &lt;a href=&quot;http://sinonjs.org/&quot;&gt;Sinon.JS&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;describe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;my random test&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;clock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;nx&quot;&gt;beforeEach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;clock&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;sinon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;useFakeTimers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;spyOn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;someRandomCode&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;nx&quot;&gt;module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;waitForIt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;

  &lt;span class=&quot;nx&quot;&gt;afterEach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;clock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;restore&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;

  &lt;span class=&quot;nx&quot;&gt;it&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;tests my random code&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;clock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;tick&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;expect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;someRandomCode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;toHaveBeenCalled&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Outra boa opção é usar o &lt;a href=&quot;http://jasmine.github.io/2.3/introduction.html#section-Jasmine_Clock&quot;&gt;Jasmine Clock&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;describe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;my random test&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;beforeEach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;jasmine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;clock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;install&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;spyOn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;someRandomCode&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;nx&quot;&gt;module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;waitForIt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;

  &lt;span class=&quot;nx&quot;&gt;afterEach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;jasmine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;clock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;uninstall&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;

  &lt;span class=&quot;nx&quot;&gt;it&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;tests my random code&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;jasmine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;clock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;tick&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;expect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;someRandomCode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;toHaveBeenCalled&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</description>
        <pubDate>Thu, 10 Sep 2015 00:00:00 +0000</pubDate>
      </item>
    
      



      <item>
        <title>Ferramentas para avaliação de qualidade de código Ruby | Guilherme Garnier</title>
        
        <link>https://blog.guilhermegarnier.com/2014/11/ferramentas-para-avaliacao-de-qualidade-de-codigo-ruby/</link>
        <guid isPermaLink="true">https://blog.guilhermegarnier.com/2014/11/ferramentas-para-avaliacao-de-qualidade-de-codigo-ruby/</guid>
        <description>&lt;p&gt;Ferramentas para avaliar a qualidade do seu código existem aos montes, para qualquer linguagem de programação. Eu já utilizei algumas para Java, mas nunca tinha testado nenhuma para Ruby, apesar de ser a linguagem que mais uso há alguns anos. Por isso, resolvi testar todas as ferramentas que pude encontrar. Separei a avaliação entre serviços e ferramentas.&lt;/p&gt;

&lt;h2 id=&quot;serviços&quot;&gt;Serviços&lt;/h2&gt;

&lt;p&gt;Classifiquei como serviços as ferramentas onde, em vez de instalar e executar localmente, você libera acesso ao seu repositório de código a elas, que coletam métricas a partir do código e geram algum relatório. Os dois serviços que avaliei são gratuitos para projetos open source e pagos para projetos com código fechado.&lt;/p&gt;

&lt;h3 id=&quot;code-climate&quot;&gt;&lt;a href=&quot;https://codeclimate.com/&quot;&gt;Code Climate&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Este é certamente o serviço mais conhecido para avaliação de qualidade de código Ruby. Seu foco principal é gerar métricas baseadas em complexidade de código, mas ele também é capaz de identificar algumas falhas de segurança e cobertura de código dos testes.&lt;/p&gt;

&lt;h3 id=&quot;coveralls&quot;&gt;&lt;a href=&quot;https://coveralls.io/&quot;&gt;Coveralls&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;O Coveralls funciona de forma semelhante ao Code Climate, porém tem um foco maior em testes. Ele exibe o histórico de cobertura e a diferença para cada arquivo a cada commit.&lt;/p&gt;

&lt;h2 id=&quot;ferramentas&quot;&gt;Ferramentas&lt;/h2&gt;

&lt;p&gt;As ferramentas abaixo são open source e distribuídas através de gems. Para utilizá-las, basta instalar a gem e executar um comando, que analisa o código e gera relatórios ou dados brutos para serem analisados.&lt;/p&gt;

&lt;h3 id=&quot;brakeman&quot;&gt;&lt;a href=&quot;http://brakemanscanner.org/&quot;&gt;Brakeman&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;O Brakeman é uma ferramenta focada em localizar potenciais falhas de segurança no seu código. Ele também exibe alertas especificamente relacionados ao Rails, como falhas de segurança que já foram corrigidas numa versão do Rails mais recente do que a que você usa.&lt;/p&gt;

&lt;p&gt;Como ele tem foco em segurança, é muito importante manter esta gem sempre atualizada, para que ele possa detectar falhas descobertas mais recentemente.&lt;/p&gt;

&lt;h3 id=&quot;rubocop&quot;&gt;&lt;a href=&quot;https://github.com/bbatsov/rubocop&quot;&gt;RuboCop&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;O foco do RuboCop é localizar más práticas de programação no seu código, com base no &lt;a href=&quot;https://github.com/bbatsov/ruby-style-guide&quot;&gt;Ruby Style Guide&lt;/a&gt;. Algumas das regras são: uso de aspas simples ou duplas para definir strings, tamanho máximo da linha, número de linhas em cada método e uso de espaços na definição de hashes.&lt;/p&gt;

&lt;p&gt;Todas as regras do RuboCop podem ser configuradas ou desabilitadas. Basta criar um arquivo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.rubocop.yml&lt;/code&gt; na raiz do projeto com as configurações desejadas. Ele também possui regras específicas para projetos usando Rails.&lt;/p&gt;

&lt;p&gt;Se você usa o editor &lt;a href=&quot;https://atom.io/&quot;&gt;Atom&lt;/a&gt;, também é possível executar o RuboCop automaticamente com o plugin &lt;a href=&quot;https://atom.io/packages/atom-lint&quot;&gt;atom-lint&lt;/a&gt;. Assim, ao salvar um arquivo, o RuboCop é automaticamente executado, e os alertas são exibidos no próprio editor, ao lado de cada linha.&lt;/p&gt;

&lt;h3 id=&quot;rubycritic&quot;&gt;&lt;a href=&quot;https://github.com/whitesmith/rubycritic&quot;&gt;RubyCritic&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;O RubyCritic foi criado com o objetivo de se tornar uma alternativa gratuita ao Code Climate. Ele gera um relatório bem semelhante ao deste serviço, reportando os trechos mais complexos do seu código.&lt;/p&gt;

&lt;h3 id=&quot;metric-fu&quot;&gt;&lt;a href=&quot;https://github.com/metricfu/metric_fu/&quot;&gt;Metric Fu&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;O Metric Fu é um agregador de ferramentas de análise de código. Ele executa diversas ferramentas e gera uma página com links para os resultados de cada uma. É uma das ferramentas mais completas para análise de código Ruby, e uma das mais antigas.&lt;/p&gt;

&lt;h3 id=&quot;ruby-lint&quot;&gt;&lt;a href=&quot;https://github.com/YorickPeterse/ruby-lint&quot;&gt;Ruby-lint&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;O foco do Ruby-lint é localizar erros como variáveis não utilizadas ou não inicializadas, ou número errado de argumentos ao executar um método. O problema é que quando extendemos ou incluimos classes ou módulos definidos fora do projeto (em gems), ele não encontra as dependências e dá erro de constante indefinida. Apesar disso, parece que vem sendo bastante atualizado.&lt;/p&gt;

&lt;h3 id=&quot;reek&quot;&gt;&lt;a href=&quot;https://github.com/troessner/reek&quot;&gt;Reek&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Esta ferramenta é um detector de bad smells no código, como Long Parameter List e Feature Envy. Possui plugins para rodar alguns editores, como &lt;a href=&quot;http://www.vim.org/&quot;&gt;Vim&lt;/a&gt; e &lt;a href=&quot;http://macromates.com/&quot;&gt;TextMate&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;roodi&quot;&gt;&lt;a href=&quot;https://github.com/roodi/roodi&quot;&gt;Roodi&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Roodi significa “Ruby Object Oriented Design Inferometer”. Ele executa algumas métricas de complexidade de código, mas é bem básico. A maioria das métricas já são calculadas por outras ferramentas apresentadas aqui.&lt;/p&gt;

&lt;h3 id=&quot;flog&quot;&gt;&lt;a href=&quot;http://ruby.sadi.st/Flog.html&quot;&gt;Flog&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Gera um score baseado na complexidade de código. Bem básico.&lt;/p&gt;

&lt;h3 id=&quot;rails-best-practices&quot;&gt;&lt;a href=&quot;https://github.com/railsbp/rails_best_practices&quot;&gt;Rails Best Practices&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Ferramenta bem útil, gera métricas de qualidade baseadas no &lt;a href=&quot;http://rails-bestpractices.com/&quot;&gt;Rails Best Practices&lt;/a&gt;. Como diz o nome, é específico para projetos Rails. Também é disponibilizado como um &lt;a href=&quot;http://railsbp.com/&quot;&gt;serviço online&lt;/a&gt; gratuito, mas somente para projetos públicos no Github. Para projetos privados, é possível instalar uma instância própria, pois &lt;a href=&quot;https://github.com/railsbp/railsbp.com&quot;&gt;o servidor é open source&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;cane&quot;&gt;&lt;a href=&quot;https://github.com/square/cane&quot;&gt;Cane&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Mais um gerador de métricas de qualidade. As métricas são parecidas com as do RuboCop.&lt;/p&gt;

&lt;h3 id=&quot;pelusa&quot;&gt;&lt;a href=&quot;https://github.com/codegram/pelusa&quot;&gt;Pelusa&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Este projeto é semelhante ao Ruby-lint, mas não consegui executar. Ele só funciona com &lt;a href=&quot;http://rubini.us/&quot;&gt;Rubinius&lt;/a&gt;, mas é incompatível com a versão mais recente (apesar de o projeto não informar quais são as versões compatíveis).&lt;/p&gt;

&lt;h3 id=&quot;flay&quot;&gt;&lt;a href=&quot;https://github.com/seattlerb/flay&quot;&gt;Flay&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Esta ferramenta procura similaridades na estrutura do seu código (ex: dois métodos que possuem o código muito semelhante).&lt;/p&gt;

&lt;h3 id=&quot;saikuro&quot;&gt;&lt;a href=&quot;https://github.com/metricfu/Saikuro&quot;&gt;Saikuro&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Gera métricas de complexidade ciclomática.&lt;/p&gt;

&lt;h3 id=&quot;laser-e-nitpick&quot;&gt;&lt;a href=&quot;https://github.com/michaeledgar/laser&quot;&gt;Laser&lt;/a&gt; e &lt;a href=&quot;https://github.com/kevinclark/nitpick&quot;&gt;Nitpick&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Mais duas ferramentas semelhantes ao Ruby-lint. Como estão há anos sem atualizações, nem testei.&lt;/p&gt;

&lt;h2 id=&quot;resumindo&quot;&gt;Resumindo…&lt;/h2&gt;

&lt;p&gt;Entre as ferramentas que testei, gostei mais do Brakeman, do RuboCop e do RubyCritic. Acredito que são complementares, e, se usadas em conjunto, ajudam bastante a encontrar falhas de segurança, os pontos mais complexos do seu código e a seguir boas práticas de programação Ruby.&lt;/p&gt;

&lt;p&gt;Além destas 3, também gosto do Metric Fu, mas por executar muitas ferramentas, acho que ele gera informação demais. Usando ferramentas e métricas em excesso, geramos tanta informação que acabamos ignorando-as. Por isso, preferi focar nas 3 ferramentas que citei, pois já consigo ter um panorama bem completo do status do meu código com elas.&lt;/p&gt;

&lt;h2 id=&quot;usando-na-prática&quot;&gt;Usando na prática&lt;/h2&gt;

&lt;p&gt;Todas as ferramentas que testei são executadas via linha de comando e geram como saída algum tipo de relatório. Apesar de poder executá-las manualmente, na minha opinião, é mais interessante executá-las no servidor de integração contínua (CI). Desta forma, garantimos que essas ferramentas serão executadas com frequencia, e todo o time tem acesso aos relatórios gerados, assim como gráficos de evolução a cada execução. Com isso, podemos analisar se a “saúde” do projeto está melhorando ou piorando, basta acompanhar se o número de warnings de uma determinada ferramenta estão aumentando ou diminuindo.&lt;/p&gt;

&lt;p&gt;No caso específico do RuboCop, como ele analisa o uso de boas práticas de programação, acho mais útil executar no editor, pois, ao salvar um arquivo, tenho a resposta imediata, e posso fazer os ajustes no mesmo momento. Mas isso é uma questão de preferência.&lt;/p&gt;

&lt;h3 id=&quot;executando-no-jenkins&quot;&gt;Executando no Jenkins&lt;/h3&gt;

&lt;p&gt;Como uso o &lt;a href=&quot;http://jenkins-ci.org/&quot;&gt;Jenkins&lt;/a&gt;, descrevi como configurar as ferramentas acima neste servidor. O processo é bem simples, acredito que não seja difícil reproduzí-lo em outros servidores de integração contínua.&lt;/p&gt;

&lt;p&gt;Cada ferramenta pode ser executada por um job à parte ou no mesmo job que roda o build e os testes do projeto. Optei pela 1a opção, por 2 motivos:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;O tempo de execução de cada ferramenta de análise de código pode ser razoavelmente longo, o que deixaria o job de build muito lento&lt;/li&gt;
  &lt;li&gt;Caso ocorra algum problema na execução de alguma destas ferramentas, não quero que o job de build do meu projeto apareça quebrado no CI. Se o build e todos os testes foram executados com sucesso, o job que executa o build deve ter sucesso&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Além disso, optei por instalar as gems diretamente no CI. Desta forma, além de não precisar configurá-las no Gemfile do projeto, garanto que as gems estarão sempre atualizadas, o que é muito importante, principalmente no caso do Brakeman, pois novas falhas de segurança são encontradas diariamente.&lt;/p&gt;

&lt;h4 id=&quot;criando-um-novo-job&quot;&gt;Criando um novo job&lt;/h4&gt;

&lt;p&gt;Ao criar um novo job no Jenkins, você pode configurá-lo para executar automaticamente após cada build do projeto ou para executar periodicamente. Apesar de a primeira opção garantir que os relatórios de análise de código estarão atualizados a cada build, a execução é um pouco demorada. Além disso, achei que um relatório por dia seria suficiente para acompanhar o status do projeto. Sendo assim, configurei o job de relatórios para executar diariamente, de segunda a sexta-feira. Para isso, na configuração do job, basta selecionar a opção &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Build periodically&lt;/code&gt;, e no campo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Schedule&lt;/code&gt;, digitar &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;H 0 * * 1-5&lt;/code&gt;, por exemplo (o formato é o mesmo usado no &lt;a href=&quot;http://pt.wikipedia.org/wiki/Crontab&quot;&gt;crontab&lt;/a&gt;). Este valor configura o job para ser executado em qualquer minuto da hora zero, em qualquer dia do mês, todos os meses, de segunda a sexta-feira (dias 1 a 5).&lt;/p&gt;

&lt;p&gt;Eu optei por criar um único job para executar todas as ferramentas, pois desta forma, tenho todos os relatórios centralizados num único local. A principal desvantagem é que, desta forma, um erro na execução de uma ferramenta fará o job encerrar com status de erro, e as ferramentas seguintes não serão executadas.&lt;/p&gt;

&lt;p&gt;Para configurar cada ferramenta dentro do job, o processo é o mesmo:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Selecionar em &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Build&lt;/code&gt; a opção &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Execute shell&lt;/code&gt;, com os comandos para instalar e executar a gem&lt;/li&gt;
  &lt;li&gt;Adicionar uma &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Post-build Action&lt;/code&gt; para exibir os resultados&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A configuração de cada ferramenta é a seguinte:&lt;/p&gt;

&lt;h4 id=&quot;brakeman-1&quot;&gt;Brakeman&lt;/h4&gt;

&lt;p&gt;Configure a execução da ferramenta digitando os seguintes comandos no campo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Execute shell&lt;/code&gt; da configuração do job:&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;mkdir&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; tmp
gem &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;brakeman &lt;span class=&quot;nt&quot;&gt;--no-ri&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--no-rdoc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; brakeman &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; tmp/brakeman-output.tabs &lt;span class=&quot;nt&quot;&gt;--no-progress&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--separate-models&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--quiet&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Para visualizar os resultados, instale o &lt;a href=&quot;https://wiki.jenkins-ci.org/display/JENKINS/Brakeman+Plugin&quot;&gt;Brakeman Plugin&lt;/a&gt; no Jenkins e selecione em &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Post-build Actions&lt;/code&gt; a opção &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Publish Brakeman warnings&lt;/code&gt;. Em &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Brakeman Output File&lt;/code&gt;, digite &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tmp/brakeman-output.tabs&lt;/code&gt;.&lt;/p&gt;

&lt;h4 id=&quot;rubycritic-1&quot;&gt;RubyCritic&lt;/h4&gt;

&lt;p&gt;Adicione o seguinte comando no campo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Execute shell&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gem &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;rubycritic &lt;span class=&quot;nt&quot;&gt;--no-ri&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--no-rdoc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; rubycritic app lib
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Para visualizar os resultados, é necessário o plugin &lt;a href=&quot;https://wiki.jenkins-ci.org/display/JENKINS/HTML+Publisher+Plugin&quot;&gt;HTML Publisher&lt;/a&gt;. Após instalá-lo, selecione em &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Post-build Actions&lt;/code&gt; a opção &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Publish HTML reports&lt;/code&gt; e digite os seguintes valores:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;HTML directory to archive:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tmp/rubycritic&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Index page[s]:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;overview.html&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Report title:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RubyCritic Reports&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;rubocop-1&quot;&gt;RuboCop&lt;/h4&gt;

&lt;p&gt;Digite os comandos abaixo no campo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Execute shell&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;mkdir&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; tmp
gem &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;rubocop &lt;span class=&quot;nt&quot;&gt;--no-ri&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--no-rdoc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; rubocop &lt;span class=&quot;nt&quot;&gt;--fail-level&lt;/span&gt; E &lt;span class=&quot;nt&quot;&gt;--rails&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--out&lt;/span&gt; tmp/rubocop.out app lib spec
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;O plugin para exibir o resultado desta ferramenta é o &lt;a href=&quot;https://wiki.jenkins-ci.org/display/JENKINS/Warnings+Plugin&quot;&gt;Warnings&lt;/a&gt;. Após instalá-lo, é necessário configurar um parser para os warnings do RuboCop. Vá até a configuração do Jenkins (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Manage Jenkins&lt;/code&gt; -&amp;gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Configure System&lt;/code&gt;). Em &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Compiler Warnings&lt;/code&gt;, adicione um novo parser com os seguintes valores:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Name:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RuboCop&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Link name:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RuboCop&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Trend report name:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RuboCop Warnings&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Regular Expression:&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;^([^:]+):(\d+):\d+: ([^:]): ([^:]+)$
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Mapping Script:&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;import hudson.plugins.warnings.parser.Warning

String fileName = matcher.group(1)
String lineNumber = matcher.group(2)
String category = matcher.group(3)
String message = matcher.group(4)

return new Warning(fileName, Integer.parseInt(lineNumber), &quot;RuboCop Warnings&quot;, category, message);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Example Log Message:&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;attributes/default.rb:21:78: C: Use %r only for regular expressions matching more than 1 '/' character.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Após salvar esta configuração, volte até a configuração do job e selecione em &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Post-build Actions&lt;/code&gt; a opção &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Scan for compiler warnings&lt;/code&gt;. Em &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;File pattern&lt;/code&gt; digite &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tmp/rubocop.out&lt;/code&gt;, e no campo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Parser&lt;/code&gt;, selecione o parser recém-criado, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RuboCop&lt;/code&gt;.&lt;/p&gt;
</description>
        <pubDate>Thu, 06 Nov 2014 00:00:00 +0000</pubDate>
      </item>
    
  </channel>
</rss>
