rake task that writes to file on local filesystem and not heroku - ruby-on-rails

I would like a rake task that gets some information and then writes to the local filesytem
I thought I had this working but not sure how.
namespace :arc do
task :vday => :environment do
locations=Location.with_current_mec "something"
jt={}
jt[:locations]={}
locations.each do |location|
jt[:locations][location.id]=location.to_small_hash
end
# want this to write to local filesytem
File.write('/Users/jt/output.json', JSON.pretty_generate(jt))
end
end
and then call like
heroku run rake arc:vday
But this is not working and gives me an error on writing the file. Is there a workaround to make it write to my local filesystem and not heroku?

Can you try this
File.open('/Users/jt/output.json', 'w') {|file| file.write(JSON.pretty_generate(jt))}
instead of
File.write('/Users/jt/output.json', JSON.pretty_generate(jt))

Related

Heroku rake task with poltergeist

I'm writing a rake task which tests an application. To test it I need Capybara and Poltergeist.
Rake task looks like this:
require "capybara"
require "capybara/poltergeist"
require "capybara/dsl"
namespace :almet do
desc "Checking if something changed on tatar.ru"
task check_news: :environment do
Capybara.current_driver = :poltergeist
Browser = Class.new { include Capybara::DSL }
page = Browser.new.page
page.visit("http://almetyevsk.tatar.ru")
unless page.has_selector?(".home-events ul li") &
page.has_selector?(".home-events__item-lead") &
page.has_selector?(".home-events__item-date") &
page.has_selector?("h2 a")
then
alert
end
news_link = page.find(:css, "h2 a", match: :first)[:href]
page.visit(news_link)
unless page.has_selector?(".page__content-i h2") &
page.has_selector?(".page__content-i .news") &
page.has_selector?("i")
then
alert
end
end
def alert
AlertMailer.alert.deliver_now
end
end
This code checks if the main site from parsing data is still the same (no changes in css selectors). This rake task works good locally, but when I pushed it to heroku I've got an error:
rake aborted!
Cliver::Dependency::NotFound: Could not find an executable ["phantomjs"] on your path.
I googled this, installed phantomJS but still got the same error.(Poltergeist using phantomJS). Is it possible to solve my problem?
You need to install PhantomJS onto your Heroku instance. Usually that involves adding a build pack which will install it for you, like https://github.com/stomita/heroku-buildpack-phantomjs

Add TODO list (rake notes) output to source control

I want my TODO list which I get as output of 'rake notes' to be under source control (Git).
Is there a way I can configure 'rake notes' to spit out out a text file which can be under source control?
Not completely understanding your question's direction, so I'll just go from the beginning. :)
Create a rake file :)
The most common is under Rails.root / lib, but it's preferable (depending on Elitests or purists) to put it under lib/tasks/output_notes.rake (yes, .rake, not .rb)
Rake scripts have namespaces. "db" is a namespace for running "rake db:migrate" for example. You can see some examples here: https://github.com/rails/rails/tree/master/railties/lib/rails/tasks
Your script might look something like this.
namespace :vasa do
desc "Output notes"
task :output_notes do
output_file_name = "#{DateTime.now.to_i}_notes.txt"
File.open(output_file_name, 'w') { |file| file.write(Note.dump_all) }
end
end
You can then run rake vasa:output_notes. Want to add the file to your repo automagically?
# after File.open ...
`
git add #{output_file_name}
git commit -m "added notes"
`
The backticks will run it as a system command.

rake aborted! Don't know how to build task 'sandbox'

I'm trying to add the sandbox to my rails spree application and have run into this error
(using windows 8/powershell with Rails 4.1.6). I'm going by this manual: https://github.com/spree/spree/issues/411
This link Use older version of Rake
seems to have a similar issue but I am not sure how to take the necessary steps to achieve it.
When I try:
C:\Ruby193\openUpShop> bundle exec rake sandbox
I get:
rake aborted!
Don't know how to build task 'sandbox'
I'm am new to rails and am still not sure how everything works so a throughout explanation
with step by step instructions would be greatly appreciated! Thanks.
you can use a file sandbox.rb
# use example: rake task:sub_task -- --sandbox
if ARGV.any? {|arg| arg == '--sandbox' }
puts "** << USING SANDBOX!! >> **"
# beginning
$sandbox = -> do
ActiveRecord::Base.connection.begin_transaction
end
# end
at_exit do
ActiveRecord::Base.connection.rollback_transaction
end
end
then only you need add at the beginning of your task.rake file
require_relative 'path_to_your/sandbox.rb'
..and add at the beggining of your task code
desc "description task"
task example_task: :environment do
$sandbox.call if $sandbox.present?
...

Rake task working on local machine, not on heroku

Here is my simple rake task :
desc 'Create objects from csv'
task :add_missing_information_from_url, [:url] => [:environment] do |t, args|
url = args[:url]
puts "STARTED"
CSV.new(open(url), :headers => :first_row).to_enum.with_index(1).each do |line, i|
user = User.find(line['id'])
next if user.nil?
user.flag = true
user.save
puts "#{i} - #{user.firstname}"
end
end
For starters doesn't do anything smart. But for some reason this doesn't work on Heroku. This is how I execute the task (my namespace is dbs):
heroku run rake dbs:add_missing_information_from_url['http://lvh.me/file.csv']
Or on dev machine :
bundle exec rake dbs:add_missing_information_from_url['http://lvh.me/file.csv']
This is the only output I get from the console when running heroku task :
Running `rake dbs:add_missing_information_from_url[http://lvh.me/file.csv]` attached to terminal... up, run.7246
And returns to my shell after it has executed with nothing printed, nor the user objects get saved.
However when I run it on my dev machine, all works fine, starting by printing STARTED onwards.
What am I doing wrong here?
Is it me or does your log actually outputs this :
Running `rake dbs:add_missing_information_from_url[http://lvh.me/file.csv]` attached to terminal... up, run.7246
If you take a closer look at the argument in the [http://lvh.me/file.csv], it appears not to be a string. You would want your log to output ['http://lvh.me/file.csv'].
So I suggest you try running your task like this :
heroku run rake dbs:add_missing_information_from_url['"http://lvh.me/file.csv"']

Custom Rails rake task to work with database

I need to make a custom Rails 4 rake task to delete all records in database using ip:
task :delete_records, [:ip] => :environment do |t, args|
User.destroy_all(ip: args.ip)
end
I try to execute it using the following command:
bundle exec rake delete_records["127.0.0.1"] but I've the error:
no matches found: delete_records["127.0.0.1"]
How can I fix it? Thank in advance!
I'm guessing you used this answer for help - although we've done rake tasks before, we've never used one with arguments like this
--
Test
We've created custom rake tasks before - they reside in lib/tasks
I would test out yours by doing the following:
#lib/tasks/your_task.rake
desc "Remove all records for particular IP"
task :delete_records, [:id] => :environment do
args.with_defaults(:ip => "127.0.0.1")
User.destroy_all ip: args.ip
end
If this does not work, it will mean there's something wrong with the definition of your task (it's not in the correct folder or something)
The above should determine if your argument definition is the issue; if it isn't you should let me know so we can work to fix it

Resources