daily pastebin goal
80%
SHARE
TWEET

Untitled

a guest Mar 13th, 2019 151 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. deployer@bigswitch-dvmh-util-01:~/rails-apps/onebigswitch/current/lib/tasks/cron $ cat *
  2. namespace :cron do
  3.   desc "Generate daily electricity switches CSV"
  4.   task :send_csv => :environment do
  5.     puts "Running obs:generate_csv at #{Time.now}"
  6.     file_path = Rails.root + "shared/cron_files/"
  7.     file_name = "cron_csv_#{Time.now.strftime("%Y%m%d_%H%M%S")}.csv"
  8.     file_full_path = file_path + file_name
  9.     FileUtils.makedirs(file_path)
  10.     cron_job = CronJob.find_by_code!("SEND_CSV")
  11.     CSV.open(file_full_path, "w", :headers => true) do |csv|
  12.       begin
  13.         # find the send csv job
  14.         records = ActiveRecord::Base.connection.select_all(cron_job.sql)
  15.         records.each_with_index do |r, index|
  16.           # CSV Header
  17.           csv << r.keys if index == 0
  18.  
  19.           csv << r.values
  20.  
  21.           # CSV Footer
  22.           csv << ["Total Records:", "#{index + 1}"] if index == records.length - 1
  23.         end
  24.       rescue => ex
  25.         Airbrake.notify(ex)
  26.         raise ex
  27.       end
  28.     end # End CSV
  29.  
  30.     if File.exists?(file_full_path)
  31.       puts "CSV #{file_full_path} generate successfully!"
  32.  
  33.       # Start sending email
  34.       file_name_without_ext = file_name.gsub(/\.csv/, '')
  35.       ObsTransmission.create_encrypted_zipfile("tmp/#{file_name_without_ext}.zip", "obs", file_full_path.to_s)
  36.       if cron_job.email_from && cron_job.email_to
  37.         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")})
  38.         puts "Email sent"
  39.       else
  40.         puts "Error: Email from and to not being sepcified in CronJob"
  41.       end
  42.     else
  43.       puts "CSV generate fail!"
  44.     end
  45.   end
  46.  
  47. end
  48. require 'whenever'
  49. namespace:cron do
  50.   CRONTAB_IDENTIFIER = "onebigswitch"
  51.  
  52.   desc "Update Crontab from CronJobs table"
  53.   task :update_crontab => :environment do
  54.  
  55.     puts "Updating crontab at #{Time.now}"
  56.  
  57.     rvm_path = `echo $rvm_path`.strip
  58.     file = <<-FILE
  59.       set :output, "#{Rails.root}/log/cron.log"
  60.  
  61.       rvm_path = `echo $rvm_path`.strip
  62.  
  63.       unless rvm_path.blank?
  64.         job_type :rake, "source #{rvm_path}/scripts/rvm && cd :path && RAILS_ENV=#{Rails.env} bundle exec rake :task --silent :output"
  65.       end
  66.  
  67.       every 5.minutes do
  68.         rake "cron:update_crontab"
  69.       end
  70.  
  71.     FILE
  72.  
  73.     CronJob.active.find_each do |cron_job|
  74.       # Needs the ability to handle jobs that are recurring and not running at a certain time, i.e. every 5 minutes.
  75.       # There will be no 'at' present.
  76.       at_str = ", :at => \"#{cron_job.at}\"" if cron_job.at.present?
  77.  
  78.       file += <<-FILE
  79.       every #{cron_job.every}#{at_str} do
  80.         rake "#{cron_job.rake_name}"
  81.       end
  82.       FILE
  83.     end
  84.  
  85.     tmp_file = Tempfile.new('tmp_file')
  86.  
  87.     begin
  88.       tmp_file.write(file)
  89.       tmp_file.rewind
  90.  
  91.       Whenever::CommandLine.execute(:file => tmp_file.path, :update => true, :identifier => CRONTAB_IDENTIFIER)
  92.     ensure
  93.       tmp_file.close
  94.       tmp_file.unlink
  95.     end
  96.    
  97.   end
  98. enddeployer@bigswitch-dvmh-util-01:~/rails-apps/onebigswitch/current/lib/tasks/cron $
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top