begin
# Initialise the Rufus scheduler object.
require 'rufus/scheduler'
scheduler = Rufus::Scheduler.start_new
scheduler.start
# Check every minute to see if any reminders need to be sent
scheduler.schedule_every('1m') do
RAILS_DEFAULT_LOGGER.info "CHECK FOR AND SEND REMINDERS"
ActivityReminder.send_reminders
end
# Tell the scheduler to perform these jobs until the
# process is stopped.
scheduler.join
rescue => e
RAILS_DEFAULT_LOGGER.warn "Exception in schedule: #{e.inspect}"
exit
end
end
> My scheduled task runs great, but once the process is complete it does > not let go of the database connection.
> Since the process is running every minute, I run out of MySQL database > connections very quickly resulting in a MySql "Too many connections" > error.
> Does anybody have an idea about why this could be happening?
Hi Jim,
no way to recycle / reuse the connection instead of setting up a new one each time ?
Thanks for the response, John! I'm not sure how I would reuse a
connection within the send_reminder() method.
I added a "remove_connection" in my scheduler block which seems to
have done the trick.
Does anybody see an issue with this solution?
# Check every minute to see if any reminders need to be sent
scheduler.schedule_every('1m') do
ActivityReminder.send_reminders
ActiveRecord::Base.remove_connection
end
On Sep 12, 12:11 am, "John Mettraux" <jmettr...@openwfe.org> wrote:
> > My scheduled task runs great, but once the process is complete it does
> > not let go of the database connection.
> > Since the process is running every minute, I run out of MySQL database
> > connections very quickly resulting in a MySql "Too many connections"
> > error.
> > Does anybody have an idea about why this could be happening?
> Hi Jim,
> no way to recycle / reuse the connection instead of setting up a new
> one each time ?
> I added a "remove_connection" in my scheduler block which seems to > have done the trick.
> Does anybody see an issue with this solution?
> # Check every minute to see if any reminders need to be sent > scheduler.schedule_every('1m') do > ActivityReminder.send_reminders > ActiveRecord::Base.remove_connection > end
Hi Jim,
I guess you're using ActiveRecord 2.1.0. I've taken a look at #remove_connection :
Seems like it tries to have 1 connection per thread (it uses the thread's object id to map the connection).
The rufus-scheduler triggers the jobs in their own threads, so as not to block the whole scheduler. I think it matches your description of the symptoms (too many open connections).
Thanks for finding this out, it's not a direct rufus-scheduler issue, but I should document it anyway.
Thanks for taking a deeper look into this. So I guess as long as my
processes take < 1 minute (or whatever the time is I specify) I should
be okay. I should probably test to see what happens if the process
goes longer than the specified interval time.
Thanks again!
Jim
On Sep 12, 11:24 am, "John Mettraux" <jmettr...@openwfe.org> wrote:
> > I added a "remove_connection" in my scheduler block which seems to
> > have done the trick.
> > Does anybody see an issue with this solution?
> > # Check every minute to see if any reminders need to be sent
> > scheduler.schedule_every('1m') do
> > ActivityReminder.send_reminders
> > ActiveRecord::Base.remove_connection
> > end
> Hi Jim,
> I guess you're using ActiveRecord 2.1.0. I've taken a look at
> #remove_connection :
> Seems like it tries to have 1 connection per thread (it uses the
> thread's object id to map the connection).
> The rufus-scheduler triggers the jobs in their own threads, so as not
> to block the whole scheduler. I think it matches your description of
> the symptoms (too many open connections).
> Thanks for finding this out, it's not a direct rufus-scheduler issue,
> but I should document it anyway.
> Thanks for taking a deeper look into this. So I guess as long as my > processes take < 1 minute (or whatever the time is I specify) I should > be okay. I should probably test to see what happens if the process > goes longer than the specified interval time.
Hi Jim,
I think your solution is right, it seems that ActiveRecord is creating a connection for each of the threads the scheduler spawns that does work with the DB. So releasing the connection once the job is done is a good thing.
What happens if you have multiple scheduled Rails tasks? How will it
know which connection to kill? It might be better to consider
disabling connection pooling in your Rufus environment.
On Sep 14, 8:56 am, "John Mettraux" <jmettr...@openwfe.org> wrote:
> > Thanks for taking a deeper look into this. So I guess as long as my
> > processes take < 1 minute (or whatever the time is I specify) I should
> > be okay. I should probably test to see what happens if the process
> > goes longer than the specified interval time.
> Hi Jim,
> I think your solution is right, it seems that ActiveRecord is creating
> a connection for each of the threads the scheduler spawns that does
> work with the DB. So releasing the connection once the job is done is
> a good thing.