Skip to content

Commit

Permalink
Added Sample Cron Job to check working of Whenever Gem (#2715)
Browse files Browse the repository at this point in the history
*  Added Sample Cron Job to test Whenever Gem

* Added make targets for deployment and cron service start

* Added make to image

* Tweak Makefile

* Fix cron starting command in Makefile

* Trim whitespace

* Explicitly set jessie as base container

* Fixed codeclimate issues
  • Loading branch information
namangupta01 authored and jywarren committed May 25, 2018
1 parent 6614c9e commit 377a09e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ ENV PHANTOMJS_VERSION 2.1.1
# > /etc/apt/sources.list

# Install dependencies
RUN apt-get update -qq && apt-get install -y bundler libmysqlclient-dev ruby-rmagick libfreeimage3 nodejs-legacy npm wget procps cron
RUN apt-get update -qq && apt-get install -y bundler libmysqlclient-dev ruby-rmagick libfreeimage3 nodejs-legacy npm wget procps cron make
RUN wget https://github.com/Medium/phantomjs/releases/download/v$PHANTOMJS_VERSION/phantomjs-$PHANTOMJS_VERSION-linux-x86_64.tar.bz2 -O /tmp/phantomjs-$PHANTOMJS_VERSION-linux-x86_64.tar.bz2; tar -xvf /tmp/phantomjs-$PHANTOMJS_VERSION-linux-x86_64.tar.bz2 -C /opt ; cp /opt/phantomjs-$PHANTOMJS_VERSION-linux-x86_64/bin/* /usr/local/bin/
RUN npm install -g bower

Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ deploy-container:
bash -c "test -e tmp/pids/server.pid && rm tmp/pids/server.pid"
docker-compose up -d
docker-compose exec -T web bash -c "echo 172.19.0.1 smtp >> /etc/hosts"
docker-compose exec -T web bundle exec whenever --update-crontab
docker-compose exec -T web service cron start

install-dev:
echo "Installing RubyGems"
Expand Down
21 changes: 10 additions & 11 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,16 +238,15 @@ def daily_note_tally(span = 365)
end
days
end


def weekly_comment_tally(span = 52)
weeks = {}
(0..span).each do |week|
weeks[span - week] = Comment.select(:timestamp)
.where( uid: drupal_user.uid,
status: 1,
timestamp: Time.now.to_i - week.weeks.to_i..Time.now.to_i - (week - 1).weeks.to_i)
.count
.where( uid: drupal_user.uid,
status: 1,
timestamp: Time.now.to_i - week.weeks.to_i..Time.now.to_i - (week - 1).weeks.to_i)
.count
end
weeks
end
Expand Down Expand Up @@ -357,15 +356,15 @@ def content_followed_in_period(start_time, end_time)
tagnames = TagSelection.where(following: true, user_id: uid)
node_ids = []
tagnames.each do |tagname|
node_ids = node_ids + NodeTag.where(tid: tagname.tid).collect(&:nid)
node_ids += NodeTag.where(tid: tagname.tid).collect(&:nid)
end

Node.where(nid: node_ids)
.includes(:revision, :tag)
.references(:node_revision)
.where("(created >= #{start_time.to_i} AND created <= #{end_time.to_i}) OR (timestamp >= #{start_time.to_i} AND timestamp <= #{end_time.to_i})")
.order('node_revisions.timestamp DESC')
.uniq
.includes(:revision, :tag)
.references(:node_revision)
.where("(created >= #{start_time.to_i} AND created <= #{end_time.to_i}) OR (timestamp >= #{start_time.to_i} AND timestamp <= #{end_time.to_i})")
.order('node_revisions.timestamp DESC')
.uniq
end

def social_link(site)
Expand Down
9 changes: 9 additions & 0 deletions config/schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,12 @@
# end

# Learn more: http://github.com/javan/whenever

# Cron Job log file
set :output, "#{Dir.pwd}/public/cron_log.log"

# To simply print date into the log file for checking if cron job is working properly
every 1.minutes do
puts Dir.pwd
command "date -u" #This will print utc time every 1 min in log/cron_log.log file
end
Empty file added public/cron_log.log
Empty file.

0 comments on commit 377a09e

Please sign in to comment.