Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- deployer@bigswitch-dvmh-util-01:~/rails-apps/onebigswitch/current/lib/tasks/cron $ cat *
- namespace :cron do
- desc "Generate daily electricity switches CSV"
- task :send_csv => :environment do
- puts "Running obs:generate_csv at #{Time.now}"
- file_path = Rails.root + "shared/cron_files/"
- file_name = "cron_csv_#{Time.now.strftime("%Y%m%d_%H%M%S")}.csv"
- file_full_path = file_path + file_name
- FileUtils.makedirs(file_path)
- cron_job = CronJob.find_by_code!("SEND_CSV")
- CSV.open(file_full_path, "w", :headers => true) do |csv|
- begin
- # find the send csv job
- records = ActiveRecord::Base.connection.select_all(cron_job.sql)
- records.each_with_index do |r, index|
- # CSV Header
- csv << r.keys if index == 0
- csv << r.values
- # CSV Footer
- csv << ["Total Records:", "#{index + 1}"] if index == records.length - 1
- end
- rescue => ex
- Airbrake.notify(ex)
- raise ex
- end
- end # End CSV
- if File.exists?(file_full_path)
- puts "CSV #{file_full_path} generate successfully!"
- # Start sending email
- file_name_without_ext = file_name.gsub(/\.csv/, '')
- ObsTransmission.create_encrypted_zipfile("tmp/#{file_name_without_ext}.zip", "obs", file_full_path.to_s)
- if cron_job.email_from && cron_job.email_to
- ObsTransmission.send_mail_to(cron_job.email_to, cron_job.email_from, "OBS Electricity Switch sales on #{Date.today}", "OBS Electricity Switch sales on #{Date.today}", {"#{file_name_without_ext}.zip" => File.read(Rails.root + "tmp/#{file_name_without_ext}.zip")})
- puts "Email sent"
- else
- puts "Error: Email from and to not being sepcified in CronJob"
- end
- else
- puts "CSV generate fail!"
- end
- end
- end
- require 'whenever'
- namespace:cron do
- CRONTAB_IDENTIFIER = "onebigswitch"
- desc "Update Crontab from CronJobs table"
- task :update_crontab => :environment do
- puts "Updating crontab at #{Time.now}"
- rvm_path = `echo $rvm_path`.strip
- file = <<-FILE
- set :output, "#{Rails.root}/log/cron.log"
- rvm_path = `echo $rvm_path`.strip
- unless rvm_path.blank?
- job_type :rake, "source #{rvm_path}/scripts/rvm && cd :path && RAILS_ENV=#{Rails.env} bundle exec rake :task --silent :output"
- end
- every 5.minutes do
- rake "cron:update_crontab"
- end
- FILE
- CronJob.active.find_each do |cron_job|
- # Needs the ability to handle jobs that are recurring and not running at a certain time, i.e. every 5 minutes.
- # There will be no 'at' present.
- at_str = ", :at => \"#{cron_job.at}\"" if cron_job.at.present?
- file += <<-FILE
- every #{cron_job.every}#{at_str} do
- rake "#{cron_job.rake_name}"
- end
- FILE
- end
- tmp_file = Tempfile.new('tmp_file')
- begin
- tmp_file.write(file)
- tmp_file.rewind
- Whenever::CommandLine.execute(:file => tmp_file.path, :update => true, :identifier => CRONTAB_IDENTIFIER)
- ensure
- tmp_file.close
- tmp_file.unlink
- end
- end
- enddeployer@bigswitch-dvmh-util-01:~/rails-apps/onebigswitch/current/lib/tasks/cron $
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement