Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Twitter API update and other improvements #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 29 additions & 29 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
GEM
remote: http://rubygems.org/
specs:
faraday (0.8.1)
multipart-post (~> 1.1)
iron_core (0.3.3)
rest (>= 2.0.0)
iron_mq (2.1.0)
iron_core (>= 0.2.0)
iron_worker_ng (0.9.6)
bundler (> 1.0.0)
iron_core (>= 0.3.0)
zip
mime-types (1.19)
multi_json (1.3.6)
multipart-post (1.1.5)
net-http-persistent (2.7)
rack (1.4.1)
rack-protection (1.2.0)
faraday (0.8.8)
multipart-post (~> 1.2.0)
iron_core (1.0.1)
rest (>= 2.2.0)
iron_mq (4.0.3)
iron_core (>= 0.5.1)
iron_worker_ng (1.0.2)
bundler (>= 1.2.0)
iron_core (>= 1.0.0)
rubyzip (= 0.9.9)
mime-types (1.25)
multi_json (1.8.1)
multipart-post (1.2.0)
net-http-persistent (2.9)
rack (1.5.2)
rack-protection (1.5.0)
rack
rest (2.0.2)
rest (2.6.3)
net-http-persistent
rest-client (>= 0.3.0)
rest-client (1.6.7)
mime-types (>= 1.16)
simple_oauth (0.1.9)
sinatra (1.3.2)
rack (~> 1.3, >= 1.3.6)
rack-protection (~> 1.2)
tilt (~> 1.3, >= 1.3.3)
tilt (1.3.3)
twitter (3.4.1)
faraday (~> 0.8)
multi_json (~> 1.3)
simple_oauth (~> 0.1.6)
uber_config (0.0.6)
zip (2.0.2)
rubyzip (0.9.9)
simple_oauth (0.2.0)
sinatra (1.4.3)
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (~> 1.3, >= 1.3.4)
tilt (1.4.1)
twitter (4.8.1)
faraday (~> 0.8, < 0.10)
multi_json (~> 1.0)
simple_oauth (~> 0.2)
uber_config (1.1.0)

PLATFORMS
ruby
Expand Down
26 changes: 23 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,35 @@ And put them in a file in this directory called `iron.json` in this format:

```javascript
{
"project_id":"PROJECT_ID",
"token":"TOKEN"
"project_id": "PROJECT_ID",
"token": "TOKEN"
}
```

Upload the worker:

iron_worker upload workers/TweetWorker


Now, open and fill configuration file named `config.json`:

```javascript
{
// This section is optional, environment variables have higher priority
"iron": {
"project_id": "PROJECT_ID",
"token": "TOKEN"
},
// Twitter API requires authorization since v1.1
"twitter": {
"consumer_key": "YOUR_CONSUMER_KEY",
"consumer_secret": "YOUR_CONSUMER_SECRET",
"oauth_token": "YOUR_OAUTH_TOKEN",
"oauth_token_secret": "YOUR_OAUTH_TOKEN_SECRET"
}
}
```

Then just push to heroku!

git push heroku master
Expand All @@ -56,6 +76,6 @@ to get it.

Then start this sinatra app using this command, replacing my_token and my_project_id with your credentials:

IRON_WORKER_TOKEN=my_token IRON_WORKER_PROJECT_ID=my_project_id rackup -p 3000 config.ru
IRON_IO_TOKEN=my_token IRON_IO_PROJECT_ID=my_project_id rackup -p 3000 config.ru


12 changes: 12 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"iron": {
"token": null,
"project_id": null
},
"twitter": {
"consumer_key": "YOUR_CONSUMER_KEY",
"consumer_secret": "YOUR_CONSUMER_SECRET",
"oauth_token": "YOUR_OAUTH_TOKEN",
"oauth_token_secret": "YOUR_OAUTH_TOKEN_SECRET"
}
}
31 changes: 23 additions & 8 deletions config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,35 @@ rescue => ex
puts "Swallowed error: #{ex.message}"
end
@config = {} unless @config
@config["iron_worker"] ||= {}
@config["iron_worker"]["token"] ||= ENV['IRON_WORKER_TOKEN']
@config["iron_worker"]["project_id"] ||= ENV['IRON_WORKER_PROJECT_ID']
@config["iron_mq"] ||= {}
@config["iron_mq"]["token"] ||= ENV['IRON_MQ_TOKEN']
@config["iron_mq"]["project_id"] ||= ENV['IRON_MQ_PROJECT_ID']

@config["iron_worker"] ||= @config["iron"] || {}
iw_conf_heroku = {
'token' => ENV['IRON_WORKER_TOKEN'] || ENV['IRON_IO_TOKEN'],
'project_id' => ENV['IRON_WORKER_PROJECT_ID'] || ENV['IRON_IO_PROJECT_ID']
}
@config["iron_worker"].merge!(iw_conf_heroku) do |k, v1, v2|
(v2.nil? || v2.empty?) ? v1 : v2
end

@config["iron_mq"] ||= @config["iron"] || {}
imq_conf_heroku = {
'token' => ENV['IRON_MQ_TOKEN'] || ENV['IRON_IO_TOKEN'],
'project_id' => ENV['IRON_MQ_PROJECT_ID'] || ENV['IRON_IO_PROJECT_ID']
}
@config["iron_mq"].merge!(imq_conf_heroku) do |k, v1, v2|
(v2.nil? || v2.empty?) ? v1 : v2
end

p @config

set :iron_worker, IronWorkerNG::Client.new(:token=>@config["iron_worker"]["token"], :project_id=>@config["iron_worker"]["project_id"])
set :iron_worker, IronWorkerNG::Client.new(@config["iron_worker"])

set :queue_name, "tweets"
ironmq = IronMQ::Client.new(:token => @config["iron_mq"]["token"], :project_id => @config["iron_mq"]["project_id"])
ironmq = IronMQ::Client.new(@config["iron_mq"])
#ironmq.logger.level = Logger::DEBUG
set :ironmq, ironmq

set :twitter_conf, @config['twitter']

require './hello'
run Sinatra::Application
5 changes: 4 additions & 1 deletion hello.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
## todo: store worker id in session then ajax show progress
#worker.queue

task = settings.iron_worker.tasks.create("TweetWorker", {:mq=>settings.ironmq.options.merge({:queue_name =>settings.queue_name})})
task = settings.iron_worker.tasks.create("TweetWorker",
{ :mq => settings.ironmq.options,
:queue_name => settings.queue_name,
:twitter => settings.twitter_conf })

session[:worker_id] = task.id
puts 'worker_id in session=' + task.id
Expand Down
59 changes: 27 additions & 32 deletions views/hello.erb
Original file line number Diff line number Diff line change
Expand Up @@ -135,38 +135,33 @@
</form>
</div>

<script>
var keep_going = true;
$.doTimeout(2000, function () {

var jqxhr = $.getJSON("/worker_status", function (json) {
// alert("success");
console.log("json");
console.log(json);
if (json.status != null) {
var msg = "Task status: " + json.status;
if (json.status != "complete") {
// if (json.msg != null) {
msg = msg + "<br/>Percent complete: " + json.percent + "%";
// }
msg = msg + "<br/>" + json.msg;
}
$("#status_div").html(msg);
keep_going = true;
} else {
keep_going = false;
$("#status_indicator").hide();
}
})
// .success(function() { alert("second success"); })
.error(function () {
alert("error");
keep_going = false;
})
// .complete(function() { alert("complete"); });

return keep_going;
});
<script type="text/javascript">
var keep_going = true;
$.doTimeout(2000, function () {
var jqxhr = $.getJSON("/worker_status", function (json) {
if (json.status != null) {
var msg = "Task status: " + json.status;
if (json.status != "complete") {
msg = msg + "<br/>Percent complete: " + json.percent + "%";
msg = msg + "<br/>" + json.msg;
} else {
msg = msg + "<br />Page will be reloaded in 3 seconds."
setInterval(function () { location.reload(); }, 3000);
}
$("#status_div").html(msg);
keep_going = true;
} else {
keep_going = false;
$("#status_indicator").hide();
}
})
.error(function () {
alert("error");
keep_going = false;
})

return keep_going;
});
</script>
<div id="status_container" style="display: none;">
<div id="status_indicator" style="display: inline-block; width: 40px;">
Expand Down
8 changes: 4 additions & 4 deletions workers/tweet_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
puts "Running..."
p params

tweet = Twitter.search("#cloud -rt").results.first.text
twitter = Twitter::Client.new(params['twitter'])
tweet = twitter.search("#cloud -rt").results.first.text
puts "tweet=#{tweet}"

mq_params = params['mq']
ironmq = IronMQ::Client.new('token' => mq_params['token'], 'project_id' => mq_params['project_id'])
response = ironmq.queue(mq_params['queue_name']).post(tweet)
ironmq = IronMQ::Client.new(params['mq'])
response = ironmq.queue(params['queue_name']).post(tweet)

puts "tweet put on queue. " + response.inspect