Software engineer, data guy, Open Source enthusiast, New Hampshire resident, husband, father. Fan of guitars, hiking, photography, homebrewing, sarcasm.
JRuby: Asynchronous background job processing via Sidekiq using PhantomJS to create analytic screenshots
In this article I’ll demo some code that uses Sidekiq for asynchronous background job processing. Since I like using headless browsers, I decided to create a worker that uses PhantomJS to create a screenshot for each page request, and a Sinatra front-end with a Google Analtyics-like JS include.
Install dependencies, as necessary:
Define (J)Ruby version, file: .ruby-version
Create a Gemfile for Ruby dependencies. Install via “bundle install”.
Create the worker class. file: worker.rb
Create a Rack config.ru for the Sidekiq worker admin dashboard. file: worker.config.ru
Start worker and dashboard via rackup
Create the Sinatra web services. file: web_services.rb
Create a Rack config.ru for Sinatra. file: web_services.config.ru
Create Sinatra view for demo page, including JS include. file: views/index.erb
Start Sinatra web services
Browse to Sinatra via http://localhost:4567, and click around on the randomly generated links. Each page request calls analytics.js as a JS include, which asynchronously calls the worker. The worker dumps screenshots into the public folder.
Browse to the Sidekiq dashboard to check out the job queues, http://localhost:9494
Next steps: create an admin dashboard that shows the screenshots realtime via a websocket connection…