Post Scheduling in Ruby Blog - ruby-on-rails

I'm working on post scheduling model by using gem "whenever" with status: published_at, schedule and drafts. But the problem is at given time post is not changing it's status from Schedule to Published_at.
#schedule.rb
every 1.minute do
rake 'scheduler'
end
#example.rake
task scheduler: :environment do
time = Time.zone.now
posts = Post.scheduled.where(published_at: (time))
posts.update_all(status: "Published")
end

task scheduler: :environment do
Post.scheduled.publish_now!
end
in your model post.rb add this line:
def self.publish_now!
where(published_at: Time.now).update_all(status: "Published")
end
I think it's too risk to search by Time.now because it will get exact time in second, but your scheduler is every 1.minutes, so you will have possibility to miss Post because of gaps 60 seconds. So that better you query like this:
def publish_now!
where("status != ? AND published_at <= now()", "Published").update_all(status: "Published")
end

Related

Using Simple Scheduler Gem for Scheduling Tasks in a Rails App

I am trying to run a method that adds the response from an API call to Cache, I decided to use the simple_scheduler gem
Below are snippets of code that I am running
# update_cache_job.rb
class UpdateCacheJob < ActiveJob::Base
def perform
return QueuedJobs.new.update_cache
end
end
And
# simple_scheduler.yml
# Global configuration options. The `queue_ahead` and `tz` options can also be set on each task.
queue_ahead: 120 # Number of minutes to queue jobs into the future
queue_name: "default" # The Sidekiq queue name used by SimpleScheduler::FutureJob
tz: "nil" # The application time zone will be used by default if not set
# Runs once every 2 minutes
simple_task:
class: "UpdateCacheJob"
every: "2.minutes"
And the method I have scheduled to run every 2.minutes
class QueuedJobs
include VariableHelper
def initialize; end
def update_cache
#variables = obtain_software_development_table
# First refresh the project Reviews
puts 'Updating reviews...'
#records = Dashboard.new.obtain_projects_reviews.pluck(
obtain_project_reviews_student_variable,
obtain_project_reviews_id_variable,
'Project'
).map { |student, id, project| { 'Student': student, 'ID': id,
'Project': project } }
Rails.cache.write(
:reviews,
#records,
expires_in: 15.minutes
)
#grouped_reviews = Rails.cache.read(
:reviews
).group_by do |review|
review[:Student]&.first
end
puts 'reviews refreshed.'
# Then refresh the coding challenges submissions
puts "Updating challenges submissions.."
#all_required_submissions_columns = Dashboard.new.all_coding_challenges_submissions.all.map do |submission|
{
id: submission.id,
'Student': submission[obtain_coding_chall_subm_student_var],
'Challenge': submission[obtain_coding_chall_subm_challenge_var]
}
end
#all_grouped_submissions = #all_required_submissions_columns.group_by { |challenge| challenge[:Student]&.first }
Rails.cache.write(
:challenges_submissions,
#all_grouped_submissions,
expires_in: 15.minutes
)
puts "challenges submissions refreshed."
end
end
I have been able to reach these methods from the rails console but when ever I run rake simple_scheduler It just logs the first puts and sometimes it does nothing at all.
What do I need to do here?

How to fetch records in batches in rails for a specific condition?

I am writing a rake task, to populate a "Product" object records. my current logic is
namespace :populate_product do
desc "This task is to populate product object, with product ID"
task populate_coaching_product_id: :environment do
UserProduct.find_each(batch_size: 10_000) do |user_product|
user_product.save
end
end
end
Now in the above query, I want to fetch records which are created 90 days back, from now. How can I change the above query?
Ref this
Something like
Person.where("age > 21").find_in_batches do |group|
sleep(50) # Make sure it doesn't get too crowded in there!
group.each { |person| person.party_all_night! }
end
For the records which are created 90 days back use
UserProduct.where('created_at <= ?', Time.now - 90.days ).find_each(batch_size: 10000) do |user_product|
user_product.save
end
You can try:
UserProduct.where('created_at >= ?', Time.now - 90.days ).find_each(batch_size: 10_000) do |user_product|
user_product.save
end
Note: If you don't care about hour & minute, you can use:
Date.today - 90.days
Hope this helps.

Automatically Generating Daily Posts For A Blog With Ruby On Rails

Currently I have a rake task which I will run daily with the Heroku Scheduler.
It currently will generate a new post for the user every day when the rake task is executed as long as today's date is after the "start date" of the users account.
This is the code for the rake task:
namespace :abc do
desc "Used to generate a new daily log"
task :create_post => :environment do
User.find_each do |currentUser|
starting_date = currentUser.start_date
Post.create!(content: "RAKED", user: currentUser, status: "new") if Date.today >= starting_date && Date.today.on_weekday?
end
puts "It worked yo"
end
end
My problem is if someone makes an account then sets their start date sometime in the past (so they can fill in old posts) my current rake task will not generate the backdated daily posts. Does anyone have any ideas about how to resolve this so that the rake task still performs its current job but also deals with this case?
namespace :abc do
desc "Used to generate a new daily log"
task :create_post => :environment do
User.find_each do |currentUser
starting_date = currentUser.start_date
if Date.today >= starting_date && Date.today.on_weekday?
if currentUser.posts.count.zero?
starting_date.upto(Date.today) { |date| currentUser.generate_post if date.on_weekday? }
else
currentUser.generate_post
end
end
end
puts "It actually worked yo!"
end
end
In User model,
def generate_post
posts.create!(content: "RAKED", status: "new")
end
Your logic remains the same, I just loopes over the starting date to the current date to create backdated posts. Checking post count to zero will ensure that the condition is true only for the new user/user whose posts are not created earlier.
Hope it helps..

rake task to expire customers points balance

i am trying to work out how to write a rake tasks that will run daily and find where the days remaining is 0 to update the column amount to zero.
I have the following methods defined in my model, though they don't exactly appear to be working as I am getting the following error in the view
undefined method `-#' for Mon, 27 Jun 2016:Date
def remaining_days
expired? ? 0 : (self.expire_at - Date.today).to_i
end
def expired?
(self.expire_at - Date.today).to_i <= 0
end
def expire_credits
if expired?
self.update(:expire_at => Date.today + 6.months, :amount => 0)
end
end
with the rake tasks i have never written of these and i thought i would be able to call a method of StoreCredit that would expire the points if certain conditions are met but i am not sure how this all works
task :expire_credits => :environment do
puts 'Expiring unused credits...'
StoreCredit.expire_credits
puts "done."
end
# model/store_credit.rb
# get all store_credits that are expired on given date, default to today
scope :expire_on, -> (date = Date.current) { where("expire_at <= ?", date.beginning_of_day) }
class << self
def expire_credits!(date = Date.current)
# find all the expired credits on particular date, and update all together
self.expire_on(date).update_all(amount: 0)
end
end
Since it's a rake task, I think it's more efficient to update all expired ones together
#rake file
result = StoreCredit.expire_credits!
puts "#{result} records updated"
Retrieve Record Count Update
class << self
def expire_credits!(date = Date.current)
# find all the expired credits on particular date, and update all together
records = self.expire_on(date)
records.update_all(amount: 0)
records.length
end
end
You call class method but define instance method. You will need to define class method:
def self.expire_credits

Destroying a Rails 3 object in rake?

I'm stuck on a simple issue here. I'm building an application that manages a database of coupons, each of which has an expiration date. I'm trying to build a rake task that will delete the expired coupons. The relevant code from the rakefile looks like this:
desc "Deletes expired offers from the database."
task :purge_expired => :environment do
today = Date.today.to_s
Offer.where('expires_on < ?', today).destroy
end
That however fails with the following error message:
rake aborted!
wrong number of arguments (0 for 1)
I'm just not sure why. What arguments would be needed?
As an experiment, I found that this worked fine:
desc "Deletes expired offers from the database."
task :purge_expired => :environment do
today = Date.today.to_s
puts Offer.where('expires_on < ?', today).count
end
That returned the right number of records, so I assume I'm successfully gathering up the right objects.
FWIW, I tried this too, and had no luck:
desc "Deletes expired offers from the database."
task :purge_expired => :environment do
today = Date.today.to_s
#offers = Offer.where('expires_on < ?', today)
#offers.destroy
end
So I'm kind of out of ideas. What am I doing wrong here?
Thanks so much for your help. I'm pretty sure I wouldn't have a job if it weren't for Stack Overflow!
You're close. You just need to use the #destroy_all method instead of #destroy. The latter requires an id argument.
today = Date.today.to_s
Offer.where('expires_on < ?', today).destroy_all
First off, to help debug things from rake, invoke it with the --trace option. Your issue here isn't rake specific though.
The Offer.where('expires_on < ?', today) is going to return a collection, and not a single instance of Offer and there isn't a destroy method available for the collection.
You can iterate over each expired offer and call destroy. Something like this:
#offers = Offer.where('expires_on < ?', today)
#offers.each { |offer| offer.destroy }

Resources