From 5f8996ec45a6d7fe3912039a1c448d5f719b4fa0 Mon Sep 17 00:00:00 2001
From: "Andrew T. Baker" <andrew.tork.baker@gmail.com>
Date: Tue, 10 Mar 2015 16:56:59 -0400
Subject: [PATCH] Adding Sidekiq monitoring, and also updating
 docker-compose.yml

---
 .env.example       |  1 +
 README.md          | 15 +++++++++++++++
 config.ru          | 17 ++++++++++++++++-
 config/routes.rb   |  4 ++++
 docker-compose.yml | 13 +++++++++++++
 5 files changed, 49 insertions(+), 1 deletion(-)

diff --git a/.env.example b/.env.example
index ab7022d..1d2d459 100644
--- a/.env.example
+++ b/.env.example
@@ -1,6 +1,7 @@
 # Set development environment variables here. Uses https://github.com/bkeepers/dotenv
 
 SIDEKIQ_TESTING=inline
+SIDEKIQ_DASHBOARD_PASSWORD=sidekiq
 
 SUNLIGHT_KEY=YOURKEY
 
diff --git a/README.md b/README.md
index 6f429ef..f2cee1e 100644
--- a/README.md
+++ b/README.md
@@ -29,6 +29,8 @@ If you want to use [Docker](https://www.docker.com/) for local development, this
 
 If you already have Docker and Docker Compose installed, starting this app is as easy as `docker-compose up`.
 
+After you've created the Docker containers, you can apply the migrations to the database with `docker-compose run web rake db:migrate`.
+
 ### RVM
 
 *If you don't want to use Docker* for some reason, you can also just install ruby and all dependencies on your own:
@@ -38,6 +40,19 @@ If you already have Docker and Docker Compose installed, starting this app is as
 1. Install Posgres and set up a user account.
 1. Install gems w/ `bundle install`
 
+You will also need to start a local PostgreSQL database and a Redis instance.
+
+Finally, apply the database migrations with `rake db:migrate`.
+
+### Sidekiq worker
+
+This app uses [sidekiq](https://github.com/mperham/sidekiq) to do background processing for a few tasks.
+
+By default, the app expects a sidekiq worker process to be available. You can start this process locally by running `bundle exec sidekiq -c 10`.
+
+If you don't want to send jobs to sidekiq, you can set the `SIDEKIQ_TESTING` environment variable to `'inline'` or `'fake'`.
+
+
 Deployment & Hosting Environments
 ---------------------------------
 
diff --git a/config.ru b/config.ru
index 9f97443..bd7803a 100644
--- a/config.ru
+++ b/config.ru
@@ -15,4 +15,19 @@ use Rack::Cors do
       :headers => :any,
       :methods => [:get, :post, :put, :options]
   end
-end
\ No newline at end of file
+end
+
+# Code to enable the sidekiq monitoring dashboard
+require 'sidekiq'
+Sidekiq.configure_client do |config|
+  config.redis = { :size => 1 }
+end
+
+require 'sidekiq/web'
+map '/sidekiq' do
+  use Rack::Auth::Basic, "Protected Area" do |username, password|
+    username == 'sidekiq' && password == ENV["SIDEKIQ_DASHBOARD_PASSWORD"]
+  end
+
+  run Sidekiq::Web
+end
diff --git a/config/routes.rb b/config/routes.rb
index 525048a..7dadede 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -16,4 +16,8 @@
   get  '/calls/new_connection',           to: 'calls#new_connection'
   post '/calls/connection_gather_prompt', to: 'calls#connection_gather_prompt'
   post '/calls/connection_gather',        to: 'calls#connection_gather'
+
+  require 'sidekiq/api'
+  get "/queue-status" => proc { [200, {"Content-Type" => "text/plain"}, [Sidekiq::Queue.new.size < 100 ? "OK" : "UHOH" ]] }
+  get "/queue-latency" => proc { [200, {"Content-Type" => "text/plain"}, [Sidekiq::Queue.new.latency < 30 ? "OK" : "UHOH" ]] }
 end
diff --git a/docker-compose.yml b/docker-compose.yml
index f84ae86..d0c27be 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -16,6 +16,19 @@ web:
     - DATABASE_URL=postgres://postgres@db:5432/postgres
     - REDIS_URL=redis://redis@redis:6379/
 
+worker:
+  image: mayday20backend_web:latest
+  command: bundle exec sidekiq -c 10
+  volumes:
+    - .:/usr/src/app
+  links:
+    - redis
+  env_file:
+    - .env
+  environment:
+    - REDIS_PROVIDER=REDIS_URL
+    - REDIS_URL=redis://redis@redis:6379/
+
 db:
   image: postgres:9.4