From 377a09ec128d5d679a398853db5db38e8be78945 Mon Sep 17 00:00:00 2001 From: Naman Gupta <01namangupta@gmail.com> Date: Sat, 26 May 2018 02:26:19 +0530 Subject: [PATCH] Added Sample Cron Job to check working of Whenever Gem (#2715) * 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 --- Dockerfile | 2 +- Makefile | 2 ++ app/models/user.rb | 21 ++++++++++----------- config/schedule.rb | 9 +++++++++ public/cron_log.log | 0 5 files changed, 22 insertions(+), 12 deletions(-) create mode 100644 public/cron_log.log diff --git a/Dockerfile b/Dockerfile index cd446f7a96..b12319b461 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/Makefile b/Makefile index c7ebfd2092..e1c59f50c5 100644 --- a/Makefile +++ b/Makefile @@ -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" diff --git a/app/models/user.rb b/app/models/user.rb index b7f79d9d3e..3b38338ef3 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 @@ -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) diff --git a/config/schedule.rb b/config/schedule.rb index de75cf9d48..96616deed9 100644 --- a/config/schedule.rb +++ b/config/schedule.rb @@ -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 diff --git a/public/cron_log.log b/public/cron_log.log new file mode 100644 index 0000000000..e69de29bb2