<?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 Chirag Singh Toor on Medium]]></title>
        <description><![CDATA[Stories by Chirag Singh Toor on Medium]]></description>
        <link>https://medium.com/@chiragtoor?source=rss-a79f99b5e41c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Lxifhh_qJVx-rzEYyiOg7A.jpeg</url>
            <title>Stories by Chirag Singh Toor on Medium</title>
            <link>https://medium.com/@chiragtoor?source=rss-a79f99b5e41c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 12:36:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@chiragtoor/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[Elixir OTP applications on Kubernetes]]></title>
            <link>https://medium.com/dsc-engineering/elixir-otp-applications-on-kubernetes-9944636b8609?source=rss-a79f99b5e41c------2</link>
            <guid isPermaLink="false">https://medium.com/p/9944636b8609</guid>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[otp]]></category>
            <dc:creator><![CDATA[Chirag Singh Toor]]></dc:creator>
            <pubDate>Wed, 12 Dec 2018 21:47:27 GMT</pubDate>
            <atom:updated>2018-12-12T22:19:54.623Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="http://erlang.org/doc/index.html">OTP (Open Telecom Platform)</a> provides rock solid foundations for building concurrent and fault-tolerant applications in Elixir. By building on top of <a href="https://hexdocs.pm/elixir/GenServer.html">GenServers</a> and <a href="https://elixirschool.com/en/lessons/advanced/otp-supervisors/">Supervision trees</a> you not only get great performance, but also a modularized application. However, if you’re just starting to integrate Elixir into your stack your production infrastructure is probably not geared towards OTP applications out of the box.</p><p>For example, at Dollar Shave Club we use Elixir for a transactional messaging service (email, sms). When customer add items to their DSC plan we email them a summary, but to avoid blasting them with several emails we aggregate their add actions into a single delivery. Our use case fits well with GenServer and <a href="https://hexdocs.pm/elixir/DynamicSupervisor.html">DynamicSupervisors</a>. A GenServer could be spawned for each customer to keep track of their added products and handle the timer logic, but all that data would be stored in-memory. We deploy on Kubernetes, so as soon as any Pod is torn down that in-memory data is going to be blown away.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RB1mCmTDGGbi3RNQybEQJg.gif" /><figcaption>deployments in Kubernetes tear down Pods, so in-memory state is lost</figcaption></figure><p>We have been exploring how to better integrate OTP and Kubernetes and got some valuable insight at ElixirConf 2018. In this article we’ll discuss why we use Elixir/OTP, go through step-by-step setting up a demo Elixir application that deals with graceful Node shutdown, and then integrate it with Kubernetes using minikube.</p><p><em>This article assumes you have Elixir 1.7, minikube, and kubectl installed along with some basic understanding of Kubernets and OTP (GenServer, Supervisor, Registry).</em></p><h4>Why Elixir and OTP?</h4><p>Take the messaging service discussed above, OTP is a great fit for this as there is separation between inputs. Inputs are clearly split by customer, and so any issues processing one should not affect the other. On the Erlang VM (BEAM) a separate <a href="http://blog.plataformatec.com.br/2018/04/elixir-processes-and-this-thing-called-otp/">Process</a> can be used for each customer, providing concurrency and containing any errors processing one customer to only that one process. OTP provides out of the box utilities that build on top of these BEAM processes.</p><p>The options for working with OTP are Erlang and Elixir, and between the two we prefer Elixir syntax. Elixir also has a growing community, some fantastic tools (mix), and is overall a joy to work with (pattern matching, pipe operator).</p><h4>Demo application: ExCluster</h4><p>The demo application is a order handling service. Each order belongs to a customer and stores a list of integers as order contents.</p><p>Create a new application: mix new ex_cluster</p><p>Update mix.exs to setup an application callback module and start up a Supervision tree with a DynamicSupervisor for orders:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/86ffb22aaac9caff8ecfb880bca8f6f7/href">https://medium.com/media/86ffb22aaac9caff8ecfb880bca8f6f7/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e21076f6b556c84fa97719300859dae2/href">https://medium.com/media/e21076f6b556c84fa97719300859dae2/href</a></iframe><p>Model orders in the application with a GenServer, the list of order contents is the internal state:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f14e9f96e0c33e15292c82c7df68788d/href">https://medium.com/media/f14e9f96e0c33e15292c82c7df68788d/href</a></iframe><p>Run the application with iex -S mix and create orders:</p><pre>$ iex -S mix<br>Erlang/OTP 21 [erts-10.0] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe]</pre><pre>16:04:29.435 [info]  ExCluster application started</pre><pre>Interactive Elixir (1.7.1) - press Ctrl+C to exit (type h() ENTER for help)</pre><pre>iex(1)&gt; DynamicSupervisor.start_child(ExCluster.OrderSupervisor, { ExCluster.Order, &quot;John&quot; })<br>16:04:43.697 [info]  Starting Order for John<br>{:ok, #PID&lt;0.148.0&gt;}</pre><p>Note the returned PID (identifier for the underlying BEAM process), the application needs to be able to associate this to a customer. Say input flows in like this:</p><pre>Customer &quot;John&quot;, created order<br>Customer &quot;Karan&quot;, created order<br>Customer &quot;Karan&quot;, added to order items: [2, 3]<br>Customer &quot;John&quot;, added to order items: [1, 2]</pre><p>Which PID should be used when a customer adds items to an order? This is where a <a href="https://hexdocs.pm/elixir/1.7.4/Registry.html">Registry</a> comes into play, providing:</p><blockquote>local, decentralized and scalable key-value process storage.</blockquote><p>In this case it is a customer to order PID key-value store. Add one to the Supervision tree:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c25df05d0d3ef5ea0c19339702d6d5c6/href">https://medium.com/media/c25df05d0d3ef5ea0c19339702d6d5c6/href</a></iframe><p>Update the order GenServer to use the Registry, and also add some functions for adding to and getting the contents of an order:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4a4a9be67cf364217390a07038b58902/href">https://medium.com/media/4a4a9be67cf364217390a07038b58902/href</a></iframe><p>Now the application can manage multiple orders:</p><pre>iex(1)&gt; DynamicSupervisor.start_child(ExCluster.OrderSupervisor, { ExCluster.Order, &quot;John&quot; })<br>13:17:26.344 [info]  Starting Order for John<br>{:ok, #PID&lt;0.136.0&gt;}</pre><pre>iex(2)&gt; DynamicSupervisor.start_child(ExCluster.OrderSupervisor, { ExCluster.Order, &quot;Karan&quot; })<br>13:17:31.503 [info]  Starting Order for Karan<br>{:ok, #PID&lt;0.138.0&gt;}</pre><pre>iex(3)&gt; ExCluster.Order.add(&quot;Karan&quot;, [2, 3])<br>:ok</pre><pre>iex(4)&gt; ExCluster.Order.add(&quot;John&quot;, [1, 2])<br>:ok</pre><pre>iex(5)&gt; ExCluster.Order.contents(&quot;John&quot;)<br>[1, 2]</pre><pre>iex(6)&gt; ExCluster.Order.contents(&quot;Karan&quot;)<br>[2, 3]</pre><p>If this were deployed to Kubernetes any deployment would cause existing orders to be blown away as they live in-memory. To address this issue and not drop order data the application will need to hook into the deployment lifecycle.</p><h4>SIGTERM, Distributed Elixir, and Horde</h4><p>During a deploy new Pods are spun up and old Pods receive a SIGTERM. This gives the old Pods a short window of time (default 30 seconds) to cleanup. During this time orders need to move from the terminating Pod to another Pod. Similar to how a Supervisor locally restarts children, a global Supervisor can be used to restart across Pods.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P8oTEA7RZPtPwbB9-B6d7g.gif" /><figcaption>diagram explaining global Supervisor and deployment lifecycle hookup</figcaption></figure><p><a href="https://github.com/derekkraan/horde">Horde</a> is an Elixir library that provides this functionality. Using Distributed Elixir all running instances of the application (Nodes) can be connected together into a Cluster. Then with Horde.Registry and Horde.Supervisor can be used to have a Registry and Supervisor act across our Cluster in a distributed fashion.</p><p>This may seem very complicated, but this is where the beauty of the Erlang VM and message passing between processes really shines. Once connected in a Cluster messages can be passed between processes across machines the same way they are passed between locally running processes. The machine to machine communication is abstracted away, so the application logic itself does not get much more complex.</p><p>Add Horde ({:horde, &quot;~&gt; 0.3.0&quot;} in deps) and then instead of using the standard Registry and Supervisor use the Horde implementations:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fbd73c914d332936e27b2fd5068d2fad/href">https://medium.com/media/fbd73c914d332936e27b2fd5068d2fad/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/127110ea2ecd8ae49b704a0a9f332be3/href">https://medium.com/media/127110ea2ecd8ae49b704a0a9f332be3/href</a></iframe><p><strong>Note:</strong> before starting the Supervision Tree the application is connecting all Nodes into a Cluster(via Node.connect/1). This is leveraging Distributed Elixir and is a building block for Horde. When ExCluster.HordeConnector starts up, it will go through all other Nodes (Node.list()) in the Cluster and join the Supervisor and Registries together, making them work across machines.</p><p>For the Node connections to work two VM flags need to be supplied when running our application: name and cookie. Name is associated to the current Node and must be unique in the Cluster, while cookie is a secret value that must be consistent across all Nodes. See this in action by spinning up a 3 Node cluster:</p><pre># Terminal 1<br>ERL_FLAGS=&quot;-name count1@127.0.0.1 -setcookie cookie&quot; NODES=&quot;count2@127.0.0.1,count3@127.0.0.1&quot; iex -S mix</pre><pre># Terminal 2<br>ERL_FLAGS=&quot;-name count2@127.0.0.1 -setcookie cookie&quot; NODES=&quot;count1@127.0.0.1,count3@127.0.0.1&quot; iex -S mix</pre><pre># Terminal 3<br>ERL_FLAGS=&quot;-name count3@127.0.0.1 -setcookie cookie&quot; NODES=&quot;count1@127.0.0.1,count2@127.0.0.1&quot; iex -S mix</pre><p>Run Node.list() in any of the 3 windows, the output will be an array with the other two Node names. For example, from Terminal 1 the output is:</p><pre>[:&quot;count2@127.0.0.1&quot;, :&quot;count3@127.0.0.1&quot;]</pre><p>This means all 3 nodes are connected in a Cluster. You can see the global Supervisor in action by setting up an order in Terminal 1 and then gracefully terminating (:init.stop()):</p><pre>iex(count1@127.0.0.1)2&gt; Horde.Supervisor.start_child(ExCluster.OrderSupervisor, { ExCluster.Order, &quot;John&quot; })<br>13:51:57.125 [info]  Starting Order for John<br>{:ok, #PID&lt;0.220.0&gt;}</pre><pre>iex(count1@127.0.0.1)3&gt; ExCluster.Order.add(&quot;John&quot;, [1, 2])<br>:ok</pre><pre>iex(count1@127.0.0.1)4&gt; ExCluster.Order.contents(&quot;John&quot;)<br>[1, 2]</pre><pre>iex(count1@127.0.0.1)5&gt; :init.stop()<br>:ok</pre><p>Look at the output from the other Nodes, on one of them the output will be:</p><pre>iex(count2@127.0.0.1)1&gt;<br>13:53:48.001 [debug] Found 1 processes on dead nodes</pre><pre>13:53:48.088 [info]  Starting Order for John</pre><p>The OrderSupervisor restarted John’s order on another Node!</p><p>However, there is one thing left to do. The order for John was restarted but the internal state for the order (it’s contents) was not carried over. Get the contents of John’s order from the remaining Nodes:</p><pre>iex(count2@127.0.0.1)2&gt; ExCluster.Order.contents(&quot;John&quot;)<br>[]</pre><p>It’s an empty list. The order GenServer state needs to be handed off to the restarted process. This will need to be implemented in the application itself as it is not a part of Horde.</p><h4>Cross-Node State Handoff with DeltaCrdt</h4><p>A hook in the order GenServer termination is needed so the internal state can be temporarily stored. Then on restart it can be picked up and resume processing.</p><p>This can be done by adding a flag to the order GenServer initialization so that a terminate callback is run when it is going down:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/61045c77c3813138ee17f41f5fe17420/href">https://medium.com/media/61045c77c3813138ee17f41f5fe17420/href</a></iframe><p>In terminate, the order_contents need to be stored externally as the current Pod is about to go away. This can be done with something like Redis, but lets stick to Distributed Elixir solutions running across the Cluster to avoid external dependencies. The application will use conflict-free replicated data types (CRDT’s), which:</p><blockquote>are <em>eventually guaranteed to converge globally</em>, meaning that in the absence of network partitions, the data will eventually be the same at every replica.</blockquote><p><em>CRDTs are used internally in the implementations of both Horde and Phoenix Presence, you can find a good intro article </em><a href="https://medium.com/@derek.kraan2/how-deltacrdt-can-help-you-write-distributed-elixir-applications-dc838c383ad5"><em>here</em></a><em>.</em></p><p>A CRDT can be used as a distributed temporary caching mechanism that is synced across our Cluster. When a order GenServer restarts across Pods, it can look into this cache for existing order contents. Set this up by adding a new StateHandoff GenServer to our Supervision Tree, this will have a CRDT as it’s internal state. Similar to Horde Registry and Supervisor, this will need to be connected to all other StateHandoff CRDT’s running across the Cluster:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1927b2e5628edc1702e0626305bcf129/href">https://medium.com/media/1927b2e5628edc1702e0626305bcf129/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/75f1d6e519db5ecf4278fb9637181347/href">https://medium.com/media/75f1d6e519db5ecf4278fb9637181347/href</a></iframe><p>This adds a replicated, eventually consistent key-value store to our application. To finish off integrate this into the order GenServer:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/619d4f94f2a80344d146963b7048091d/href">https://medium.com/media/619d4f94f2a80344d146963b7048091d/href</a></iframe><p>Take it out for a spin: run multiple Nodes, create and add some contents to a order, then terminate:</p><pre>iex(count1@127.0.0.1)1&gt; Horde.Supervisor.start_child(ExCluster.OrderSupervisor, { ExCluster.Order, &quot;John&quot; })<br>15:28:05.211 [info]  Starting Order for John<br>{:ok, #PID&lt;0.238.0&gt;}</pre><pre>iex(count1@127.0.0.1)2&gt; ExCluster.Order.add(&quot;John&quot;, [1, 2])<br>:ok</pre><pre>iex(count1@127.0.0.1)3&gt; ExCluster.Order.contents(&quot;John&quot;)<br>[1, 2]</pre><pre>iex(count1@127.0.0.1)4&gt; :init.stop()<br>:ok</pre><p>Check on the order contents for John’s restarted order:</p><pre>iex(count2@127.0.0.1)1&gt;<br>15:28:34.463 [debug] Found 1 processes on dead nodes<br>15:28:34.554 [info] Starting Order for John</pre><pre>iex(count2@127.0.0.1)2&gt; ExCluster.Order.contents(“John”)<br>[1, 2]</pre><p>There you have it, a complete migration of orders happening from one Node to the other!</p><h4>Dynamic Cluster membership with LibCluster and Kubernetes</h4><p>Till now a fixed list of NODES has been specified via the environment when the application starts up. This works locally but not in production. In production the Cluster membership is dynamic as Pods get swapped in and out. Fortunately there is already a solution available in the Elixir community for managing dynamic Cluster membership: <a href="https://github.com/bitwalker/libcluster">LibCluster</a>.</p><p>Before getting into LibCluster details setup <a href="https://github.com/bitwalker/distillery">Distillery</a> for building releases, update mix.exs deps:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7601c6d82a05fba965b4ac367d01cf92/href">https://medium.com/media/7601c6d82a05fba965b4ac367d01cf92/href</a></iframe><p>Run mix release.init to configure Distillery release. Update rel/vm.args file to configure the name and cookie for the Node:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b5633cc97b789b812d451c21f22f93b/href">https://medium.com/media/0b5633cc97b789b812d451c21f22f93b/href</a></iframe><p>The syntax used here is specifying to sub-in runtime env variables MY_POD_IP and NODE_COOKIE when the Pod is run. Add a Dockerfile:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8ca5b77aeb7e0073532b435a823044b1/href">https://medium.com/media/8ca5b77aeb7e0073532b435a823044b1/href</a></iframe><p>Add two charts for Kubernetes, one for deployment which will use the downward API to grab the IP for each individual Pod and pass it through the environment:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa402ee3698ce14ab661bdc9a387eb52/href">https://medium.com/media/fa402ee3698ce14ab661bdc9a387eb52/href</a></iframe><p>One for a Kubernetes Headless Service, which provides a internal DNS that returns the IP’s of all Pods currently deployed:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3e425ebd0ce6959c92a9105c3e0ff486/href">https://medium.com/media/3e425ebd0ce6959c92a9105c3e0ff486/href</a></iframe><p>The Node names are specified as ex_cluster@${MY_POD_IP} and the downward API provides the unique MY_POD_IP via the env, giving each Pod a unique name. The cookie is configurable via the charts and consistent across all Nodes, satisfying the clustering requirements.</p><p>Integrate LibCluster as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7691d180127985d1cc1986b2f9dba612/href">https://medium.com/media/7691d180127985d1cc1986b2f9dba612/href</a></iframe><p>And configure LibCluster topologies by environment:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aca7f29eac9fc092758935a4999cc0a1/href">https://medium.com/media/aca7f29eac9fc092758935a4999cc0a1/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f4a4093161871fc18123f9f605835de1/href">https://medium.com/media/f4a4093161871fc18123f9f605835de1/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/561725c35c5cdfc54cf4564529d184ef/href">https://medium.com/media/561725c35c5cdfc54cf4564529d184ef/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9a8a310f93c72d5516fe39a338fe4702/href">https://medium.com/media/9a8a310f93c72d5516fe39a338fe4702/href</a></iframe><p>In production, LibCluster uses the headless service DNS every 3 seconds to check on Pod deployments and automatically update Cluster membership. <br><strong>Remember,</strong> on application start the Registry, Supervisor, and StateHandoff GenServer’s are connected to all other copies running in the Cluster (via ExCluster.HordeConnector). This ensures that as Pods get taken in and out our cross-Pod connections are kept up to date.</p><p>In development, a custom LocalStrategy is used that builds a Cluster based on a runtime env variable NODES, like we have been doing when running locally.</p><h4>Running in Kubernetes with minikube</h4><p>After starting minikube, build and tag a docker container and update the minikube docker-env:</p><pre>$ docker build -t ex_cluster:local .<br>$ eval $(minikube docker-env)</pre><p>Create the service and deployment:</p><pre>$ kubectl create -f k8s/service-headless.yaml<br>$ kubectl create -f k8s/deployment.yaml</pre><p>You should be able to see 2 Pods running:</p><pre>$ kubectl get pods</pre><pre>NAME                          READY     STATUS    RESTARTS   AGE<br>ex-cluster-5b45c8577b-jltm9   1/1       Running   0          5s<br>ex-cluster-5b45c8577b-ztn4p   1/1       Running   0          5s</pre><p>Exec onto one, attach to the console, and create a few orders:</p><pre>$ kubectl exec -it ex-cluster-5b45c8577b-jltm9 /bin/bash</pre><pre>[in the pod shell]$ _build/prod/rel/ex_cluster/bin/ex_cluster remote_console</pre><pre>[should now be attached to iex on the Pod]</pre><pre>iex(ex_cluster@172.17.0.6)1&gt; Horde.Supervisor.start_child(ExCluster.OrderSupervisor, { ExCluster.Order, &quot;John&quot; })</pre><pre>iex(ex_cluster@172.17.0.6)3&gt; ExCluster.Order.add(&quot;John&quot;, [1,2,3])<br>:ok</pre><pre>iex(ex_cluster@172.17.0.6)4&gt; ExCluster.Order.contents(&quot;John&quot;)<br>[1, 2, 3]</pre><p>Terminate the Pod where the order is running:</p><pre>$ kubectl delete pod ex-cluster-5b45c8577b-jltm9</pre><p>A new Pod should have been automatically spun up, with the terminated Pod now going down. Exec onto one of the two running Pods, and check on the order:</p><pre>$ kubectl get pods</pre><pre>NAME                          READY     STATUS        RESTARTS   AGE<br>ex-cluster-5b45c8577b-8cg9c   1/1       Running       0          3m<br>ex-cluster-5b45c8577b-d4cm8   1/1       Running       0          5s<br>ex-cluster-5b45c8577b-fkfj2   1/1       Terminating   0          3m</pre><pre>$ kubectl exec -it ex-cluster-5b45c8577b-d4cm8 /bin/bash</pre><pre>[in the pod shell]$ _build/prod/rel/ex_cluster/bin/ex_cluster remote_console</pre><pre>[should now be attached to iex on the Pod]</pre><pre>iex(ex_cluster@172.17.0.7)1&gt; ExCluster.Order.contents(&quot;John&quot;)<br>[1, 2, 3]</pre><p>There you go, the order was preserved through Pod tear down!</p><p>A full Repo is available of this demo application is available <a href="https://github.com/dollarshaveclub/ex_cluster">here</a>.</p><h4>Conclusion</h4><p>With this setup there are a few scenarios to consider:</p><ul><li>In memory order handling still has issues: How large are your inputs? How many come in at a time? How long do they stay around on average?</li><li>Non graceful termination, although unlikely, is possible. StateHandoff will not kick in here, so how will you recover from that? (Have a re-playable input source like Kafka?)</li><li>Horde.Supervisor does not guarantee to start the process on the calling machine. Internally Horde has it’s own way of balancing processes across the Cluster. See the Horde docs for more.</li></ul><p>Even with these concerns, we have made headway into integrating OTP with Kubernetes. As the Elixir community continues to mature I believe some of these concerns will be better addressed.</p><p><em>Much of this setup was inspired by Daniel Azuma’s talk and example application presented at ElixirConf 2018, you can view it </em><a href="https://www.youtube.com/watch?v=nLApFANtkHs"><em>here</em></a><em> and find more info </em><a href="http://daniel-azuma.com/articles/talks/elixirconf-2018"><em>here</em></a><em>. The StateHandoff implementation came out of reading the Horde internals. Horde is very well written and documented (thank you Derek Kraan) and worth looking at if you find yourself doing this kind of work.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9944636b8609" width="1" height="1" alt=""><hr><p><a href="https://medium.com/dsc-engineering/elixir-otp-applications-on-kubernetes-9944636b8609">Elixir OTP applications on Kubernetes</a> was originally published in <a href="https://medium.com/dsc-engineering">DSC Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Nerves on a Cellular Network with the Hologram Nova]]></title>
            <link>https://medium.com/@chiragtoor/nerves-on-a-cellular-network-with-the-hologram-nova-b4fb26e5278b?source=rss-a79f99b5e41c------2</link>
            <guid isPermaLink="false">https://medium.com/p/b4fb26e5278b</guid>
            <category><![CDATA[raspberry-pi]]></category>
            <category><![CDATA[elixir]]></category>
            <dc:creator><![CDATA[Chirag Singh Toor]]></dc:creator>
            <pubDate>Wed, 21 Nov 2018 17:31:12 GMT</pubDate>
            <atom:updated>2018-11-21T17:31:12.837Z</atom:updated>
            <content:encoded><![CDATA[<p>I’ve been experimenting with a Nerves project lately that required a network connection on the go. I wanted to be able to run HTTPoison.post or connect to a Phoenix channel on a moving device, beyond the range of home WiFi.</p><h4>Background — Skip if you want just the technical details</h4><p>Initially I came across devices that provided low-level AT command interfaces for making network calls, but these quickly became limiting as each HTTP call required multiple AT commands to setup and run. Large payload posts or websocket connections were not reliably working.</p><p>During the 2018 ElixirConf I spoke to a few people in the Nerves community about the issues I was running into and got some pointers, what I needed was some way to establish a serial PPP session with a usb modem. I began looking around and found the <a href="https://hologram.io/nova/">Hologram Nova</a>. This device plugs into a USB port and comes with a handy Python SDK, which had a function called connect that:</p><blockquote>Connects to the specified network by establishing a PPP session.</blockquote><p>This was exactly what I needed so I ordered one, set it up, and plugged it into the Pi:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YUmJeKEe9rT1z3lB_gRPxQ.jpeg" /><figcaption>Raspberry Pi 3B+ with the Hologram Nova plugged in</figcaption></figure><p>Everything powered up correctly, now I needed to figure out how to setup the connection from Elixir code. I didn’t know much about PPP or the details required so I started by first building a modified Nerves system that added Python and all the requirements for the Hologram Python SDK, then used <a href="https://github.com/fazibear/export">export</a> to call a Python function from Elixir that established the connection to the cellular network. Although this worked, it seemed a bit shaky because:</p><ul><li>I had to copy the Python SDK locally and modify it in order to make it work with the Nerves setup (it internally used sudo)</li><li>The Python SDK had several parts I did not need and bloated my project (it has many features beyond just the PPP sessions that I was not using)</li><li>When installing with buildroot’s utils/scanpypi not all dependencies setup automatically, so I had to make some hacks to get it all to work</li></ul><p>These reasons made the custom system hard to work with and maintain, and since I was only trying to connect to a PPP session I spent some time going through the Python SDK and pulling out what I needed. Below the steps indicate how to modify the base RPi3 Nerves system and how to connect to a cellular network through the Nova with just Elixir.</p><h4>Modifying the Nerves System</h4><p>To get this working you will need a modified <a href="https://github.com/nerves-project/nerves/blob/master/docs/Systems.md#customizing-your-own-nerves-system">Nerves system, the docs explain how to set this up</a> (If your on a Mac you might run into issues building the system locally, if so <a href="http://wsmoak.net/2016/10/17/building-using-custom-nerves-system.html">Wendy Smoak’s article is a big help</a>). Once you have a setup for modifying a system, the steps end up being fairly simple:</p><ul><li>Enabling the USB option driver (communicating with the Nova depends on this)</li><li>Enabling several PPP related modules</li><li>Enable mknod via BusyBox</li></ul><p>These changes come down to the following additions to the Nerves system files:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/804458b9a849c1e1a6e9d8ce3da9fce5/href">https://medium.com/media/804458b9a849c1e1a6e9d8ce3da9fce5/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/65ecf553a4953941e0d11b6abb468b22/href">https://medium.com/media/65ecf553a4953941e0d11b6abb468b22/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/74fabb1b7ccee36b06c5095f2b610e1d/href">https://medium.com/media/74fabb1b7ccee36b06c5095f2b610e1d/href</a></iframe><p>Build the system with the above changes (I followed Wendy’s article and used nerves_system_br/create_build.sh) and export the output.</p><p>A complete modified example system with these changes is available here: <a href="https://github.com/chiragtoor/hologram_system_rpi3">https://github.com/chiragtoor/hologram_system_rpi3</a></p><h4>Connecting to the Cellular Network</h4><p>Now that we have a custom system built we can load up a new Nerves project and go through step by step how to establish the network connection. Let’s start by using the generator to setup a new project:</p><blockquote>mix nerves.new cellular — init-gadget — target rpi3</blockquote><p>In the config/config.exs file we want to setup the init-gadget config to use ethernet so we can ssh onto the Pi:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a4c1d3779d07fbb81cfa2fc6c112fdc3/href">https://medium.com/media/a4c1d3779d07fbb81cfa2fc6c112fdc3/href</a></iframe><p>Now you should be able to run ssh nerves.local to open up an iex shell.</p><p>Let’s add HTTPoison to make it easy for us to test our connection, update your mix.exs file to include {:httpoison, &quot;~&gt; 1.4&quot;}. Update your firmware and re-ssh onto your Pi, you should be able to run HTTPoison.get &quot;google.com&quot; and get the following output:</p><blockquote>** (HTTPoison.Error) :nxdomain<br>(httpoison) lib/httpoison.ex:128: HTTPoison.request!/5</blockquote><p>Use the modified system from above by either linking to it in your mix.exs or by setting NERVES_SYSTEM in your env to point to the outputted system. To finish the setup there are two more files you will need to add to your nerves project rootfs_overlay directory in the following folders:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/22beea67ab0b0089b1d01f5dc6d96135/href">https://medium.com/media/22beea67ab0b0089b1d01f5dc6d96135/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/42e6d15220d70a8109c6ebd62ad0e4cf/href">https://medium.com/media/42e6d15220d70a8109c6ebd62ad0e4cf/href</a></iframe><p>These two files define some configs and steps needed to establish connection to the Hologram network. To run this all, we can add a connect function to our application:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e176aa045a5770d00bff5d099af0b50f/href">https://medium.com/media/e176aa045a5770d00bff5d099af0b50f/href</a></iframe><p>At this point you should be able to build the firmware and run the above function from iex. You should run this asynchronously, something similar toTask.async(fn -&gt; Cellular.connect() end). This should establish a PPP session and connect to the network, test by running HTTPoison.get! &quot;google.com&quot;, this time you should get a response.</p><p>An example application for the above is available here: <a href="https://github.com/chiragtoor/cellular">https://github.com/chiragtoor/cellular</a></p><p><em>A big thank you to the Nerves team for being very helpful on Slack and fantastic docs on how to go about modifying systems. If you find the need to do enable additional modules or add libraries the buildroot docs are also very helpful.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b4fb26e5278b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Calling Python from Elixir: ErlPort vs Thrift]]></title>
            <link>https://medium.com/hackernoon/calling-python-from-elixir-erlport-vs-thrift-be75073b6536?source=rss-a79f99b5e41c------2</link>
            <guid isPermaLink="false">https://medium.com/p/be75073b6536</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[elixir]]></category>
            <dc:creator><![CDATA[Chirag Singh Toor]]></dc:creator>
            <pubDate>Tue, 20 Dec 2016 05:17:24 GMT</pubDate>
            <atom:updated>2019-04-29T10:31:15.930Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://hackernoon.com/tagged/learning">Learning</a> and using Elixir has been one of my most enjoyable <a href="https://hackernoon.com/tagged/programming">programming</a> experiences, it has a great community and many awesome tools. The only issue I have come across till now is that some libraries in the community do not exist or are not fully developed yet.</p><p>I ran into this problem recently when I needed to process some large GeoJSON files that defined lat/long boundaries for blocks and municipal zones. The data was represented as Polygons and I needed to figure out which blocks came into commercial zones. This required detecting a intersection between two Polygons, a difficult problem to solve.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2GfT9dGckVYHwIFYX6IJjQ.png" /><figcaption>city blocks in red, commercial zones layered on top in green</figcaption></figure><p>I could not find any library to help do this in Elixir, but there was a Python library, <a href="https://pypi.python.org/pypi/Shapely">Shapely</a>, that would do exactly what I needed. I found two options for integrating this into my project, <a href="http://erlport.org/">ErlPort</a> and <a href="https://thrift.apache.org/">Apache Thrift</a>.</p><p>To show how I used these two I’ll be going through a simple example that works on two very small GeoJSON files, one defining blocks and one defining zones. I’ll assume non-commercial zones are already filtered out, we will just be trying to find the blocks that intersect with any of the zones. The files have this format:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a509476d8b07f054a0fa36abbe5c0adf/href">https://medium.com/media/a509476d8b07f054a0fa36abbe5c0adf/href</a></iframe><p>For these examples I created a new mix project, PythonCalls. The code and GeoJSON files are available here: <a href="https://github.com/chiragtoor/python_calls">https://github.com/chiragtoor/python_calls</a></p><h4>Using ErlPort</h4><p>To start with we can pull out the block and zone info using <a href="https://github.com/devinus/poison">Poison</a>, keep in mind our example files are very small so we can read them entirely into memory:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/783f50afc7fa2b350c45168b949b3d56/href">https://medium.com/media/783f50afc7fa2b350c45168b949b3d56/href</a></iframe><p>Using these private helper functions we get the lat/long coordinates of the blocks and zones with pattern matching:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/31e827b0910e9f34b3b144b48db48837/href">https://medium.com/media/31e827b0910e9f34b3b144b48db48837/href</a></iframe><p>Now we need some Python code that will take in these coordinates and use Shapely’s Polygons to detect an intersection:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/27e4218e41d846ce71e4b47fddf52c1e/href">https://medium.com/media/27e4218e41d846ce71e4b47fddf52c1e/href</a></iframe><p>Connecting these two pieces via ErlPort is very simple, we need to add it to our deps:</p><pre> {:erlport, git: “https://github.com/hdima/erlport.git&quot;}</pre><p>Now just start up a Python instance and use it to make calls to our Python function, since our blocks and zones are only made up of lists and floats the data we pass to Python is automatically converted to Python equivalents by ErlPort:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c62ba9b968819d44694297e2b56cf700/href">https://medium.com/media/c62ba9b968819d44694297e2b56cf700/href</a></iframe><p>With this our Elixir and Python pieces are connected and we can easily tell which blocks come into commercial zones using Shapely.</p><h4>Using Apache Thrift</h4><p>Thanks to <a href="https://github.com/pinterest/riffed">Pinterest’s Riffed</a> library, using Thrift is very easy. Here I’ll just be showing a quick setup, for more details on Thrift refer to the <a href="https://thrift.apache.org/">project webpage</a>.</p><p>With Thrift we will be setting up a separate service in Python that will be called through a Elixir client. The <a href="https://github.com/pinterest/riffed/blob/master/doc/GettingStarted.md">getting started guide</a> in the Riffed Github repo covers how to setup Riffed very well, so assuming that is done we need to define a Thrift interface file. This defines the service we will build and the data structures that will be used:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/48e20594d4791fdd1a240625b9c85167/href">https://medium.com/media/48e20594d4791fdd1a240625b9c85167/href</a></iframe><p>We need to be able to express our blocks and zones in some way, so we have a Point struct for each pair of lat/long coordinates and a Boundary struct for all the Points of a block/zone. The service only needs one function that will take in a block and zone and tell us if they intersect. We need to run Thrift in order to build our Python service on top of this interface.</p><pre>thrift -gen py -out python/ ./thrift/geoservice.thrift</pre><p>This takes our defined interface and outputs several files to a directory called ‘python’ at the root of our mix project. We will use these generated files to implement our service:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0426b3f7eb36dbe303912296e172813e/href">https://medium.com/media/0426b3f7eb36dbe303912296e172813e/href</a></iframe><p>Now we need to setup a Elixir client for our Python service using Riffed, this will also give us Elixir structs for the data structures we specified in our Thrift file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6c7c04599b756604f9ece880ba0c094f/href">https://medium.com/media/6c7c04599b756604f9ece880ba0c094f/href</a></iframe><p>After running our Python service we can use the same setup as earlier and make calls through our client:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e921c6cbf7b111e8ee2c025fb3b4d3d9/href">https://medium.com/media/e921c6cbf7b111e8ee2c025fb3b4d3d9/href</a></iframe><p>We are creating Boundary structs with the GeoJSON data for each block and zone, these will be taken in and converted between Elixir and Python data structures during our service calls by the generated Thrift code.</p><h4>Comparing the two approaches</h4><p>Setting up either approach was not too difficult, however I had used Thrift before and so the concepts and use of it was familiar. With the Thrift approach the interface between Elixir and Python is clearly defined, but the ErlPort approach took much less code (especially when you count the files generated by Thrift). For this sort of simple case I would go with ErlPort, Thrift was a bit of overkill.</p><p>However, there are some cases in which I would go with the Thrift approach instead:</p><ol><li>Passing complicated custom data structures with ErlPort does not seem trivial, you have to define your own encoding and decoding whereas with Thrift the data structures defined in the interface are automatically handled between languages.</li><li>ErlPort Python instances cannot handle multiple calls concurrently, and they use OS processes so you need to be very careful with spawning multiple instances. With Thrift you can move the service to another machine and even setup threaded server types to have it handle multiple requests, you get this option out of the box.</li><li>Depending on what language you are trying to connect with, Thrift has <a href="https://thrift.apache.org/lib/">many options</a> where as with ErlPort you only have Python or Ruby.</li></ol><p>So for simple approaches like this GeoJSON example I would go with ErlPort, but for anything complicated I would think Thrift would work better.</p><p>Example project repo: <a href="https://github.com/chiragtoor/python_calls">https://github.com/chiragtoor/python_calls</a></p><figure><a href="http://bit.ly/HackernoonFB"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0hqOaABQ7XGPT-OYNgiUBg.png" /></a></figure><figure><a href="https://goo.gl/k7XYbx"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vgw1jkA6hgnvwzTsfMlnpg.png" /></a></figure><figure><a href="https://goo.gl/4ofytp"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gKBpq1ruUi0FVK2UM_I4tQ.png" /></a></figure><blockquote><a href="http://bit.ly/Hackernoon">Hacker Noon</a> is how hackers start their afternoons. We’re a part of the <a href="http://bit.ly/atAMIatAMI">@AMI</a> family. We are now <a href="http://bit.ly/hackernoonsubmission">accepting submissions</a> and happy to <a href="/service/mailto:partners@amipublications.com">discuss advertising &amp; sponsorship</a> opportunities.</blockquote><blockquote>If you enjoyed this story, we recommend reading our <a href="http://bit.ly/hackernoonlatestt">latest tech stories</a> and <a href="https://hackernoon.com/trending">trending tech stories</a>. Until next time, don’t take the realities of the world for granted!</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*35tCjoPcvq6LbB3I6Wegqw.jpeg" /></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Fdde502%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2Fhackernoon%2F&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href">https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=be75073b6536" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/calling-python-from-elixir-erlport-vs-thrift-be75073b6536">Calling Python from Elixir: ErlPort vs Thrift</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Avoiding 3rd Party API Calls in Phoenix Tests]]></title>
            <link>https://medium.com/hackernoon/controlled-testing-in-phoenix-50709cb5c9c5?source=rss-a79f99b5e41c------2</link>
            <guid isPermaLink="false">https://medium.com/p/50709cb5c9c5</guid>
            <category><![CDATA[phoenix-framework]]></category>
            <category><![CDATA[elixir]]></category>
            <dc:creator><![CDATA[Chirag Singh Toor]]></dc:creator>
            <pubDate>Tue, 22 Nov 2016 05:16:11 GMT</pubDate>
            <atom:updated>2019-04-29T10:31:17.573Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently when working on a <a href="https://hackernoon.com/tagged/phoenix">Phoenix</a> project I found that I wanted to avoid making 3rd party API calls when running my tests. Using <a href="https://www.twilio.com/">Twilio</a>, through the <a href="https://github.com/danielberkompas/ex_twilio">ExTwilio</a> library, I was sending SMS messages to users. Each SMS message cost money to send and my tests were <a href="https://hackernoon.com/tagged/generating">generating</a> random phone numbers and messages, so you can see why I needed to do this. I had this <a href="https://hackernoon.com/tagged/method">method</a> making the 3rd party API call:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b4d121bdb8bb3960e03754ed4806c7a3/href">https://medium.com/media/b4d121bdb8bb3960e03754ed4806c7a3/href</a></iframe><p>To avoid the library call I did some digging in the <a href="https://hexdocs.pm/plug/Plug.Conn.html">Plug.Conn</a> docs and saw that when running tests, the adapter field for conn is ‘Plug.Adapters.Test.Conn’. Knowing this I thought pattern matching was a simple way to avoid the API calls during tests, I would just have to pass in conn to my method and edit it like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/429ad594feb213cf57ec6533ca322337/href">https://medium.com/media/429ad594feb213cf57ec6533ca322337/href</a></iframe><p>If running tests, the first <a href="https://hackernoon.com/tagged/implementation">implementation</a> will be called because it will match first, otherwise the real implementation will be called. With this I am able to avoid my API calls pretty easily, this didn’t seem bad to me as it was only happening in one place in my code.</p><p>However with this I am passing in a argument to the function without really doing anything with it inside, so if I had to do this in multiple places it could start to feel dirty. Also, it is dependent on conn, so if I had to call this method anytime a web request was not being processed there would be a issue. To get around this there is another approach that uses the config setup for the project and has a fake module to be called during tests.</p><p>Lets say I am are running the same text message send code except it is done in the background and we have no conn, I pull a list of phone numbers and send each a text message every hour. Lets define a module Twilio.ex like this to handle the API calls:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fdd5a0042a60bfe1d7e252493e6881da/href">https://medium.com/media/fdd5a0042a60bfe1d7e252493e6881da/href</a></iframe><p>At the same time I would define a fake module that mimics the real module to be used in test cases, FakeTwilio.ex:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9096ea70f63b4693c26fde906306a52f/href">https://medium.com/media/9096ea70f63b4693c26fde906306a52f/href</a></iframe><p>Now I can easily use the correct module depending on if I am running tests or not by setting up definitions in our config.exs and test.exs files:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bd53e3abc1e6d05fa55caacd63eac8e4/href">https://medium.com/media/bd53e3abc1e6d05fa55caacd63eac8e4/href</a></iframe><p>This sets it up so the correct module is retrieved through ‘Application.get_env’ calls, just need to wrap the module in a helper function that will get the right config setup like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f866cf24a9dd929543c960c961f4e90d/href">https://medium.com/media/f866cf24a9dd929543c960c961f4e90d/href</a></iframe><p>Automatically here the private text_module method will load up the fake module during tests and the real module otherwise.</p><p>Both of these methods allowed me to avoid making 3rd party API calls when running my tests, here is a example project with both of these setups: <a href="https://github.com/chiragtoor/test_controlled_apis">https://github.com/chiragtoor/test_controlled_apis</a></p><figure><a href="http://bit.ly/HackernoonFB"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0hqOaABQ7XGPT-OYNgiUBg.png" /></a></figure><figure><a href="https://goo.gl/k7XYbx"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vgw1jkA6hgnvwzTsfMlnpg.png" /></a></figure><figure><a href="https://goo.gl/4ofytp"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gKBpq1ruUi0FVK2UM_I4tQ.png" /></a></figure><blockquote><a href="http://bit.ly/Hackernoon">Hacker Noon</a> is how hackers start their afternoons. We’re a part of the <a href="http://bit.ly/atAMIatAMI">@AMI</a> family. We are now <a href="http://bit.ly/hackernoonsubmission">accepting submissions</a> and happy to <a href="/service/mailto:partners@amipublications.com">discuss advertising &amp; sponsorship</a> opportunities.</blockquote><blockquote>If you enjoyed this story, we recommend reading our <a href="http://bit.ly/hackernoonlatestt">latest tech stories</a> and <a href="https://hackernoon.com/trending">trending tech stories</a>. Until next time, don’t take the realities of the world for granted!</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*35tCjoPcvq6LbB3I6Wegqw.jpeg" /></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Fdde502%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2Fhackernoon%2F&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href">https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=50709cb5c9c5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/controlled-testing-in-phoenix-50709cb5c9c5">Avoiding 3rd Party API Calls in Phoenix Tests</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>