<commit_before>namespace :cosuka_opsworks do
  desc 'Watch disk space'
  task :watch_disk_space, [:mount_point, :threshold] => :environment do |_, args|
    args.with_defaults(mount_point: '/', threshold: 80)

    usage_percentage = CosukaOpsworks::DiskSpace.usage(mount_point: args[:mount_point])
    if usage_percentage >= args[:threshold].to_i
      raise "[#{Socket.gethostname}] Disk space now using #{usage_percentage}%"
    end
  end

  desc 'Watch nginx connections'
  task :watch_nginx_connections, [:threshold] => :environment do |_, args|
    args.with_defaults(threshold: 80)

    require 'cosuka_opsworks/nginx_status'
    status = CosukaOpsworks::NginxStatus.new
    usage_percentage = status.usage
    if usage_percentage >= args[:threshold].to_i
      raise "[#{Socket.gethostname}] Nginx connections now #{usage_percentage}% (#{status.active_connections}/#{status.worker_connections})"
    end
  end

  desc 'Output cron by config/schedule.rb'
  task :output_cron, [:rails_env, :type] => :environment do |_, args|
    require 'socket'
    require 'whenever'

    args.with_defaults(rails_env: 'production', type: 'master')
    master_host = args[:type] == 'master' ? Socket.gethostname : 'dummy.host'

    begin
      original_env = ENV.to_hash
      ENV.update('RAILS_ENV' => args[:rails_env], 'MASTER_HOST' => master_host)
      puts Whenever::JobList.new(file: 'config/schedule.rb').generate_cron_output
    ensure
      ENV.replace(original_env)
    end
  end

  desc 'Update crontab by config/schedule.rb'
  task update_cron: :environment do
    require 'whenever'

    old_tmp = Tempfile.open
    old_crontab = `crontab -l | grep -v "# Begin Whenever" | grep -v "# End Whenever" > #{old_tmp.path}`

    Kernel.system('/usr/local/bin/bundle exec whenever -i rails', exeption: true)

    new_tmp = Tempfile.open
    new_crontab = `crontab -l | grep -v "# Begin Whenever" | grep -v "# End Whenever" > #{new_tmp.path}`

    diff = `diff -u #{old_tmp.path} #{new_tmp.path}`<commit_msg>fix: typo<commit_after>namespace :cosuka_opsworks do
  desc 'Watch disk space'
  task :watch_disk_space, [:mount_point, :threshold] => :environment do |_, args|
    args.with_defaults(mount_point: '/', threshold: 80)

    usage_percentage = CosukaOpsworks::DiskSpace.usage(mount_point: args[:mount_point])
    if usage_percentage >= args[:threshold].to_i
      raise "[#{Socket.gethostname}] Disk space now using #{usage_percentage}%"
    end
  end

  desc 'Watch nginx connections'
  task :watch_nginx_connections, [:threshold] => :environment do |_, args|
    args.with_defaults(threshold: 80)

    require 'cosuka_opsworks/nginx_status'
    status = CosukaOpsworks::NginxStatus.new
    usage_percentage = status.usage
    if usage_percentage >= args[:threshold].to_i
      raise "[#{Socket.gethostname}] Nginx connections now #{usage_percentage}% (#{status.active_connections}/#{status.worker_connections})"
    end
  end

  desc 'Output cron by config/schedule.rb'
  task :output_cron, [:rails_env, :type] => :environment do |_, args|
    require 'socket'
    require 'whenever'

    args.with_defaults(rails_env: 'production', type: 'master')
    master_host = args[:type] == 'master' ? Socket.gethostname : 'dummy.host'

    begin
      original_env = ENV.to_hash
      ENV.update('RAILS_ENV' => args[:rails_env], 'MASTER_HOST' => master_host)
      puts Whenever::JobList.new(file: 'config/schedule.rb').generate_cron_output
    ensure
      ENV.replace(original_env)
    end
  end

  desc 'Update crontab by config/schedule.rb'
  task update_cron: :environment do
    require 'whenever'

    old_tmp = Tempfile.open
    old_crontab = `crontab -l | grep -v "# Begin Whenever" | grep -v "# End Whenever" > #{old_tmp.path}`

    Kernel.system('/usr/local/bin/bundle exec whenever -i rails', exception: true)

    new_tmp = Tempfile.open
    new_crontab = `crontab -l | grep -v "# Begin Whenever" | grep -v "# End Whenever" > #{new_tmp.path}`

    diff = `diff -u #{old_tmp.path} #{new_tmp.path}`
{'fraction_non_alphanumeric': 0.1306520109917562, 'fraction_numerical': 0.0019985011241568824, 'mean_word_length': 4.030150753768845, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7295504', 'n_tokens_mistral': 1364, 'n_tokens_neox': 1313, 'n_words': 304}