tweaking things a bit to add a basic batch worker using sidekiq

master
Thomas Riboulet 6 years ago
parent 7d49e80dba
commit c716d7cf27

@ -9,4 +9,4 @@ ADD . /var/app
RUN cd /var/app; bundle install RUN cd /var/app; bundle install
CMD ["/usr/local/bin/foreman","run", "web","-d","/var/app"] CMD ["./var/app/bin/http"]

@ -2,3 +2,4 @@ source 'https://rubygems.org'
gem 'sinatra' gem 'sinatra'
gem 'haml' gem 'haml'
gem 'puma' gem 'puma'
gem 'sidekiq'

@ -1,34 +1,26 @@
GEM GEM
remote: https://rubygems.org/ remote: https://rubygems.org/
specs: specs:
celluloid (0.16.0) concurrent-ruby (1.0.5)
timers (~> 4.0.0) connection_pool (2.2.1)
connection_pool (2.1.2)
haml (4.0.5) haml (4.0.5)
tilt tilt
hitimes (1.2.2)
json (1.8.2)
puma (2.11.1) puma (2.11.1)
rack (>= 1.1, < 2.0) rack (>= 1.1, < 2.0)
rack (1.5.2) rack (1.5.2)
rack-protection (1.5.2) rack-protection (1.5.2)
rack rack
redis (3.2.1) redis (4.0.1)
redis-namespace (1.5.1) sidekiq (5.0.5)
redis (~> 3.0, >= 3.0.4) concurrent-ruby (~> 1.0)
sidekiq (3.3.2) connection_pool (~> 2.2, >= 2.2.0)
celluloid (>= 0.16.0) rack-protection (>= 1.5.0)
connection_pool (>= 2.1.1) redis (>= 3.3.4, < 5)
json
redis (>= 3.0.6)
redis-namespace (>= 1.3.1)
sinatra (1.4.4) sinatra (1.4.4)
rack (~> 1.4) rack (~> 1.4)
rack-protection (~> 1.4) rack-protection (~> 1.4)
tilt (~> 1.3, >= 1.3.4) tilt (~> 1.3, >= 1.3.4)
tilt (1.4.1) tilt (1.4.1)
timers (4.0.1)
hitimes
PLATFORMS PLATFORMS
ruby ruby
@ -36,6 +28,8 @@ PLATFORMS
DEPENDENCIES DEPENDENCIES
haml haml
puma puma
redis
sidekiq sidekiq
sinatra sinatra
BUNDLED WITH
1.16.0

@ -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

@ -0,0 +1,3 @@
#!/bin/sh
echo "the port is : ${PORT}"
bundle exec puma -t 5:5 -p ${PORT:-8080} -e ${RACK_ENV:-development}

@ -1,12 +1,26 @@
require 'sinatra' require 'sinatra'
require 'sidekiq'
require 'redis'
require 'sidekiq/api'
require 'haml' require 'haml'
$redis = Redis.new
class SimpleApp < Sinatra::Base class SimpleApp < Sinatra::Base
get '/' do get '/' do
SinatraWorker.perform_async(msg="beanstalk was pulled")
haml "%h3 Hello THE BEANSTALK AWESOME World!" haml "%h3 Hello THE BEANSTALK AWESOME World!"
end 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 get '/mu-c9478619-b3ea1fef-e218a7ee-09081759' do
'42' '42'
end end

@ -0,0 +1,15 @@
<h1>Sinatra + Sidekiq Example</h1>
<h2>Failed: <%= @failed %></h2>
<h2>Processed: <%= @processed %></h2>
<form method="post" action="/msg">
<input type="text" name="msg">
<input type="submit" value="Add Message">
</form>
<a href="/">Refresh page</a>
<h3>Messages</h3>
<% @messages.each do |msg| %>
<p><%= msg %></p>
<% end %>

@ -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
Loading…
Cancel
Save