From c716d7cf2717ad736863eef8ab739ff20dc02272 Mon Sep 17 00:00:00 2001 From: Thomas Riboulet Date: Fri, 1 Dec 2017 13:41:39 +0100 Subject: [PATCH] tweaking things a bit to add a basic batch worker using sidekiq --- Dockerfile | 2 +- Gemfile | 1 + Gemfile.lock | 28 +++++++++++----------------- bin/batch | 6 ++++++ bin/http | 3 +++ hello.rb | 14 ++++++++++++++ views/index.erb | 15 +++++++++++++++ worker.rb | 15 +++++++++++++++ 8 files changed, 66 insertions(+), 18 deletions(-) create mode 100755 bin/batch create mode 100755 bin/http create mode 100644 views/index.erb create mode 100644 worker.rb diff --git a/Dockerfile b/Dockerfile index 4e0cc82..0b4e1f9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,4 +9,4 @@ ADD . /var/app RUN cd /var/app; bundle install -CMD ["/usr/local/bin/foreman","run", "web","-d","/var/app"] +CMD ["./var/app/bin/http"] diff --git a/Gemfile b/Gemfile index efb63d3..ee60067 100644 --- a/Gemfile +++ b/Gemfile @@ -2,3 +2,4 @@ source 'https://rubygems.org' gem 'sinatra' gem 'haml' gem 'puma' +gem 'sidekiq' diff --git a/Gemfile.lock b/Gemfile.lock index ad91976..112bb6a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,34 +1,26 @@ GEM remote: https://rubygems.org/ specs: - celluloid (0.16.0) - timers (~> 4.0.0) - connection_pool (2.1.2) + concurrent-ruby (1.0.5) + connection_pool (2.2.1) haml (4.0.5) tilt - hitimes (1.2.2) - json (1.8.2) puma (2.11.1) rack (>= 1.1, < 2.0) rack (1.5.2) rack-protection (1.5.2) rack - redis (3.2.1) - redis-namespace (1.5.1) - redis (~> 3.0, >= 3.0.4) - sidekiq (3.3.2) - celluloid (>= 0.16.0) - connection_pool (>= 2.1.1) - json - redis (>= 3.0.6) - redis-namespace (>= 1.3.1) + redis (4.0.1) + sidekiq (5.0.5) + concurrent-ruby (~> 1.0) + connection_pool (~> 2.2, >= 2.2.0) + rack-protection (>= 1.5.0) + redis (>= 3.3.4, < 5) sinatra (1.4.4) rack (~> 1.4) rack-protection (~> 1.4) tilt (~> 1.3, >= 1.3.4) tilt (1.4.1) - timers (4.0.1) - hitimes PLATFORMS ruby @@ -36,6 +28,8 @@ PLATFORMS DEPENDENCIES haml puma - redis sidekiq sinatra + +BUNDLED WITH + 1.16.0 diff --git a/bin/batch b/bin/batch new file mode 100755 index 0000000..b5ddda6 --- /dev/null +++ b/bin/batch @@ -0,0 +1,6 @@ +#!/bin/sh +CONCURRENCY_LEVEL=${CONCURRENCY:-1} + +echo "Concurrency set to : ${CONCURRENCY_LEVEL}" +echo "" +bundle exec sidekiq -r ./worker.rb -c $CONCURRENCY_LEVEL diff --git a/bin/http b/bin/http new file mode 100755 index 0000000..e357581 --- /dev/null +++ b/bin/http @@ -0,0 +1,3 @@ +#!/bin/sh +echo "the port is : ${PORT}" +bundle exec puma -t 5:5 -p ${PORT:-8080} -e ${RACK_ENV:-development} diff --git a/hello.rb b/hello.rb index db4b18f..adea306 100644 --- a/hello.rb +++ b/hello.rb @@ -1,12 +1,26 @@ require 'sinatra' +require 'sidekiq' +require 'redis' +require 'sidekiq/api' require 'haml' +$redis = Redis.new + class SimpleApp < Sinatra::Base get '/' do + SinatraWorker.perform_async(msg="beanstalk was pulled") haml "%h3 Hello THE BEANSTALK AWESOME World!" end + get '/stats' do + stats = Sidekiq::Stats.new + @failed = stats.failed + @processed = stats.processed + @messages = $redis.lrange('sinkiq-example-messages', 0, -1) + erb :index + end + get '/mu-c9478619-b3ea1fef-e218a7ee-09081759' do '42' end diff --git a/views/index.erb b/views/index.erb new file mode 100644 index 0000000..6d0fdc6 --- /dev/null +++ b/views/index.erb @@ -0,0 +1,15 @@ +

Sinatra + Sidekiq Example

+

Failed: <%= @failed %>

+

Processed: <%= @processed %>

+ +
+ + +
+ +Refresh page + +

Messages

+<% @messages.each do |msg| %> +

<%= msg %>

+<% end %> diff --git a/worker.rb b/worker.rb new file mode 100644 index 0000000..50202d7 --- /dev/null +++ b/worker.rb @@ -0,0 +1,15 @@ +require 'sinatra' +require 'sidekiq' +require 'redis' +require 'sidekiq/api' + +$redis = Redis.new + +class SinatraWorker + include Sidekiq::Worker + + def perform(msg="lulz you forgot a msg!") + sleep 2 + rand(10) + $redis.lpush("sinkiq-example-messages", msg) + end +end