<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Bruce Pomeroy on Medium]]></title>
        <description><![CDATA[Stories by Bruce Pomeroy on Medium]]></description>
        <link>https://medium.com/@brucepomeroy?source=rss-95fe8640ab9f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*rskYX6je7MpIjrgHk8GEbg@2x.jpeg</url>
            <title>Stories by Bruce Pomeroy on Medium</title>
            <link>https://medium.com/@brucepomeroy?source=rss-95fe8640ab9f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 16:10:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@brucepomeroy/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Troubleshooting Disc Space Usage with Elixir and Edeliver]]></title>
            <link>https://medium.com/@brucepomeroy/troubleshooting-disc-space-usage-with-elixir-and-edeliver-8a93d9954908?source=rss-95fe8640ab9f------2</link>
            <guid isPermaLink="false">https://medium.com/p/8a93d9954908</guid>
            <category><![CDATA[edeliver]]></category>
            <category><![CDATA[elixir]]></category>
            <dc:creator><![CDATA[Bruce Pomeroy]]></dc:creator>
            <pubDate>Mon, 29 Jul 2019 22:05:45 GMT</pubDate>
            <atom:updated>2019-07-29T22:07:10.099Z</atom:updated>
            <content:encoded><![CDATA[<p>These commands will help you determine if you are out of disc space:</p><pre># Check if we&#39;re out of disc space:<br>df<br># Check if we&#39;re out of inodes:<br>df -i</pre><p>Here is a command to help you find large folders, tweak the depth and starting point as needed:</p><pre>sudo du -cha --max-depth=4 / | grep -E &quot;M|G&quot;</pre><p>Here are some directories that may accumulate files:</p><pre>/&lt;deployment_root&gt;/my_app/lib/<br>/&lt;deployment_root&gt;/my_app/releases/<br>/&lt;deployment_root&gt;/my_app/var/log/</pre><p>You may also find that OS updates are consuming disc space, often they will leave large numbers of small files and hit inode limits without consuming all disc space. If this is the case check out <a href="https://medium.com/@brucepomeroy/running-out-of-inodes-on-ubuntu-718aef71ac16">this article</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8a93d9954908" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Run Sentry Error Monitoring on EC2]]></title>
            <link>https://medium.com/@brucepomeroy/run-sentry-error-monitoring-on-ec2-2cbafc302dca?source=rss-95fe8640ab9f------2</link>
            <guid isPermaLink="false">https://medium.com/p/2cbafc302dca</guid>
            <category><![CDATA[nginx]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[sentryio]]></category>
            <dc:creator><![CDATA[Bruce Pomeroy]]></dc:creator>
            <pubDate>Tue, 05 Mar 2019 23:48:10 GMT</pubDate>
            <atom:updated>2019-03-05T23:54:59.430Z</atom:updated>
            <content:encoded><![CDATA[<p>First of all, I highly recommend the <a href="https://sentry.io/">hosted version of Sentry</a>. I’ve used this extensively and found it to be extremely reliable and affordable and it’s certainly the simplest way to get up and running with Sentry. With that said, if you want to host it yourself, here’s one approach.</p><p>From your EC2 dashboard click “Launch Instance”. On the AMIs page search for Sentry Error Tracker, click “Select”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cVJo-nKV7kOyj2iB9e-vJQ.png" /><figcaption>Select this AMI by Momate</figcaption></figure><p>This will launch an Ubuntu instance with Sentry listening locally on port 9000 and Nginx externally accessible on port 80. Nginx is setup as a reverse proxy to relay requests to Sentry.</p><p>Obviously the instance size you choose when launching the image will affect your capacity. Sentry will run on even the smallest t2.nano which will cost you under $5/month.</p><p>When you get to configuring the security group for the instance leave the SSH access as-is and add rules to allow access form anywhere on ports 80 and 443. Like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Bo2KjG_UUbY_uEfokEeosQ.png" /><figcaption>Open ports 80 and 443 in addition to port 22 which will be open by default</figcaption></figure><p>When the instance finishes launching, select it in your EC2 instances list and make a note of the instance id and the public DNS, I underlined them in Green in the image below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/904/1*hWL7HxLmIo3XK_TC-n_0mg.png" /><figcaption>Make a note of the instance id and the public DNS of the new image</figcaption></figure><p>Go to the public DNS in your browser, you should see the Sentry login page. At this stage you <em>could</em> login, using the account admin and use the instance id that you noted earlier as the password. You probably want to give it a friendly domain name and setup ssl first though.</p><p>First let’s setup a domain name, in this case we’ll use a subdomain. Something like sentry.myapp.com. Assuming you have access to manage the DNS for myapp.com, you need to create an A record from sentry to the public ip of the instance you just launched. You should now be able to get to your Sentry login page by browsing to <a href="http://sentry.myapp.com./">http://sentry.myapp.com.</a> Make sure you can before proceeding.</p><p>Next we need to secure sentry with SSL so it can be accesses using https. The AMI we used already has Nginx setup but it doesn’t have ssl setup obviously. SSL needs to be setup on a per-domain basis. Certbot can issue free ssl certs and it will even integrate them with Nginx.</p><p>SSH into the sentry instance as the user ubunutu then run these commands, you may need to hit ENTER or y to get through some steps:</p><pre>sudo vi /home/ubuntu/.sentry/nginx.conf</pre><p>Now edit the file, set:</p><pre>server_name &quot;sentry.myapp.com&quot;</pre><p>This will help Certbot understand which part of your Nginx conf it should add the ssl cert to. Now use Certbot to issue an ssl cert and add it to your Nginx conf.</p><pre>sudo add-apt-repository ppa:certbot/certbot<br>sudo apt install python-certbot-nginx<br>sudo certbot --nginx -d sentry.myapp.com</pre><p>Now edit /home/ubuntu/.sentry/nginx.conf again and comment out `listen 80;`, we needed that before so Certbot could access our server to verify the domain but we don’t need it anymore. Now restart nginx:</p><pre>sudo service nginx restart</pre><p>Now if you visit <a href="http://sentry.myapp.com/">http://sentry.myapp.com</a> in your browser you should be redirected to the https version and you should see a valid certificate if you click the lock icon in your browser.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*blsmiWrUKbXMzvyRhCtY2g.png" /><figcaption>Sentry login page, now secured by ssl. Login with “admin” and your instance id</figcaption></figure><p>After you login, you’ll be prompted for a Root URL, enter <a href="https://sentry.brucepomeroy.com/">https://sentry.myapp.com</a> (make sure to start it with https).</p><p>There’s one more thing. Sentry needs to be able to send emails so it’s going to need smtp server info edit the following file</p><pre>sudo vi /home/ubuntu/.sentry/config.yml</pre><p>…and change mail.backend from dummy to smtp, then uncomment the lines below to add smtp info. If you’re using AWS SES to send mail it would look something like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1016/1*s3jTYBZELZfk3zachXBccg.png" /></figure><p>If you’re using gmail it would be something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*x6wp9ZdXkaGssSnGfg4jLw.png" /></figure><p>Now when you’re logged in to Sentry, got to User Account &gt; Emails and add your real email as a new email, check your inbox and follow the instructions to confirm your email. Now make your real email the primary email for that account and delete the admin email. You’ll want to set the password too, at this stage the password is still the instance id. You also want to make sure password reset is working, you don’t want to get locked out if you lose your password. You can reset your password and test password reset, by logging out , then clicking the forgot password link on the login page.</p><p>I think that should be about it! Let me know if I missed anything.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2cbafc302dca" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using Xref to 10x Dev Compile Speed]]></title>
            <link>https://medium.com/@brucepomeroy/using-xref-to-10x-dev-compile-speed-b152b70b2fdc?source=rss-95fe8640ab9f------2</link>
            <guid isPermaLink="false">https://medium.com/p/b152b70b2fdc</guid>
            <category><![CDATA[phoenix-framework]]></category>
            <category><![CDATA[elixir]]></category>
            <dc:creator><![CDATA[Bruce Pomeroy]]></dc:creator>
            <pubDate>Thu, 04 Oct 2018 17:03:45 GMT</pubDate>
            <atom:updated>2018-10-04T17:06:20.980Z</atom:updated>
            <content:encoded><![CDATA[<h3>Using Elixir’s Xref to 10x Dev Compile Speed</h3><p>I was making some changes in a view file. Phoenix was doing its thing and whenever I hit save it was recompiling and then reloading the page in my browser. Nice! Only thing was it was taking almost 10 seconds from the time I hit save until the time I saw the page reload in my browser.</p><p>I looked in the Elixir console and when I hit save, I saw the following:</p><pre>[debug] Live reload: lib/spruce_web/views/admin/timeable_view.ex<br>Compiling <strong>122 files</strong> (.ex)</pre><p>What? 122 files is a lot of recompiling to change the label on a form field. Hmm. I wanted to have a simple and consistent way to reproduce.</p><pre>$ mix compile<br>$ mix compile<br>$ touch lib/spruce_web/views/admin/timeable_view.ex<br>$ mix compile<br>Compiling 122 files (.ex)</pre><p>Yup sure enough, every time I was getting the same results. Touching one file caused exactly 122 files to be recompiled.</p><p>So why were so many files being recompiled, and ultimately why was I waiting 10 seconds to see my changes in the browser?</p><p>There was nothing special about that particular file, but it was a good starting point and I suspected if I improved things for that file it would likely improve many other files.</p><p>Let’s see which files depend on the file I was modifying:</p><pre>mix xref graph --sink lib/spruce_web/views/admin/timeable_view.ex --format stats</pre><pre># Abridged output:<br>...<br>Top 10 files with most incoming dependencies:<br><strong>* lib/spruce_web/router.ex (121)</strong><br>...</pre><p>By far the dependant file with the most incoming dependancies was the router. I wasn’t surprised that the router had a lot of dependancies, but I was surprised that the router depended on the view I was editing. I don’t want the router to be recompiled whenever I edit a view. I needed to find out why the router was being compiled in response to view changes. I found the clearest way was to analyze dependancies from the router’s point of view. I should expect to see the view I was editing in the router’s dependancy graph along with an explanation of why:</p><pre>mix xref graph --source lib/spruce_web/router.ex</pre><p>After running the above command I searched the output for the view file I’d been editing. When I found it I looked at it’s parent in the dependancy graph. I found it’s parent was `lib/spruce_web/views/layout_view.ex` which was a direct child of the router. Hmm, is `layout_view.ex` the culprit?</p><p>Let’s look at this another way, Xref can generate an image:</p><pre>mix xref graph --sink lib/spruce_web/views/admin/timeable_view.ex --format dot<br>dot -Tpng xref_graph.dot -o xref_graph.png</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/513/1*oPTINLXo7bWJdZ_hClGvxQ.png" /></figure><p>Here’s part of the generated image. The router has a lot of incoming dependancies, that makes sense and may be inevitable. If we modify the router a lot of files need to be recompiled, I’m ok with that. But there’s one arrow going the other way, the router’s only outgoing arrow. Again this indicated that layout_view.ex was likely the “edge” between the router and the view I was editing.</p><p>Inspecting my router I found the cause of this dependancy. I referenced the layout in a plug. Plugs are initialized at compile time for runtime performance so this type of reference causes a compile-time dependancy.</p><pre>pipeline :admin do<br>  plug(:put_layout, {SpruceWeb.LayoutView, :admin})<br>end</pre><p>After removing this reference from my router I began to see live reload working in under a second instead of almost 10 seconds. Much better!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b152b70b2fdc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Running Phoenix with a Self-Signed SSL Cert in Dev]]></title>
            <link>https://medium.com/@brucepomeroy/running-phoenix-with-a-self-signed-ssl-cert-in-dev-acec3e20178e?source=rss-95fe8640ab9f------2</link>
            <guid isPermaLink="false">https://medium.com/p/acec3e20178e</guid>
            <category><![CDATA[ssl]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[phoenix-framework]]></category>
            <dc:creator><![CDATA[Bruce Pomeroy]]></dc:creator>
            <pubDate>Thu, 28 Jun 2018 00:07:46 GMT</pubDate>
            <atom:updated>2018-06-28T00:10:15.793Z</atom:updated>
            <content:encoded><![CDATA[<p>First off, I’m assuming you’re not going to do this out of concern for security. This is just a quick way to run via ssl locally in dev. You may need to do this if you’re embedding a third party widget that requires ssl or using a browser feature, like location services that requires ssl. For example if you’re trying to use Chrome’s geolocation features in dev. I’m assuming OS X but the steps will likely be similar on Linux or Windows.</p><p><strong>Generate a self-signed ssl certificate</strong><br>In the directory of your choice run the following command to generate a self signed cert and key:<br>openssl req -new -x509 -nodes -out dev.crt -keyout dev.key<br>Hit enter repeatedly to skip through all the questions. Afterwards you’ll see two new files in your current directory, dev.crt and dev.key. Note the absolute paths to these two files.</p><p><strong>Adjust your Phoenix endpoint configuration</strong><br>Adjust your Phoenix endpoint configuration to use https and to reference the files you just created. You likely have a line simlar to this in config/dev.exs:<br>http: [port: 4000]<br>Replace it with:</p><pre>https: [<br>  port: 4000,<br>  keyfile: &quot;/.../dev.key&quot;,<br>  certfile: &quot;/.../dev.crt&quot;<br>]</pre><p>Note that we replaced the http key with https. The keyfile and certfile values should be the paths to the self signed cert files you generated earlier.</p><p><strong>Try it out!</strong><br>Restart your phoenix server. You should see something like this in the output:<br>[info] Running MyApp.Endpoint with Cowboy using <a href="https://0.0.0.0:4000/"><strong>https</strong></a><a href="https://0.0.0.0:4000/">://0.0.0.0:4000</a>. Now browse to <a href="https://localhost:4000/"><strong>https</strong>://localhost:4000</a> or whatever url you usually use, only change is that we start with https://.</p><p>Because you signed the cert yourself, your browser will warn you that the certificate is not signed by a valid authority. Just click “Advanced” Then “Proceed to x (Unsafe)”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/1*ok2YuPftdxO1mTz1m1U0hw.png" /></figure><p>You’ll probably get an angry message in the address bar of your browser but clientside features that require ssl should now work.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=acec3e20178e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Publishing Metrics to Prometheus from Elixir]]></title>
            <link>https://medium.com/@brucepomeroy/publishing-metrics-to-prometheus-from-elixir-bb70efcd6ec1?source=rss-95fe8640ab9f------2</link>
            <guid isPermaLink="false">https://medium.com/p/bb70efcd6ec1</guid>
            <category><![CDATA[prometheus]]></category>
            <category><![CDATA[elixir]]></category>
            <dc:creator><![CDATA[Bruce Pomeroy]]></dc:creator>
            <pubDate>Thu, 24 May 2018 23:02:23 GMT</pubDate>
            <atom:updated>2018-06-28T18:37:54.263Z</atom:updated>
            <content:encoded><![CDATA[<p>I think this is the most basic setup to get you up and running publishing custom metrics from your Elixir app to <a href="https://prometheus.io/">Prometheus</a>. By contrast Sebastian Kosch has published a <a href="https://aldusleaf.org/2016-09-30-monitoring-elixir-apps-in-2016-prometheus-and-grafana.html">much more comprehensive guide</a> that includes instrumentation of Phoenix, Ecto and the Erlang VM.</p><p>Note, this example is for a Phoenix app, however I’m not instrumenting Phoenix itself. The only think that’s Phoenix-specific is the way we add our /metrics endpoint to plug pipeline.</p><p>Install the dependancies:</p><pre># In mix.exs<br>{:prometheus_ex, &quot;~&gt; 1.0&quot;},<br>{:prometheus_plugs, &quot;~&gt; 1.0&quot;},</pre><pre>$ mix deps.get</pre><p>Create an exporter module, this takes care of making the metrics you collect in your app, available to prometheus at the /metrics endpoint:</p><pre># Create a file like this, you can put it anywhere in lib/<br>defmodule MyApp.Metrics.PrometheusExporter do<br>  use Prometheus.PlugExporter<br>end</pre><p>Add the module you just created to the plug pipeline before the Phoenix router:</p><pre># lib/my_app_web/endpoint.ex or wherever your Phoenix Endpoint module lives<br># Add this line before the router plug:<br>plug MyApp.Metrics.PrometheusExporter</pre><p>Initialize the exporter in your apps start function:</p><pre>defmodule MyApp.Application do<br>  ...<br>  def start(_type, _args) do<br>    MyApp.Metrics.PrometheusExporter.setup()<br>    ...<br>  end<br>end</pre><p>If you run your Phoenix app now you should be able to browser to /metrics , that’s the url that Prometheus will scrape. You will see a bunch of metrics relating to the Erlang VM which is cool but I think what’s really fun is to publish info about events and values that are specific to our application’s domain. Let’s do that now, we’re going to create a counter but there are other patterns for collecting metrics. Check out the Prometheus.Metrics.* modules <a href="/service/ttps://hexdocs.pm/prometheus_ex/Prometheus.html">here</a>.</p><p>Let’s create a counter module. In my case we have a bunch of devices that report “tag time” events to our Phoenix server via MQTT. We want to record information about the number of these events reported and be able to segment the data by which device them.</p><pre>defmodule MyApp.Metrics.TagTimesInstrumenter do<br>  use Prometheus.Metric<br>  def setup() do<br>    Counter.declare(<br>      name: :tag_times_total,<br>      help: &quot;Total tag times&quot;,<br>      labels: [:device]<br>    )<br>  end</pre><pre>  def inc(device) do<br>    Counter.inc(<br>      name: :tag_times_total,<br>      labels: [device]<br>    )<br>  end<br>end</pre><p>You’ll need to call the setup function of this new module when your app starts. Something like this:</p><pre># lib/my_app_web/endpoint.ex or wherever your Phoenix Endpoint module lives<br># Add this line before the router plug:<br>plug MyApp.Metrics.PrometheusExporter # &lt;- this is the line we added earlier<br>MyApp.Metrics.TagTimesInstrumenter.setup()</pre><p>You could Now, whenever we receive a tag time event we can record it by calling TagTimesInstrumenter.inc/1 passing in the name/id of the device that reported the event.</p><p>After some events have come in go browse to /metrics again and look for data relating to your new custom metric.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bb70efcd6ec1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Running out of inodes on Ubuntu]]></title>
            <link>https://medium.com/@brucepomeroy/running-out-of-inodes-on-ubuntu-718aef71ac16?source=rss-95fe8640ab9f------2</link>
            <guid isPermaLink="false">https://medium.com/p/718aef71ac16</guid>
            <category><![CDATA[ubuntu]]></category>
            <dc:creator><![CDATA[Bruce Pomeroy]]></dc:creator>
            <pubDate>Thu, 21 Sep 2017 21:46:44 GMT</pubDate>
            <atom:updated>2019-04-08T17:45:44.167Z</atom:updated>
            <content:encoded><![CDATA[<p>So you’re seeing errors on Ubuntu indicating you’re low on disc space. First step of course is to see if the disc is actually full with df you should see something like this:</p><pre>Filesystem     1K-blocks    Used Available Use% Mounted on</pre><pre>udev              497828       0    497828   0% /dev</pre><pre>tmpfs             101424    4324     97100   5% /run</pre><pre>/dev/xvda1       8117828 2280780   5401772  30% /</pre><p>Check out the Use% column, if you see 100% anywhere in that column then that disc is simply full and you need to delete some files.</p><p>If you don’t see 100% then there must be another reason for the out of disc space errors. Let’s check the inodes with df -i the output will be similar to the previous example but this time we’re seeing inode usage. 100% inode usage can cause disc space errors even if the file system is far from full.</p><p>Here is a command to find the probblem:</p><pre>sudo find . -xdev -type f | cut -d &quot;/&quot; -f 2 | sort | uniq -c | sort -n</pre><p>If you see 100% inode usage then that’s likely the problem. A really common thing on Ubuntu is old kernel header files eating up inodes to the point where the system reaches it’s limit.</p><p>Let’s see how many kernel versions we have headers for:</p><pre>for i in /usr/src/*; do echo $i; find $i |wc -l; done</pre><p>It’s fine if there are two or three versions listed but if you’re seeing 10 or more versions this is quite likely the problem. Here’s how we can remove old kernel headers:</p><pre>sudo apt-get -f install<br>sudo apt autoremove</pre><p>If either command fails with a disc space error you may need to manually delete a couple of the oldest header file directories that were listed initially.</p><p>Now run the previous command again to list header files, this time you should only see one or two versions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=718aef71ac16" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Specifying primary key values with Ecto]]></title>
            <link>https://medium.com/@brucepomeroy/specifying-primary-key-values-with-ecto-57405b7ecbc5?source=rss-95fe8640ab9f------2</link>
            <guid isPermaLink="false">https://medium.com/p/57405b7ecbc5</guid>
            <category><![CDATA[elixir]]></category>
            <dc:creator><![CDATA[Bruce Pomeroy]]></dc:creator>
            <pubDate>Thu, 08 Jun 2017 00:13:22 GMT</pubDate>
            <atom:updated>2017-06-08T00:13:22.030Z</atom:updated>
            <content:encoded><![CDATA[<p>There are some cases where we have an entity stored in a GenServer or ETS table that already has a unique id and now we want to put it in the database. If we want to we can use the id we already have for it as the primary key. Of course this doesn’t make sense with an auto-incrementing integer id, you’ll need to use a string or binary_id primary key.</p><p>To create a new table:</p><pre>defmodule MyApp.MyMigration do<br>  use Ecto.Migration</pre><pre>  def change do<br>    create table(:companies, primary_key: false) do<br>      add :id, :uuid, primary_key: true<br>      add :title, :string<br>      timestamps()<br>    end<br>  end<br>end</pre><p>Or to alter an existing table that currently uses in integer primary key (you can’t do this if you already have data in there:</p><pre>def change do<br>    alter table(:companies) do<br>      remove :id # remove the existing id column<br>      add :id, :uuid, primary_key: true<br>    end<br>  end</pre><p>Next we need to annotate our schema to get the binary_id primary key working:</p><pre>defmodule MyApp.Company do<br>  use Ecto.Schema</pre><pre><a href="http://twitter.com/primary_key">  @primary_key</a> {:id, :binary_id, autogenerate: true}<br>  <a href="http://twitter.com/derive">@derive</a> {Phoenix.Param, key: :id}<br>  schema &quot;companies&quot; do<br>    field :title, :string<br>    timestamps()<br>  end<br>end</pre><p>Now, when we insert a record we can assign it an id first. For example:</p><pre>company = %Company{id: Ecto.UUID.generate(), title: &quot;Acme&quot;}</pre><pre>Repo.insert!(company)</pre><p>That way we could create an item with a unique id, store it in a GenServer or ets table, reference it by its id. Later if we want to save it to the database we can just insert it and the database will us the specified id as the primary key.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=57405b7ecbc5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Use Ecto changesets without a Repo]]></title>
            <link>https://medium.com/@brucepomeroy/use-ecto-changesets-without-a-repo-86e035233637?source=rss-95fe8640ab9f------2</link>
            <guid isPermaLink="false">https://medium.com/p/86e035233637</guid>
            <category><![CDATA[elixir]]></category>
            <dc:creator><![CDATA[Bruce Pomeroy]]></dc:creator>
            <pubDate>Tue, 30 May 2017 01:04:18 GMT</pubDate>
            <atom:updated>2017-05-30T01:25:27.623Z</atom:updated>
            <content:encoded><![CDATA[<p>An Ecto changeset describes an initial state along with a set of desired changes.</p><p>Here’s a typical usage of a changeset:</p><pre>def update_user(%User{} = user, attrs) do<br>  user<br>  |&gt; User.changeset(attrs)<br>  |&gt; Repo.update()<br>end</pre><pre>update_user(my_user, name: &quot;John&quot;)</pre><p>The goal here is of course to take an existing User and change his name to “John”. In this example we’ve also updated the database record. What if we only want to update the User struct? The answer in Ecto 2.1 is <a href="https://hexdocs.pm/ecto/Ecto.Changeset.html#apply_changes/1">https://hexdocs.pm/ecto/Ecto.Changeset.html#apply_changes/1</a>.</p><pre>def update_user(%User{} = user, attrs) do<br>  changeset =<br>   user<br>   |&gt; CdpRevision.changeset(attrs)<br>      <br> if changeset.valid? do<br>   {:ok, apply_changes(changeset)}<br> else<br>   {:error, changeset}<br> end<br>end</pre><p>We start off basically the same as before by using the existing User struct along with the attributes we want to change to create a changeset. This time we don’t want to invoke the Repo though. Ecto.ChangeSet.apply_changes/1 will return a struct with the changes applied. Unlike Repo functions though it won’t fail if the changeset is invalid.</p><p>Ecto 2.2 will bring Ecto.ChangeSet.apply_action/2 which will do something very similar to what we’ve done above, applying the changes if the changeset is valid. When you update to Ecto 2.2 you should be able to write it like this:</p><pre><strong>ECTO 2.2!</strong><br>def update_user(%User{} = user, attrs) do<br>  changeset =<br>   user<br>   |&gt; CdpRevision.changeset(attrs)<br>   |&gt; apply_action(:update)<br>end</pre><p>If you’re interested in Ecto 2.2’s apply_action/2 check out this link:</p><p><a href="https://github.com/elixir-ecto/ecto/pull/1929/files">https://github.com/elixir-ecto/ecto/pull/1929/files</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=86e035233637" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Accepting optional options in Elixir]]></title>
            <link>https://medium.com/@brucepomeroy/accepting-optional-options-in-elixir-65e7eaed11ac?source=rss-95fe8640ab9f------2</link>
            <guid isPermaLink="false">https://medium.com/p/65e7eaed11ac</guid>
            <category><![CDATA[elixir]]></category>
            <dc:creator><![CDATA[Bruce Pomeroy]]></dc:creator>
            <pubDate>Fri, 26 May 2017 23:28:10 GMT</pubDate>
            <atom:updated>2017-05-26T23:29:08.466Z</atom:updated>
            <content:encoded><![CDATA[<p>It’s a common pattern to write a function where the last parameter is a set of options that are all optional. Typically you also want to have default values. It’s conventional in Elixir to use a keyword list as this last parameter. I’m not going to discuss the merit of this, in some cases a map might make more sense but I think keyword lists are so conventional for this that I’d be hesitant to stray from them for fear of causing confusion. There‘s an interesting issues here: you can of course pattern-match on keyword lists to extract parameters but the order of the list matters. If someone calls the function with [color: &quot;red&quot;, shape: &quot;square&quot;] instead of [shape: &quot;square&quot;, color: &quot;red&quot;] you won’t get a match.</p><p>Below we accept an options argument and set options to an empty list if it’s not provided. Then we use Keyword.get to pull out the keys wwe’re interested in if they exist, the third parameter we pass to Keyword.get/3 is a default value to use if the key’s not found. No pattern matching used here. Nice and explicit but could get tedious if there are lots of options.</p><pre>@default_color &quot;black<br>@default_shape &quot;circle&quot;</pre><pre>def draw(options \\ [] ) do<br>  color = Keyword.get(options, :color, @default_color)<br>  shape = Keyword.get(options, :shape, @default_shape)<br>  IO.puts(&quot;Draw a #{color} #{shape}&quot;)<br>end</pre><p>Below is an alternative approach, we take advantage of pattern matching to extract values but we have to convert to a map first. We can create our default as a map then merge values from the keyword list passed in.</p><pre><a href="http://twitter.com/defaults">@defaults</a> %{color: &quot;black&quot;, shape: &quot;circle&quot;}</pre><pre>def draw(options \\ [] ) do<br>  %{color: color, shape: shape} = Enum.into(options, <a href="http://twitter.com/defaults">@defaults</a>)<br>  IO.puts(&quot;Draw a #{color} #{shape}&quot;)<br>end</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=65e7eaed11ac" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Troubleshooting Elixir crashes]]></title>
            <link>https://medium.com/@brucepomeroy/troubleshooting-elixir-crashes-50fbe80b459b?source=rss-95fe8640ab9f------2</link>
            <guid isPermaLink="false">https://medium.com/p/50fbe80b459b</guid>
            <category><![CDATA[elixir]]></category>
            <dc:creator><![CDATA[Bruce Pomeroy]]></dc:creator>
            <pubDate>Tue, 23 May 2017 21:47:45 GMT</pubDate>
            <atom:updated>2019-11-13T21:57:44.548Z</atom:updated>
            <content:encoded><![CDATA[<p>Elixir and Erlang have a well-earned reputation for stability and fault-tolerance. So I was surprised the first time I received a notification that my app was “down”. It turns out Elixir can’t protect you from yourself and I’ve found a couple of ways to bring down an Elixir app. Good news is, if you can diagnose the problem you can fix it pretty quick.</p><p>If you’re app just went down here’s how you can troubleshoot.</p><p>ssh in to your web server, cd into the directory you deployed your app to then run ls -l</p><pre>... Feb 28 04:44 bin<br><strong>... Apr  7 16:04 erl_crash.dump<br>... May 23 19:28 log</strong><br>... Feb 28 02:59 erts-8.2<br>... Feb 28 04:44 releases</pre><p>You’ll see the directory contents along with the modification date. Pay attention to erl_crash.dump and log. If the modification date on erl_crash.dump corresponds to the start of the outage (remember that the server’s clock may not be in your timezone) then erl_crash.dump will likely contain some useful info. There’s a guide here to interpretting the contents of this file: <a href="http://erlang.org/doc/apps/erts/crash_dump.html">http://erlang.org/doc/apps/erts/crash_dump.html</a>.</p><p>If the modification date of erl_crash.dump doesn’t correspond to the crash date then next stop is the logs, cd into log and use ls -l to find the log file with the most recent modification date then tail it with tail -f -n100 &lt;logfile name&gt;</p><p>Hopefully you found some useful info in erl_crash.dump or log/*.</p><p>One thing I’ve run into is that supervisors restart processes after they crash but by default they’ll only restart a process three times in a five second window. You may have tested in a controlled scenario that your supervisor will restart the process but in production you may get scenarios where it crashes too frequently and the supervisor gives up, if this is the case you’ll see a message like “Maximum restart intensity reached”. You can read more about “restart intensity” and how to change it here. Search for :max_restarts and :max_seconds.</p><p><a href="https://hexdocs.pm/elixir/Supervisor.Spec.html#supervise/2">Elixir v1.4.4 Supervisor.Spec</a></p><p>In general though if you’re having to increase the restart intensity of a supervisor it’s a bad sign. It can be ok to “let it crash” and rely on process supervision but it needs some careful thought. It only makes sense if restarting the process will resolve the problem. There are many cases where it won’t. For example, if your process crashes when a database is unreachable you have a problem. Restarting your process isn’t going to fix the database connection. On restart the database will probably still be down and the process will crash again until max restart intensity is reached. In some cases increasing restart intensity could be a stop-gap measure to keep your system running until you can resolve the issue but it’s not a good road to go down.</p><p>The erl_crash.dump or the log may yield some useful info but if not the Beam may have been killed externally.</p><p>Ubuntu and I expect other OS will kill processes when <em>the OS</em> begins running dangerously low on memory. A service called OOM Killer (Out of Memory Killer) will kill processes when memory becomes critically low. We can view the activity of OOM Killer with `tail -f -n100 /var/log/kern.log`</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/572/1*gT5Vz4HFcZU1T4CEB87p7g.png" /></figure><p>Here we can see an example of OOM Killer having killed the BEAM process. Note the last few lines:</p><pre>Jan 01 00:00:00 ip-0-0-0-0 kernel: [7250834.152637] Out of memory: Kill process 22397 (beam) score 81 or sacrifice child</pre><pre>Jan 01 00:00:00 ip-0-0-0-0 kernel: [7250834.157643] Killed process 22397 (beam) total-vm:2301796kB, anon-rss:166036kB, file-rss:0kB</pre><p>This doesn’t necessarily mean that BEAM is “to blame” for memory consumption. Every process consumes memory and it doesn’t really make sense to blame a specific process. There’s some method to how OOM Killer chooses it’s victim but that’s not really important. What is important is to keep memory usage in a reasonable range so OOM Killer never needs to step in.</p><p>In the log OOM Killer lists the OS processes along with their memory consumption at the moment it killed BEAM. Check out the rows highlighted in blue in the image above. There is a BEAM process that’s using substantial memory but that’s expected. What’s more suspect in this case is numerous convert (an ImageMagick tool) processes that are all using substantial amounts of memory. This bears further investigation, is it expected or necessary for that many ImageMagick processes to be running concurrently? This is just one example. Other processes can use a lot of memory, PhantomJS, Redis, RethinkDB, anything else you might be running on that server. Obviously it may make sense to run your app on a server with more memory or to move a memory-hungry process onto a separate machine, that’s up to you and what makes sense for your app.</p><p>OOM Killer uses SIGKILL to terminate the process, not SIGTERM. Unlike SIGTERM, SIGKILL kills the process without giving it the opportunity to respond. If you’re using PID files to monitor your process you may find your system isn’t notified of an OOM killer termination. You can simulate OOM killer with pkill -9 beam.smp If you’re using pidfiles, you’ll find they are still there even after you kill the process in this way.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=50fbe80b459b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>