TypeORM migration timer - typeorm

Is there a way to get a console log of how long it took to run the migrations?
Something like this:
console.time(migration)
runMigration()
console.timeEnd(migration)
or Ansible has a command time you add before to time stuff

Related

Rails whenever not able to execute service methods

I have a NotificationService which has methods like this:
def trial teacher
notification = Notification.create(recipient: teacher, tag: 'trial', text: 'text', title: 'title')
notification.deliver
end
I want to have a Whenever runner job to execute this method ... bin/rails runner "NotificationService.new.trial" --silent, but if I try to do so I get an error Please specify a valid ruby command or the path of a script to run.. Yet if I try to use a model, not a service, it works (for instance, Notification.last.deliver)
And also that executes perfectly fine on development, but doesn't work on production. Seems like that service class doesn't exist for Cron
What am I missing?
Solved it! Turned out that the trouble was not with Whenever or Cron, but the exact method I was trying to run.
And as it was production environment with constantly changing data, when I ran NotificationService.new.trial I just got lucky not to get an error. Then data changed and Cron's attempts to run were failing.
Using safe navigation operator (&.) solved everything!

Monit's second "Does not exist" overrides first one

I have a process which I am monitoring using Monit. If process dies for some reason, I want to send a Slack notification using a shell script and also restart it. This behaviour though does not work with "does not exist" directive. The last one is executed and previous one ignored. For example code below:
check process xyz with pidfile /var/run/xyz.pid
start program = "/etc/init.d/xyz start" with timeout 60 seconds
stop program = "/etc/init.d/xyz stop"
if does not exist then restart
if does not exist then exec "/opt/somescript.sh"
It executes script but does not restart. it also looks like from documentation that this is how it will behave. Any other way to get this working. Documentation reference (Not exactly clear but resembles the actual behaviour):
If not defined, it defaults to a restart action.
You can override the default action with the following statement:
I believe monit doesn't allow you to have the same statements twice. You would have to write your script on restarting the process in your somescript.sh.
My guess is the default action is already to restart the process, as per the documentation, and you are overriding that with an exec action
Cleaner way is to add the restart script inside your somescript.sh.
If you don't want to do that, you can also combine the two actions in one, like this:
if does not exist then exec "/etc/init.d/xyz restart && /opt/somescript.sh"

Is there a way to print migration file when I run a generator

Rails is convenient in that it lets you run generators to create migration files, models, views, controllers, etc.
However everytime I generate a migration using--for example, rails generate migration add_title_to_posts title:string, I open up the resulting migration file to see that the result is same as I expected. It's kind of annoying because i have to do this every time.
I was wondering if there's an easy way to print the resulting migration file when I run the generator.
Short answer, no.
the generate command doesn't have an option like what you're looking for, but you can always do a cat command(on linux), OSX should have cat too or something similar, at least then you can see the file after execute the command on the terminal or if you really, really, really want to have that, do a sh bash that execute booth commands with the parameters that you execute... i just know i wouldn't.
cat db/migrate/20140515041846_add_default_to_sliders.rb
PS:
rails g
execute rails g for see all the options for generate

Rails 3.2 observe a date attribute of a model

I have a model which saves several dates. Now I am looking for an permament observer which observes the dates and if one of this dates expires (in comparison to today's date), then I would like to perform some action (e.g. save this date to another model named ExpiredDate).
I saw the Rails Observer can only observe a Model after something new was created, deleted or updated. Is there any way how I can observe model attributes permamently?
The reason, observers only work on create, update and delete is, that you need some trigger to start any action in Rails. Normally, that's a http request by a user.
To trigger actions on a time base, you could write a rake task or use rails/runner to execute some model method.
You then run the task or script with cron.
You can use a gem like whenever to handle the cron jobs. It also helps you, to set up the environtment to run ruby on rails.
Instead of
10 0 * * * /bin/bash -l -c 'cd /home/user/rail-app/releases/20130522173433 && RAILS_ENV=production bundle exec rake my_rake_task --silent'
it simplifies the configuration to
every :day, at: '0:10am' do
rake 'my_rake_task'
end

Call a ruby script from within a rails app controller

I'm very new to rails and I have a script that I run from the console like this
$ ruby axml2xml.rb ExamPaper.apk
Now, how do I call this script from within my controller method and pass the same parameter as ExamPaper.apk?
I tried require 'axml2xml.rb' but got some error pointing to this line of code Zip::ZipFile.foreach(ARGV[0]) do |f|. So basically, how do I make something like axml2xml.rb 'ExamPaper.apk' in my controller?
You have at least 3 options:
exec(command)
%x{ command }
system(command)
They have different behaviors, so make sure to read this quicktip and/or the answer of this question to learn more about these commands.
In your case, the backticks or %x command is probably the best option.
value = `ruby axml2xml.rb ExamPaper.apk`
You can try using system or popen, but only for short tasks, for more information about that, please see here.
If your task is more time consuming you definitely should have a look at something like delayed_job and use a background job or some sort of queue to run your job. This way your server doesn't get blocked and your users do not have to wait til your job completes.
If you want to execute it as a shell command, use:
exec 'ruby axml2xml.rb ExamPaper.apk'
In ruby there are several ways to run shell commands.
system("ls")
%x("ls")
`ls` #nice they are back ticks
exec "ls"
But I'm not sure about the permissions necessary for running commands like that via rails.

Resources