Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Permission denied on lockfile when loading Rails Console #186

Closed
emaiax opened this issue Dec 15, 2015 · 11 comments
Closed

Permission denied on lockfile when loading Rails Console #186

emaiax opened this issue Dec 15, 2015 · 11 comments

Comments

@emaiax
Copy link

emaiax commented Dec 15, 2015

I've configured rufus-schedule to use some lockfile on production just like these:

require "rufus-scheduler"

options = Rails.env.test? ? {} : { lockfile: "/tmp/.rufus-scheduler.lock", discard_past: false }
s = Rufus::Scheduler.singleton(options)

And when I try to access the production console:

$ bundle exec rails c
/opt/rubies/ruby-2.2.2/lib/ruby/gems/2.2.0/gems/rufus-scheduler-3.1.10/lib/rufus/scheduler/locks.rb:66:in `initialize': Permission denied @ rb_sysopen - /tmp/.rufus-scheduler.lock (Errno::EACCES)
    from /opt/rubies/ruby-2.2.2/lib/ruby/gems/2.2.0/gems/rufus-scheduler-3.1.10/lib/rufus/scheduler/locks.rb:66:in `new'
    from /opt/rubies/ruby-2.2.2/lib/ruby/gems/2.2.0/gems/rufus-scheduler-3.1.10/lib/rufus/scheduler/locks.rb:66:in `lock'
    from /opt/rubies/ruby-2.2.2/lib/ruby/gems/2.2.0/gems/rufus-scheduler-3.1.10/lib/rufus/scheduler.rb:107:in `initialize'
    from /opt/rubies/ruby-2.2.2/lib/ruby/gems/2.2.0/gems/rufus-scheduler-3.1.10/lib/rufus/scheduler.rb:116:in `new'
    from /opt/rubies/ruby-2.2.2/lib/ruby/gems/2.2.0/gems/rufus-scheduler-3.1.10/lib/rufus/scheduler.rb:116:in `singleton'
    from /var/app/current/config/initializers/scheduler.rb:4:in `<top (required)>'

That means my app runs under a different user than my ssh user, but I think my scheduler shouldn't be loaded on console. Also, we use this filelock because we run Puma with a few threads and they've been starting the scheduler multiple times.

Is there any approach that I've been missing?

@emaiax emaiax changed the title Permission denied on file_lock when loading Rails Console Permission denied on lockfile when loading Rails Console Dec 15, 2015
@jmettraux
Copy link
Owner

This is more a Ruby on Rails question than a "rufus-schedule" question.

but I think my scheduler shouldn't be loaded on console

You're in charge, you could take inspiration from http://stackoverflow.com/questions/4603704/how-can-i-detect-if-my-code-is-running-in-the-console-in-rails-3 and avoid scheduling when the initializer is run in console mode.

I don't know what version of Ruby on Rails you're using but tell me if that StackOverflow if block works and I'll add something to the Ruby on Rails section of rufus-scheduler's readme.

Thanks in advance.

@emaiax
Copy link
Author

emaiax commented Dec 15, 2015

@jmettraux thanks, your tip actually worked for me.

You're in charge, you could take inspiration from http://stackoverflow.com/questions/4603704/how-can-i-detect-if-my-code-is-running-in-the-console-in-rails-3 and avoid scheduling when the initializer is run in console mode.

Thinking about it again, I guess you're right. The whole problem isn't the lockfile, but how I'm trying to do things with different permissions in this context.

Thanks for you time, man 🚀 👯

@jmettraux
Copy link
Owner

You're welcome.

What version of Ruby on Rails are you using?

@emaiax
Copy link
Author

emaiax commented Dec 15, 2015

Oh, I'm sorry. Ruby 2.2.2, Rails 4.2.5.

@jmettraux
Copy link
Owner

Thanks!

jmettraux added a commit that referenced this issue Dec 15, 2015
@jmettraux
Copy link
Owner

@unjuli
Copy link
Contributor

unjuli commented May 5, 2016

In accordance with the option to avoid scheduling when running the rails console, is there a similar option to avoid scheduling in other scenarios where the rails environment reloads, such as when running migrations and other utilities using 'rake'. Or perhaps, a way to restrict the scheduling to rails server only.

Thanks in advance. :)

@jmettraux
Copy link
Owner

Hello,

I had a look at http://stackoverflow.com/questions/2467208/how-can-i-tell-if-rails-code-is-being-run-via-rake-or-script-generate

You could do something like:

unless defined?(Rails::Console) || File.split($0).last == 'rake'

  # not called from the rails console nor from a rake task, do schedule!

  # scheduling...
end

@unjuli
Copy link
Contributor

unjuli commented May 5, 2016

This works perfect. Thanks a lot for the quick response. :)

jmettraux added a commit that referenced this issue May 5, 2016
@aesyondu
Copy link

aesyondu commented Sep 4, 2020

Hi, I would like to add, if using bin/spring (https://stackoverflow.com/questions/39081940/determine-if-using-rails-console-or-server-when-using-spring):

unless $PROGRAM_NAME.include?('spring') do
  # code
end

rails 4.2.6
spring 2.0.0
rufus-scheduler 3.6.0

When I use bin/rails console the defined?(Rails::Console) is false, so I used an alternative instead.

@jmettraux
Copy link
Owner

@aesyondu Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants