2012-02-13 01:34:37 -05:00
|
|
|
# Make sure you have Sinatra installed, then start sidekiq with
|
|
|
|
# ./bin/sidekiq -r ./examples/sinkiq.rb
|
|
|
|
# Simply run Sinatra with
|
|
|
|
# ruby examples/sinkiq.rb
|
|
|
|
# and then browse to http://localhost:4567
|
|
|
|
#
|
|
|
|
require 'sinatra'
|
2012-04-02 22:21:26 -04:00
|
|
|
require 'sidekiq'
|
|
|
|
require 'redis'
|
2014-05-03 22:40:13 -04:00
|
|
|
require 'sidekiq/api'
|
2012-04-02 22:21:26 -04:00
|
|
|
|
2013-08-09 16:20:27 -04:00
|
|
|
$redis = Redis.new
|
2012-02-13 01:34:37 -05:00
|
|
|
|
|
|
|
class SinatraWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
|
|
|
|
def perform(msg="lulz you forgot a msg!")
|
2012-03-18 23:09:27 -04:00
|
|
|
$redis.lpush("sinkiq-example-messages", msg)
|
2012-02-13 01:34:37 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/' do
|
2012-12-05 11:46:55 -05:00
|
|
|
stats = Sidekiq::Stats.new
|
|
|
|
@failed = stats.failed
|
|
|
|
@processed = stats.processed
|
2012-02-13 01:34:37 -05:00
|
|
|
@messages = $redis.lrange('sinkiq-example-messages', 0, -1)
|
|
|
|
erb :index
|
|
|
|
end
|
|
|
|
|
|
|
|
post '/msg' do
|
|
|
|
SinatraWorker.perform_async params[:msg]
|
|
|
|
redirect to('/')
|
|
|
|
end
|
|
|
|
|
|
|
|
__END__
|
|
|
|
|
|
|
|
@@ layout
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>Sinatra + Sidekiq</title>
|
|
|
|
<body>
|
|
|
|
<%= yield %>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
|
|
|
|
@@ index
|
2012-04-18 06:54:18 -04:00
|
|
|
<h1>Sinatra + Sidekiq Example</h1>
|
2012-02-13 01:34:37 -05:00
|
|
|
<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 %>
|