Ruby, random failures with File.new - ruby

I have a script that works great, except that it randomly fails generating new files...
this is the code:
...
file_log_path = File.join(Rails.root, 'log', "xls_import_#{Time.now.to_i}.log")
#log = File.new(file_log_path, 'w+')
....
and this is the error inside delayed_job.log
2012-12-21T18:18:41+0100: [Worker(delayed_job host:webserver2.netbanana.it pid:24482)] LoadDataFromCsv failed with Errno::ENOENT: No such file or directory - /var/www/rails/myapp/releases/20121210093945/log/xls_import_1356110321.log - 0 failed attempts
2012-12-21T18:18:41+0100: [Worker(delayed_job host:webserver2.netbanana.it pid:24482)] PERMANENTLY removing LoadDataFromCsv because of 1 consecutive failures.
Other times, it works! Someone can help me?
-- edit:
Well... it seems that Rails.root uses a wrong deploy path... in fact /var/www/rails/myapp/releases/20121210093945 doesn't exists.
But, as I said, the script sometimes works, sometimes not... If I reload delayed_job, my script works a few, and then start failing.

If you're using Capistrano to manage your releases, which I'm guessing is the case based on the path structure, then you'll need to be careful about referencing paths which can be removed after a deployment has occurred. DelayedJob needs to be restarted each time you deploy or it might be working in an orphaned directory.
If possible, you might want to use the shared/log path instead since that persists between deployments.

I found several delayed_job processes (zombies), still running... killed them all (not metallica's song) and now it works!

Related

Testing a Jekyll site with rspec and capybara, getting a bizarre race-case on rspec start

So check this out: it appears as though, upon running bundle exec rspec, there's a race between jekyll serve and puma/rspec's boot up. Sometimes I run the command, and my tests run fine. Other times, I get the error for each of my spec files: cannot load such file -- /path/to/project/sitename_jekyll/_layouts/spec/form_spec.rb which is interesting cause that's not where my spec files are located. They're in /path/to/project/sitename_jekyll/spec/form_spec.rb.
What's crazy is that I can literally just re-run the command over and over and over again and sometimes it'll go through and run the spec tests in the correct location, and sometimes it'll look for them in _layouts and error out. It probably runs correctly maybe once out of ever three or five attempts. All the other times I get the following errors:
Here's what my spec_helper.rb looks like: https://gist.github.com/johnhutch/2cddfafcde0485ff021501d5696c0c2d
And here's an example test file:
https://gist.github.com/johnhutch/a35d15c170f5fd9ca07998bf035d111d
My .rspec only contains two lines:
--color
--require spec_helper
And here's the output, both successful and unsuccesful, back to back:
https://gist.github.com/johnhutch/7927d609170ef5c70a595735502b128d
HEEELLLLLP!
This sounds like jekyll is changing the current directory while building the site, which since it is being run in a thread also affects the tests RSpec is trying to run (See https://bugs.ruby-lang.org/issues/9785 for why Dir.chdir is not threadsafe) - leading to attempts to load things from incorrect locations.
A potential solution to this would be to wait for the Jekyll site to be built before actually running your tests. A comment in your spec_helper seems to state that someone thought passing force_build: true would do this but from a quick perusal of the jekyll-rack code I don't think that's true and you actually need to wait for compiling? to return false (v 0.5) (complete? to return true in the current master branch) to ensure building has finished (as well as passing force_build). This could either be done in a loop sleeping and checking (simpler)
sleep 0.1 while <jekyll app>.compiling?
or (if using the master branch) via the mutex/conditional Rack::Jekyll exposes like in its test suite - https://github.com/adaoraul/rack-jekyll/blob/master/test/helper.rb#L49
Note: Also check my comment about your tests that aren't actually testing anything.
As per Thomas Walpole's super helpful responses this ended up working:
sleep 0.1 while Capybara.app.compiling?
inserted right after:
51 Capybara.app = Rack::Jekyll.new(force_build: true)
in my spec_helper.rb
Thanks again, Thomas!

File fixture getting deleted after running RSpec tests

I have tests written in RSpec that have been passing up until an upgrade to Ruby 2.5.1.
The test in question has several let statements in a before(:each) block. The file that keeps getting deleted is in there like so:
let(:fake_s3_file) { File.open(File.join(File.dirname(__FILE__), '../fixtures/Alexa.pdf')) }
Later in before(:each) stanza, I have this:
allow(s3_client).to receive(:get_object).and_return(fake_s3_file)
allow(Tempfile).to receive(:new).and_return(fake_s3_file)
It appears that in Ruby 2.5.1, my source files are getting deleted, either because of the File.open or the Tempfile reference. Has anyone run into this, and what is a good way around this? I haven't found any docs that say any changes have happened to Tempfile or to File.open. Should I copy my fixture file into a /tmp directory before every spec run? It seems wrong to do that, and slow.

Ruby Dir.mkdir No such file or directory # dir_s_mkdir

I have a script that calls Dir.mkdir nesting a folder in /process_data
path = "d:/process_data/#{processName}"
Dir.mkdir path
Unfortunately it fails 70% of the time.
The base directory exists and is successfully being populated by other processes.
Any thoughts on why this would be happening?
Update: Having googled a bit more someone suggested the solution of using FileUtils.mkdir_p. I don't think it's the right approach to resolving this but it'll do for now, it's worked so far.
Would still appreciate and ideas on this.
Thanks.

RSpec Autotest loops with failures, doesn't work when exceptions are added

I've been playing around with autotest trying to make it work all day.. but am having some problems...
I've been following https://github.com/rspec/rspec/wiki/autotest, I'm running with:
Ruby 1.9.3-p194
rspec 2.10.0
ZenTest 4.8.1
I also created a .rspec file.
So with this setup, I run autotest, and it works - my test runs, it passes, hooray!. When I stick a failure into my test e.g. false.should == true, then the test starts looping, over and over again.
what happens is that it's an integration test, and I'm writing to an sqlite db. If I run find . -mmin -1 then I'm able to see that my db folder has changed - so I figured this is the problem.
So I edit .autotest and add the following:
Autotest.add_hook :initialize do |autotest|
%w{db}.each { |exception| autotest.add_exception(exception) }
false
end
But now when I run autotest, it just says the following:
loading autotest/rspec2
and that's it, it won't do anything anymore. Previously the output was:
loading autotest/rspec2
/home/me/.rbenv/versions/1.9.3-p194/bin/ruby -rrubygems -S '/home/me/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.10.1/exe/rspec' ``--tty '/home/me/Workspace/myproject/spec/integration/db/lead_spec.rb'
and then it'd run my test and show the result...
Anyone know what could be going on? it's very frustrating, and I feel like I've come to a road block....
Thanks for your help!
Autotest checks if defined exceptions match any part of the filename. Your spec has db in it's path so it is ignored by autotest.
If you want to ignore db folder, then do the following:
Autotest.add_hook :initialize do |a|
a.add_exception %r{^\./db}
end

Path Error running CakePHP's "cake" console utility in Windows

A while ago when I started with Cake, I managed to get the console running in my Windows environment, and I have no idea how. I'm now having problems to make it work again in another computer.
This is what I've done in the new machine:
Downloaded my Cake code from source control (so all the files are exactly the same as the computer where it worked, including the configuration files)
Added PHP and "c:\my_cake_path\cake\console" to the path
If I run "cake OneOfMyShells", either standing on the /app, or in /cake/console directories, I get the following error:
Warning: get_object_vars() expects parameter 1 to be object, null given in C:\my_cake_path\cake\libs\model\connection_manager.php on line 199
Error: Missing Database Connection. Try 'cake bake'
"cake Bake", if run normally, when I try to get it to do the DB config ends up throwing another error (which is not that relevant to this)
However, if I run: cake bake -app "c:\my_cake_path\app"
Then bake works, I can do the database config, and it writes the DB config file (which is useless at this point, since I already had one)
Then, of course: cake OneOfMyShells -app "c:\my_cake_path\app"
does work perfectly well.
So, everything's working fine, I just need to manually specify the path to "app" every single time, which is very annoying.
How can I get around this? Where is Cake looking for to find the path to app?
Thanks!
Daniel
Not much of a difference in the sense you still need to type but you can run cake from the app dir like this: C:\XXX\project\app> ..\cake\console\cake.bat
To make it smaller you can put that line on a .bat or just add the \cake\console dir the windows path

Resources