Permalink
Browse files

First commit, working example.

  • Loading branch information...
0 parents commit e3babe6bb2a8caa1815a8e814aa44b80f970ad77 @treeder treeder committed Dec 14, 2011
Showing with 260 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +6 −0 Gemfile
  3. +49 −0 Gemfile.lock
  4. +10 −0 README.md
  5. +14 −0 config.ru
  6. +33 −0 hello.rb
  7. +125 −0 views/hello.erb
  8. +21 −0 workers/uber_worker.rb
@@ -0,0 +1,2 @@
+.idea/*
+config.yml
@@ -0,0 +1,6 @@
+source 'http://rubygems.org'
+gem 'sinatra'
+
+gem 'iron_worker'
+gem 'ironmq'
+gem 'twitter'
@@ -0,0 +1,49 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ activesupport (3.1.3)
+ multi_json (~> 1.0)
+ addressable (2.2.6)
+ faraday (0.7.5)
+ addressable (~> 2.2.6)
+ multipart-post (~> 1.1.3)
+ rack (< 2, >= 1.1.0)
+ iron_worker (2.1.1)
+ rest-client
+ rest-client
+ zip
+ zip
+ ironmq (1.2.7)
+ typhoeus
+ typhoeus
+ mime-types (1.17.2)
+ multi_json (1.0.4)
+ multipart-post (1.1.4)
+ rack (1.3.5)
+ rack-protection (1.1.4)
+ rack
+ rest-client (1.6.7)
+ mime-types (>= 1.16)
+ simple_oauth (0.1.5)
+ sinatra (1.3.1)
+ rack (>= 1.3.4, ~> 1.3)
+ rack-protection (>= 1.1.2, ~> 1.1)
+ tilt (>= 1.3.3, ~> 1.3)
+ tilt (1.3.3)
+ twitter (2.0.2)
+ activesupport (>= 2.3.9, < 4)
+ faraday (~> 0.7)
+ multi_json (~> 1.0)
+ simple_oauth (~> 0.1)
+ typhoeus (0.3.3)
+ mime-types
+ zip (2.0.2)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ iron_worker
+ ironmq
+ sinatra
+ twitter
@@ -0,0 +1,10 @@
+
+
+First you need a token and project_id from your Iron.io account. Login at http://www.iron.io
+to get it.
+
+To run in development, run:
+
+ IRON_WORKER_TOKEN=my_token IRON_WORKER_PROJECT_ID=my_project_id rackup -p 3000 config.ru
+
+
@@ -0,0 +1,14 @@
+require 'yaml'
+require 'iron_worker'
+
+#@config = YAML.load_file('config.yml')
+
+IronWorker.configure do |iwc|
+ iwc.token = ENV['IRON_WORKER_TOKEN']
+ iwc.project_id = ENV['IRON_WORKER_PROJECT_ID']
+ #iwc.token = @config['iron']['token']
+ #iwc.project_id = @config['iron']['project_id']
+end
+
+require './hello'
+run Sinatra::Application
@@ -0,0 +1,33 @@
+require 'sinatra'
+require_relative 'workers/uber_worker'
+#require_relative ''
+
+set :public_folder, File.dirname(__FILE__) + '/static'
+set :ironmq, IronMQ::Client.new('token'=>ENV['IRON_WORKER_TOKEN'], 'project_id'=>ENV['IRON_WORKER_PROJECT_ID'])
+set :queue_name, "tweets"
+
+post '/run_uber_worker' do
+ worker = UberWorker.new
+ worker.token = ENV['IRON_WORKER_TOKEN']
+ worker.project_id = ENV['IRON_WORKER_PROJECT_ID']
+ puts "settings.queue_name=" + settings.queue_name
+ worker.queue_name = settings.queue_name
+ # todo: store worker id in session then ajax show progress
+ worker.queue
+ worker.wait_until_complete
+ p worker.status
+ "status=" + worker.status.inspect
+end
+
+get '/' do
+ @msg = settings.ironmq.messages.get(:queue_name=>settings.queue_name)
+ erb :hello
+end
+
+get '*' do
+ if request.host == "blog.simpleworker.com"
+ redirect "#{request.scheme}://blog.iron.io#{request.path}"
+ return true
+ end
+ redirect '/'
+end
@@ -0,0 +1,125 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <meta charset='utf-8'>
+
+ <title>Powered by Iron.io</title>
+
+ <style type="text/css">
+ body {
+ margin-top: 1.0em;
+ font-family: Helvetica, Arial, FreeSans, san-serif;
+ }
+
+ a:visited {
+ color: darkblue;
+ }
+
+ /*a:visited#main {*/
+ /*color: red;*/
+ /*}*/
+
+ #container {
+ margin: 0 auto;
+ width: 800px;
+ }
+
+ h1 {
+ font-size: 3.1em;
+ margin-bottom: 3px;
+ /*letter-spacing: -2px;*/
+ }
+
+ h1 .sw {
+ color: #1e90ff;
+ }
+
+ h1 .iw {
+ color: #b22222;
+ }
+
+ h1 .small {
+ font-size: 0.4em;
+ }
+
+ h1 a {
+ text-decoration: none
+ }
+
+ h2 {
+ font-size: 1.5em;
+ color: #1e90ff;
+ }
+
+ h3 {
+ text-align: center;
+ color: #1e90ff;
+ }
+
+ a {
+ color: #1e90ff;
+ }
+
+ .description {
+ font-size: 1.2em;
+ margin-bottom: 30px;
+ margin-top: 30px;
+ }
+
+ .download {
+ float: right;
+ }
+
+ pre {
+ background: #000;
+ color: #fff;
+ padding: 15px;
+ }
+
+ hr {
+ border: 0;
+ width: 80%;
+ border-bottom: 1px solid #aaa
+ }
+
+ .footer {
+ text-align: center;
+ padding-top: 30px;
+ font-style: italic;
+ }
+ </style>
+</head>
+
+<body>
+
+<br/><br/>
+
+<div id="container">
+
+ <h1><a href="http://www.iron.io" id="main">Powered by Iron.io</a></h1>
+
+ <% if @msg %>
+ <div>
+ Latest Tweet:
+ <br/>
+ <span style="font-weight: bold;">
+ <%= @msg.body %>
+ </span>
+ </div>
+ <% end %>
+
+ <div class="description">
+ <form action="/run_uber_worker" method="post">
+ <button>Run UberWorker</button>
+ </form>
+ </div>
+
+
+ <div class="footer">
+ </div>
+
+</div>
+
+</body>
+</html>
+
@@ -0,0 +1,21 @@
+
+class UberWorker < IronWorker::Base
+
+ merge_gem 'ironmq'
+ merge_gem 'twitter'
+
+ attr_accessor :token, :project_id, :queue_name
+
+ def run
+ puts "Hello! I am Uber."
+
+ tweet = Twitter.search("#cloud -rt").first.text
+ puts "tweet=#{tweet}"
+
+ ironmq = IronMQ::Client.new('token'=>token, 'project_id'=>project_id)
+ response = ironmq.messages.post(tweet, :queue_name=>queue_name)
+
+ puts "tweet put on queue. " + response.inspect
+
+ end
+end

0 comments on commit e3babe6

Please sign in to comment.