Class: TaskLoop::Launch

Inherits:
Command
  • Object
show all
Defined in:
lib/taskloop/command/launch.rb

Constant Summary

Constants inherited from Command

Command::DOLPHIN, Command::LOGO

Instance Method Summary collapse

Methods inherited from Command

#create_dir_if_needed, #create_file_if_needed, #create_taskloop_file_structure_if_needed, #initialize, #taskloop_cron_log_path, #taskloop_cron_tab_path, #taskloop_data_dir, #taskloop_data_proj_dirs, #taskloop_dir, #taskloop_environments_path, #taskloop_proj_list_dirs, #taskloop_proj_list_path, #taskloop_taskfile_paths, #tasklooprc_path

Constructor Details

This class inherits a constructor from TaskLoop::Command

Instance Method Details

#create_environments_file_if_neededObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/taskloop/command/launch.rb', line 18

def create_environments_file_if_needed
  unless File.exists?(taskloop_environments_path)
    env_file = File.new(taskloop_environments_path, "w+")
    variables = ["PATH", "RUBY_VERSION", "GEM_PATH", "GEM_HOME", "IRBRC"]
    variables.each do |var|
      env_file.puts "export " + var + "=#{ENV[var]}"
    end
    env_file.close
  end
end

#create_tasklooprc_file_if_neededObject



29
30
31
32
33
34
35
36
# File 'lib/taskloop/command/launch.rb', line 29

def create_tasklooprc_file_if_needed
  unless File.exists?(tasklooprc_path)
    rc = File.new(tasklooprc_path, "w+")
    rc.puts "source ~/.taskloop/environments"
    rc.puts "taskloop run > ~/.taskloop/cronlog 2>&1"
    rc.close
  end
end

#register_taskloop_into_crontab_if_neededObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/taskloop/command/launch.rb', line 38

def register_taskloop_into_crontab_if_needed
  # TODO: @baocq 考虑 crontab 为空的情况
  system("crontab -l > #{taskloop_cron_tab_path}")
  registered = false
  pattern = /\A\* \* \* \* \* sh ~\/\.tasklooprc/
  File.open(taskloop_cron_tab_path, "r").each_line do |line|
    if line.match?(pattern)
      registered = true
      break
    end
  end

  if registered
    puts "Warning: taskloop has already launched. Please do not launch again.".ansi.yellow
    puts "    If your want to shutdown taskloop, please execute the `taskloop shutdown` command.".ansi.yellow
    return
  end

  File.open(taskloop_cron_tab_path, "a") do |file|
    file.puts "* * * * * sh ~/.tasklooprc"
  end

  system("crontab #{taskloop_cron_tab_path}")

  puts LOGO.ansi.blue
  puts "    taskloop has launched successfully. ".ansi.blue
  puts ""
end

#runObject



11
12
13
14
15
16
# File 'lib/taskloop/command/launch.rb', line 11

def run
  super
  create_environments_file_if_needed
  create_tasklooprc_file_if_needed
  register_taskloop_into_crontab_if_needed
end