Make a rake task fail if system call return error - ruby

I have a Rakefile that I use to automate some tasks in my project.
Inside some tasks, I call system, but, even if the process return an error,
task continues without any issue.
How can I avoid that? I want to make rake exit when some subprocess return an error.
Thanks in advance

You can evaluate the return value of system
system('inexistent command') or exit!(1)
puts "This line is not reached"

sh is the Rake way to call a command. It will fail with a neat message. Compared with system, sh prints out the command as well.

Related

why rake tasks are not executing using and operator?

I have a rake task :
task :kill_process do
system %q(ps -ef | awk '{if($8~"java" || $8~"glassfish" || $8~"ruby" || $8~"god" || $8~"couch"){printf("Killing : %s \n",$2);{system("kill -9 "$2)};}}')
end
This is basically killing processes. And this task is a part of another rake task :
desc "stop the entire system"
task :stop => [...., :kill_process]
There's another task:
desc "start the entire system"
task :start => [....]
When I am doing rake stop && rake start
stop task is executed successfully. but rake start is not executing.
If i execute both tasks separately, then it works fine. but not in rake stop && rake start
What will be better to use here exec function or system or any other, please suggest me.
My only requirement is to kill these mentioned processes at the end of rake stop. But also it should not effect other things like rake stop && rake start should work fine.
As mentioned in the comments, the exit code is 137 which evaluates to false and therefore the other part of the && does not get executed. The reason for this is probably kill -9.
There are a few options now.
Return 0 from your rake task, something like exit(0)
Don't use kill -9
Create restart command which does execute stop and start but without logically depending on each other (&&).
Exit code 137 indicates that a process has received a SIGKILL signal and was thus killed from the outside.
This happens since a Rake task is also executed by Ruby. As such, your stop task is sending a SIGKILL to its own process too (along with all other Ruby processes on the system). Now, since you have specified that you only want to execute the rake start process if the previous process was successful (i.e. had a exit code of 0), your shell doesn't start the rake task.
To quickly fix this, you can instead run rake stop; rake start, i.e run the two processes regardless of their individual exit codes (by default).
However, a better idea would probably to make your stop task more explicit and only kill the specific processes you need rather than everything in sight which looks slightly like a related process. This will likely result in a more stable system overall too when you don't kill potentially unrelated processes all the time.

How to call ODI Scenario from command line and wait for its execution

I'd like to call an ODI Scenario from command line and wait until its done. I am using ODI 12c and installed a standalone agent. I already found out that you can use the startscen.cmd command and it works for me. The only problem is that cmd is not waiting for the scenario to be done. Any Suggestions to achieve sth like that?
My .bat-file looks like this:
cd C:\Oracle\Middleware\Oracle_Home\user_projects\domains\base_domain\bin
call startScen.cmd "-INSTANCE=CITestAgent" MAPPING 1_0_0 GLOBAL "-SESSION_NAME=TEST_RUN" "-AGENT-URL=http://localhost:20910/oraclediagent"
cd C:\Users\Redekera\documents\testFiles
"C:\Users\REDEKERA\Documents\instantclient_19_3\sqlplus.exe" db_user/pw#db/scheme #run_tests_lieferschein.sql
After that command i'd like to run an sql via sql*plus, which needs to wait until the scenario has finished.
Thanks for help guys :)
By default startscen.cmd will wait for the end of the execution to return.
This can be changed with parameter -ASYNC=yes to start the execution asynchronously. In that case it would return the SESSION number that is useful to check the status of execution.
If you want the second command to execute only if the first exited successfully:
execute scenario command && sql*plus command
Extracted from here
The main idea is the “&&” sign!

Jenkins always SUCCESS state when the Shell script actually failed

I'm facing this challenge in my current Jenkins setup. Where the set of cases like Shell(bash) script executed remotely:
Permission denied while my installer copied
Unable to connect with SSH
Any suggestion on these cases how can I fix it? any pointers?
Thanks in advance
A pipeline will fail if a script / software returns a value not equal zero. There are programs like Robocopy that execute a command, fail and return a 0. Jenkins does not understand that the program was not successful and marks the pipeline as a success.
Basically this is what you have to do. If your script returns a value not equal zero the pipeline will fail.

Use whenever with sinatra

I'm trying to get whenever to work with sinatra. When I run the whenever command, I get the generated cron tab. But the problem is, that in my sinatra app, I don't have a script/runner file, which is present in Rails.
How do I get this runner, or is there a whenever command to generate one?
thx!
You can use a rake task in place of script/runner. The Whenever gem supports defining the job via a rake task (and more in fact)
Sample:
# config/schedule.rb
every 3.hours do
rake "destroy_all"
end
and in your Rakefile: (for lack of good examples)
task :destroy_all do
puts "Do not do this"
# sh "rm -rf ."
end

Windows 2008 Task Scheduler Return Code and Matlab Script

How do I allow my Matlab script to pass back a return code to the Task Scheduler? I currently have a task that runs "matlab -r myscript". The problem is the Task Scheduler always succeeds immediately after starting, even though myscript takes several minutes to run. So, I don't see how to pass back an error code.
How can I make Task Scheduler wait until the script stops running and then get matlab to pass back a return code?
Use the matlab -wait command line option to have it block until the program is finished.
There appears to be an undocumented argument to quit() to set the exit status - e.g. quit(42) - which then shows up in %ERRORLEVEL%. Since it's undocumented, you might not want to rely on it. Alternatively, have your script write its status to a file and have a wrapper script parse it.

Resources