Skip to content

Commit

Permalink
Merge pull request #269 from NYULibraries/cron-job-to-clear-search
Browse files Browse the repository at this point in the history
Cron job to clear search
  • Loading branch information
the-codetrane authored Aug 8, 2023
2 parents eab1476 + 5843920 commit 85b06f6
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ gem 'sqlite3'
gem 'turbolinks'
gem 'twitter-typeahead-rails'
gem 'uglifier'
gem 'whenever'

# Range limit gem for slider on Solr integer fields (year)
# Currently broken (9/2/2016)
Expand All @@ -43,5 +44,7 @@ group :development, :test do
gem 'selenium-webdriver'
gem 'simplecov'
gem 'spring'
gem 'timecop'
gem 'web-console'
gem 'whenever-test'
end
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ GEM
capybara (>= 1.0, < 4)
launchy
childprocess (4.1.0)
chronic (0.10.2)
coderay (1.1.3)
concurrent-ruby (1.1.7)
config (2.2.1)
Expand Down Expand Up @@ -381,6 +382,7 @@ GEM
thor (1.0.1)
thread_safe (0.3.6)
tilt (2.0.10)
timecop (0.9.6)
turbolinks (5.2.1)
turbolinks-source (~> 5.2)
turbolinks-source (5.2.0)
Expand All @@ -403,6 +405,10 @@ GEM
websocket-driver (0.7.3)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
whenever (1.0.0)
chronic (>= 0.6.3)
whenever-test (1.0.1)
whenever
xpath (3.2.0)
nokogiri (~> 1.8)

Expand Down Expand Up @@ -442,10 +448,13 @@ DEPENDENCIES
solr_wrapper
spring
sqlite3
timecop
turbolinks
twitter-typeahead-rails
uglifier
web-console
whenever
whenever-test

BUNDLED WITH
2.1.4
24 changes: 24 additions & 0 deletions config/schedule.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Use this file to easily define all of your cron jobs.
#
# It's helpful, but not entirely necessary to understand cron before proceeding.
# http://en.wikipedia.org/wiki/Cron

# Example:
#
# set :output, "/path/to/my/cron_log.log"
#
# every 2.hours do
# command "/usr/bin/some_great_command"
# runner "MyModel.some_method"
# rake "some:great:rake:task"
# end
#
# every 4.days do
# runner "AnotherModel.prune_old_records"
# end

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

every 1.week do
rake 'vacate_searches:vacate'
end
6 changes: 6 additions & 0 deletions lib/tasks/vacate_searches.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace :vacate_searches do
desc 'Vacate week-old searches'
task vacate: :environment do
Search.where('created_at < ?', (Date.today - 7.days)).destroy_all
end
end
7 changes: 7 additions & 0 deletions spec/factories/search.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

FactoryBot.define do
factory :search do
query_params { { q: 'test' } }
end
end
7 changes: 7 additions & 0 deletions spec/support/tasks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'rake'

RSpec.configure do |config|
config.before(:suite) do
Rails.application.load_tasks
end
end
32 changes: 32 additions & 0 deletions spec/tasks/vacate_searches_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'timecop'

RSpec.describe 'Vacate Searches', type: :task do
context 'vacate_searches:vacate' do
before do
# Freeze time as task is time-sensitive
Timecop.freeze Time.now
end

after { Timecop.return }

it 'deletes searches older than 1 week' do
create(:search, created_at: 2.week.ago)
expect { invoke_task }.to change { Search.count }.by(-1)
end

it 'does not delete newer searches' do
create(:search, created_at: 2.days.ago)
expect { invoke_task }.not_to(change { Search.count })
end

private

def invoke_task
task = Rake::Task['vacate_searches:vacate']
# Ensure task is re-enabled, as rake tasks by default are disabled
# after running once within a process http://pivotallabs.com/how-i-test-rake-tasks/
task.reenable
task.invoke
end
end
end
14 changes: 14 additions & 0 deletions spec/whenever/schedule_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# spec/whenever_spec.rb
require 'spec_helper'

describe 'Whenever Schedule' do
before do
load 'Rakefile' # Makes sure rake tasks are loaded so you can assert in rake jobs
end

it 'should have a rake task that runs every week' do
schedule = Whenever::Test::Schedule.new(file: 'config/schedule.rb')
task_name = schedule.jobs[:rake].first[:task]
task_name.should include('vacate_searches:vacate')
end
end

0 comments on commit 85b06f6

Please sign in to comment.