ruby on rails - ultrasphinx - ruby-on-rails

Ruby on Rails - UltraSphinx
Hi guys, I'm using Ultrasphinx for the search thing.
My question is : I have the "rake ultrasphinx:daemon:start" running in the background. Now, should I have a cron job that does "rake ultrasphinx:index" regularly or will the daemon take care of indexing whenever a new object is created.
Please, let me know. Its kind of emergency.
Thanks

You will have to run a cron job to regularly update your index. However, you can run the rake task ultrasphinx:index:delta to only update the delta index which contains recently changed records.
From time to time, you might also want to merge your delta index into your main index using the task ultrasphinx:index:merge
Hope this helps.

add this to your model if you want to use delta indexing:
is_indexed :fields => ['column1', 'column2'], :delta => true
Run ultrasphinx daemon:
rake ultrasphinx:daemon:start
Add to your CRON(I run it every 10 mins, but final decision is all up to your app):
rake ultrasphinx:index:delta
You will need to run rake ultrasphinx:index:main once a day to move the delta contents into the main index.
Source: official documentation.

Related

Rails - How to auto-transfer records from table to table at a specific time?

I am pre-storing records in a table A and I want to transfer these records from table A to table B automatically at a specific time, lets say on every evening at 08:00 PM.
Any ideas on how to solve this little problem?
You could create rake task to implement your job, and then schedule it with cron, default *nix time manager. Its syntax is difficult to remember, so I prefer to use Ruby wrapper around it, gem whenever.
You can use whenever gem to run cron jobs ...for example job that runs every 5 mins
in schedule.rb
every 5.minutes do
rake "transfer_data:send_data"
end
lib/tasks/send_data.rake
#!/usr/bin/env ruby
namespace :transfer_data do
desc "Rake task to transfer data
task :send_data => :environment do
## code to transfer data from one table to other table
end
end
Execute the task using bundle exec rake transfer_data:send_data

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

Scheduling in ruby on rails involving database access

I want to schedule daily reports to subscribed users via email.
For that I have written action in reports_controller that fetch data from database & convert it into pdf using pdfkit/wkhtmltopdf.The action works fine when called from get request.But when converted so that be defined like
def self.dailymail
ac = ActionController::Base.new()
kit = PDFKit.new #retrieve data from db
pdf = kit.to_pdf
ReportMailer.send_reports(ac.send_data(pdf)).deliver
end
It raises exception at send_data call when used with rufus scheduler:
RackDelegation#content_type= delegated to #_response.content_type=, but #_response is nil: #<ActionController::Base:0x206b068 #_routes=nil, #_action_has_layout=true, #_headers={"Content-Type"=>"text/html"}, ...
so, my question is what how can I solve this problem or Is there any alternate scheduler in rails that work fair on both Windows and Linux?
I wish to know any scheduler that can be helpful to send reports fetched from database.
I agree with claasz regarding the rake task. Check out the whenever gem https://github.com/javan/whenever
There is no suport for windows Task Scheduler, but it does support creating cron jobs.
Check out the documentation for the details, but esentially the gem creates cron jobs based on what you configure in the schedule.rb file that is created when you install the gem.
sample content of schedule.rb:
every 3.hours do
runner "MyModel.some_process"
rake "my:rake:task"
command "/usr/bin/my_great_command"
end
This would be like running bundle exec rake my:rake:task every 3 hours
After creating the schedule.rb you will need to run the whenever command from the console in order to add your schedule to cron. If you run whenever without arguments, the output shows you the contents of the schedule.rb. There is an argument you need to provide that I can't remember off the top of my head, just pass --help and I think you'll get the answer.
Hope this helps
EDIT:The argument is -w to write to cron-tab
As willglynn already points out, you should get rid of any controller interaction. There's simply no need here and it makes things unnecessarily complicated. So your code should look more like
def self.dailymail
kit = PDFKit.new #retrieve data from db
pdf = kit.to_pdf
ReportMailer.send_reports(pdf).deliver
end
If you got problems with the rufus scheduler (which I don't know), you could create a rake task to send out your mails and use the OS scheduler (e.g. cron on Linux) to call the task. Having the rake task would be also convenient for testing.

how to update db on midnight everyday on rails3

i have a job to update some information in db every midnight ,so i want to find a way can use rails model to update information to db,
what's the common soluation?
Use a cron job which then runs a rake task. Define the task like this in _lib/tasks/your_thing.rake_:
task :your_thing => :environment do
# do stuff
end
Delayed Job https://github.com/tobi/delayed_job
Resque https://github.com/blog/542-introducing-resque

ruby-on-rails: revision counter

I want to add a revision counter to my rails app.
Not the number of commits necessary but the number of live pushes/deployments for example.
I'm using github as my remote repo.
Any suggestions?
Thanks
There's not one magic solution.
But basically, you should execute some code every time you deploy your application that increments the number of deployments by one.
One solution would be to create a capistrano task which would increment this.
namespace :deploy do
desc "Increments the number of deployments"
task :increment do
Config.find_by_key('deployments').update('value = value + 1'
end
end
It will take the uplet "deployments" in a config database (which you have to implement, this way or an other).
And in your capistrano recipes, you add the following :
after "deploy", "deploy:increment"
Every time you deploy your application, the deployment value in the config model will be updated by one.
This is only one example of a possible implementation. You might want to store the number of deployments somewhere else.
The main idea is to have the code executed every time you deploy.

Resources