Skip to content
This repository has been archived by the owner on Mar 7, 2018. It is now read-only.

Accessing Sinatra settings object from within a Job #227

Closed
grahamb opened this issue Sep 9, 2013 · 9 comments
Closed

Accessing Sinatra settings object from within a Job #227

grahamb opened this issue Sep 9, 2013 · 9 comments

Comments

@grahamb
Copy link

grahamb commented Sep 9, 2013

Forgive me if this is obvious; I'm new to Sinatra and Ruby in general. How, if at all, can I access the Sinatra settings object from within a Job? I wanted to have some config settings stored in YAML files, so I added a method to config.ru to read them in and add them to settings. From within config.ru, I can access them (e.g. settings.bamboo[:bamboo_host] but from within a Job file I get a NoMethodError when trying to access settings.bamboo. When I try to access settings.auth_token or settings.defaut_dashboard from my Job, it returns nil.

Here's what I'm doing to read in the config files (from within the configure block in config.ru).

Dir.glob('config/*.yml').each do |f|
  config_name = File.basename(f, '.*')
  raw_config = File.read(f)
  config = YAML.load(raw_config).symbolize_keys
  set config_name.to_sym, Proc.new { config }
end
@pushmatrix
Copy link
Member

Try Sinatra::Application.settings.auth_token

@grahamb
Copy link
Author

grahamb commented Sep 9, 2013

@pushmatrix Hmm. Still nil from the Job file, at least outside the SCHEDULER.every block. Inside that block I can access both settings.auth_token and Sinatra::Application.settings.auth_token. Any idea why?

Here's my job, in both working and non-working form: https://gist.github.com/grahamb/6502637

@pushmatrix
Copy link
Member

Try just Sinatra::Application.auth_token

@grahamb
Copy link
Author

grahamb commented Sep 9, 2013

Still nil

@pushmatrix pushmatrix reopened this Sep 9, 2013
@grahamb
Copy link
Author

grahamb commented Sep 9, 2013

I'm wondering if there's some sort of timing issue. If I have my jobs scheduled using, say
SCHEDULER.every '30s', :first_in => 0 do
I'll get a NoMethod error right after starting dashing. Subsequent runs of the job are fine. It's also fine if I remove :first_in.

@grahamb grahamb closed this as completed Sep 30, 2013
@gabrielpoca
Copy link

Hi, I have the same problem. Looking into the code is obvious it wont work, dashing.rb is loading every file in jobs folder before you have settings.

You can rename the jobs folder to something else like my_jobs and then require every job in the end of the config.ru when sinatra is already running, as example:

 require File.expand_path("../my_jobs/job.rb", __FILE__) 

Hope it helps some looking for it.

@sjernigan
Copy link

Seems to work for me..you could include something along the lines of

job_dir = File.expand_path("./my_jobs/")
Dir.new(job_dir).each do |file|
  begin
    require File.join(job_dir,file) if file.match(/.*\.rb/)
  rescue Exception =>e
    puts "Failed to load job #{file} due to exception #{e}: #{e.backtrace}"
  end
end

@phoet
Copy link

phoet commented Sep 3, 2014

@pushmatrix thx, was just looking for this 😄

@cob16
Copy link

cob16 commented Oct 20, 2015

Instead of renaming you can use @sjernigans code at the end of configre do and before add

ENV["JOB_PATH"] = 'dashing can you wait a sec before you load the jobs?'

at the top of your file

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

No branches or pull requests

6 participants