How to solve whenever gem error “Trying to perform task XXX more than once, stopped by the lock”

67 views
Skip to first unread message

valentina...@gmail.com

unread,
May 14, 2014, 7:18:51 PM5/14/14
to whenev...@googlegroups.com

I'm using Rails 3.2.17 and whenever gem 0.9.0

On my schedule.rb file I have:

every '0 4 1 * *' do
  scheduled_task 'myapp:payments:all_users:pending_fee_notification'
end

every '0 4 7 * *' do
  scheduled_task 'myapp:payments:all_users:pending_fee_last_notification'
end

The schedule file is being read correctly, but those are not being executed. I looked into the log and found:

Trying to perform task "myapp:payments:all_users:pending_fee_last_notification" more than once, stopped by the lock

Trying to perform task "myapp:payments:all_users:pending_fee_last_notification" more than once, stopped by the lock

Any ideas why this is happening?

Thanks!

Javan Makhmali

unread,
May 17, 2014, 1:55:07 PM5/17/14
to whenev...@googlegroups.com
What does `scheduled_task` do? Are you using lockrun (http://www.unixwiz.net/tools/lockrun.html)?

-Javan


--
--
You received this message because you are subscribed to the Google
Groups "Whenever Gem" group and because you're awesome.
To post to this group, send email to whenev...@googlegroups.com
To unsubscribe from this group, send email to
whenever-gem...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/whenever-gem?hl=en
---
You received this message because you are subscribed to the Google Groups "Whenever Gem" group.
To unsubscribe from this group and stop receiving emails from it, send an email to whenever-gem...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

valentina...@gmail.com

unread,
May 20, 2014, 1:01:12 PM5/20/14
to whenev...@googlegroups.com
Hi there Javan,

No, we are not using lockrun apparently.

and here's the rest of the config file:

set :path, "/webapps/myapp/current"
set :cron_log, "/webapps/myapp/current/log/scheduler/standard.log"
set :command, "/usr/local/bin/bundle exec rails runner"
set :require_module, "require 'myapp/scheduled_task';"
set :myapp_module, "MyApp::ScheduledTask"

job_type :scheduled_task, 'cd :path && :command -e :environment ":require_module :myapp_module.run(\':task\')" :output'

Javan Makhmali

unread,
May 20, 2014, 1:42:29 PM5/20/14
to whenev...@googlegroups.com
I would investigate MyApp::ScheduledTask - It probably has some kind of locking logic.

valentina...@gmail.com

unread,
May 20, 2014, 5:11:05 PM5/20/14
to whenev...@googlegroups.com
Indeed it is my ScheduleTask the one blocking the task, however I don't see anything wrong in it:

require File.expand_path('../../../config/application', __FILE__)
require 'rake'

MyApp::Application.load_tasks

module MyApp
  class ScheduledTask
    def self.run(task_name)
      ensure_block_file(task_name) do
        output("rake #{task_name}")
        start_time = Time.now
        rake(task_name)
        duration = Time.now - start_time
        output("done (took #{duration} seconds)")
      end
    end

    private

    def self.ensure_block_file(task_name, &block)
      file_name = '/tmp/myapp_%s.lock' % task_name
      if File.exist?(file_name)
        $stdout.puts "Trying to perform task \"#{task_name}\" more than once, stopped by the lock"
      else
        File.open(file_name, "w") { |out| out.puts "busy" }
        block.call
        File.delete(file_name)
      end
    end

    def self.rake(task_name)
      Rake::Task[task_name].reenable
      Rake::Task[task_name].invoke
    rescue Exception => exception
      Airbrake.notify(exception)
    end

    def self.output(text)
      Time.zone = "Pacific Time (US & Canada)"
      time = DateTime.now.in_time_zone.strftime("%Y-%m-%d %H:%M:%S PST")
      $stdout.puts "\x1B[32m#{time}: #{text}\x1B[0m"
    end
  end
end


Do you?

Thanks!

Javan Makhmali

unread,
May 21, 2014, 10:18:30 AM5/21/14
to whenev...@googlegroups.com
If `block.call` raises an exception, the file will never be deleted and the task will remain locked. Chances are, you have an old /tmp/myapp_*s.lock file hanging around, which you should probably delete.

Here's one way to ensure the lock file is always deleted:

begin
  File.open(file_name, "w") { |out| out.puts "busy" }
  block.call
ensure
  File.delete(file_name)
end

-Javan

valentina...@gmail.com

unread,
May 21, 2014, 12:32:25 PM5/21/14
to whenev...@googlegroups.com
Thank you so much!!!
Reply all
Reply to author
Forward
0 new messages