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 better English in 2010.
Please read that linked article. Your future self with thank you for investing your time in this.
load and parse images in separate threads and then later pass them (with metadata) to the main thread.
My biggest problem with default Webworker loader is versioning and maintenance of separate files. I don’t like extra dependencies unless I need to 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
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
When loading is done it simply kills itself
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: