I have two models: Schedule and Task. Task belongs_to Schedule and Schedule has_many tasks. I have a nested form for task that uses javascript to dynamically create more task forms by pressing an "add task" button. I am trying to use rufus_scheduler to make it so that a task has an end date and end time that the user inputs. When a task if complete, It should simply print "yo yo yo" to the server.
I was successfully able to have the first task complete in an interval of seconds with the following code:
def set_schedule
scheduler = Rufus::Scheduler.new
scheduler.in self.tasks.first.timeframe do
puts 'yo yo yo'
end
end
In the code above, timeframe is an integer field that the user inputs with the given number of seconds before which the job should occur.
The problem is that the code above uses an interval and I want to use an exact date and time. So I changed timeframe to a string and have the user input a date in this format: '12/18/2013'. Here's what I tried:
def set_schedule
scheduler = Rufus::Scheduler.new
scheduler.at (self.tasks.first.timeframe + " " + self.tasks.first.complete_time.to_s + " EST")do
puts 'yo yo yo'
end
end
Just to be clear, timeframe is a string that consists of a date and complete_time is a time field that consists of the time the task should end.
This code did not work. "yo yo yo" still prints but it just prints right away.
I am running rails on webrick with a postgres database. I am using Ruby on Rails 4 and Ruby 2. I'd appreciate any help. Thanks!
UPDATE:
I realized two errors in my code. complete_time was being converted into an unreadable format automatically. I fixed this by changing complete_time into a string. Additionally, I realized my argument for the .at method was not in quotes. I fixed this. However, I am still having the same problem. Rather than waiting until the said time to put "yo yo yo", it is putting it right away. Here is the new code:
def set_schedule
scheduler = Rufus::Scheduler.new
scheduler.at ("#{self.tasks.first.timeframe} #{self.tasks.first.complete_time} EST")do
puts 'yo yo yo'
end
end
The scheduler.at method goes to the server as this:
scheduler.at("12/03/2013 12:33:00 EST")
Ofcourse the time and date change deepening on the users input, the code above is just the last time i ran it.
When rufus-scheduler #at or #in triggers its block immediately it means the schedule time was in the past.
Maybe it's a timezone issue.
You can check how rufus-scheduler sees your string by doing:
On my system (Debian GNU Linux, Ruby 1.9.3p392 or Ruby 2.0.0p195), this yields: