Skip to content

Commit d8995ee

Browse files
committed
Adding clockwork to recurrently fetch feeds
It will replace Scheduler (for deployment on Heroku), so that no extra step is needed due to another spawn command being called on unicorn.rb starting clockwork. Still on Heroku, Procfile is updated, describing the worker and clock components (if the user wants a more robust architecture). This fixes #411 Also, for development environments, it will allow fetching feeds without having to manually running rake commands. The default interval for fetching feeds is 10 min and it can be customized by env var FETCH_INTERVAL.
1 parent 0dd251c commit d8995ee

File tree

5 files changed

+31
-2
lines changed

5 files changed

+31
-2
lines changed

Diff for: Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ end
2727
gem "activerecord", "~> 4.1.11"
2828
gem "arel", "~> 5.0"
2929
gem "bcrypt-ruby", "~> 3.1.2"
30+
gem "clockwork", "~> 1.2"
3031
gem "delayed_job", "~> 4.1"
3132
gem "delayed_job_active_record", "~> 4.1"
3233
gem "feedbag", "~> 0.9.2"

Diff for: Gemfile.lock

+7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ GEM
3030
rack (>= 1.0.0)
3131
rack-test (>= 0.5.4)
3232
xpath (~> 2.0)
33+
clockwork (1.2.0)
34+
activesupport
35+
tzinfo
3336
coderay (1.1.0)
3437
columnize (0.3.6)
3538
coveralls (0.7.0)
@@ -187,6 +190,7 @@ DEPENDENCIES
187190
arel (~> 5.0)
188191
bcrypt-ruby (~> 3.1.2)
189192
capybara (~> 2.4.1)
193+
clockwork (~> 1.2.0)
190194
coveralls (~> 0.7)
191195
delayed_job (~> 4.1)
192196
delayed_job_active_record (~> 4.1)
@@ -217,3 +221,6 @@ DEPENDENCIES
217221
timecop (~> 0.7.1)
218222
unicorn (~> 4.7)
219223
will_paginate (~> 3.0, >= 3.0.5)
224+
225+
BUNDLED WITH
226+
1.11.2

Diff for: Procfile

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
2+
worker: bundle exec rake work_jobs
3+
clock: bundle exec clockwork lib/clock.rb
24
console: bundle exec racksh

Diff for: config/unicorn.rb

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
timeout 30
33
preload_app true
44

5-
@delayed_job_pid = nil
5+
work_pids = {}
66

77
before_fork do |_server, _worker|
88
# the following is highly recommended for Rails + "preload_app true"
99
# as there's no need for the master process to hold a connection
1010
ActiveRecord::Base.connection.disconnect! if defined?(ActiveRecord::Base)
1111

12-
@delayed_job_pid ||= spawn("bundle exec rake work_jobs")
12+
unless ENV["WORKER_EMBEDDED"] == "false"
13+
work_pids[:delayed_job] ||= spawn("bundle exec rake work_jobs")
14+
work_pids[:recurring_jobs] ||= spawn("bundle exec clockwork lib/clock.rb")
15+
end
1316

1417
sleep 1
1518
end

Diff for: lib/clock.rb

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require "clockwork"
2+
require "delayed_job"
3+
require "delayed_job_active_record"
4+
5+
require_relative "../app"
6+
require_relative "../app/jobs/fetch_feed_job"
7+
require_relative "../app/repositories/feed_repository"
8+
9+
include Clockwork
10+
11+
fetch_interval = (ENV["FETCH_INTERVAL"] || 10*60).to_i # Fetch every 10 minutes by default
12+
every(fetch_interval.seconds, "clockwork.frequent") do
13+
FeedRepository.list.each do |feed|
14+
Delayed::Job.enqueue FetchFeedJob.new(feed.id)
15+
end
16+
end

0 commit comments

Comments
 (0)