Deploying your Elixir App onto ARM-Linux device with Exrm
I had a Raspberry Pi 1 running OSMC on my local network for quite a while now and I trust I should be able to host my Phoenix application on that box as well. In Erlang land you need a release to run your app on a remote host. Elixir has Exrm lib for building releases for mix projects. Phoenix Docs describe deploy process thoroughly but it works for systems with Erlang pre-installed. You are on your own if you want to cross-compile your release from Intel to ARM architecture.
I failed to do build it manually on OSX so I’ve used erlang-minimal with help of Erlang Embedded docs. This post this post goes over details on how to get minimal runtime up and running.
In essence, you must have a working elrang runtime environment to get your app working, then OpenSSL lib must be present to be able to deal with crypto.
After the installation run, you will get the erlang build installed in /usr/lib/erlang/ next copy that folder to a convenient location /Volumes/Disk/erlang
That folder can be used for all further deploys to most ARM architectures.
Then tell Exrm about specific libs before running release. Libs and erts of Erlang built for Pi (/Volumes/Disk/erlang see Configure relx).
Problem described here is an inherited design and doesn’t reflect my engineering opinion. I just had to make it work in given ecosystem.
Given a list of hardware servers we need to pull the least busy one from the pool.
Pool in our case is a sorted set of worker ips which is sorted by CPU load in descending order (least busy first). Thus queuer can just take first worker ip in the array and know that’s the least busiest worker.
We need a Redis DB running and be accessible by other servers to update information about themselves.
Will use a Redis Set to store our server ips. And Redis Hashes to store CPU utilization and some other info about the server.
Here we have two workers and one server_ips set
In JSON that would be
In JSON that would be
Since cpu load of our “10.0.1.2” worker is less than “0.50” it’s considered the least busy one.
It would be awesome to get entire hash back when you do the sorting, but you can only use GET command for hash property.
You can do pretty cool things with if you read the docs.
Loading Images as binary with Webworker built from the function
Few months ago I had to read binary image data from s3 with XHRHttpRequest and then parse ExIf header to determine its orientation.
I had to load and parse numerous images asynchronously. Tried executing it all in one thread; no fun. Browser starts freaking out once I hit 4 images.
Just loading binary image data is a problem.
I am not going to describe the basics of Webworkers in this post. Html5 Rocks explained it with much better English in 2010.
Please read that linked article, your future self will thank you for investing time in this.
Load and parse images in separate threads and then pass their metadata to the main thread.
My biggest problems with default Webworker loader are versioning and maintenance of separate files. I don’t like extra dependencies in my code unless I must support unstable versions of browsers.
Worker here is actually a string I load from browserify module.
Try following in the console you’ll get the result.
Please note that there’s a context problem. When you load this to webworker as a blob you’ll start getting weird messages about undefined references. Try calling self.postMessage…
I immediately invoke the wrapper function to keep the context.
Final version with xhr calls minus ExIf parser looks like so:
And to utilize that module I just require the module and call it in the view:
Now your webworker is passing messages essential to keep main thread informed of the loading and work process. See onload, onprogress.
When loading is done it simply kills itself self.close().
Important thing to notice that you should transfer objects not copy them (see about transferrable objects Html5 Rocks).
And here’s what we do with the image when worker is done:
Obviously, the solution above is over engineered and most likely you won’t get a job :(. It’s fun to play with matrixes but when it comes to real life you should never use code described above in production.
The spiral problem is solved by keeping track of the coordinates for the traversed two-dimensional array.