fcl_rails_daemon 1.0.12 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +38 -22
- data/bin/fcld +43 -40
- data/fcl_rails_daemon-1.0.12.gem +0 -0
- data/fcl_rails_daemon.gemspec +4 -2
- data/lib/core/daemon.rb +41 -33
- data/lib/core/manager.rb +219 -0
- data/lib/core/recorder.rb +16 -0
- data/lib/fcl_rails_daemon/config.rb +4 -3
- data/lib/fcl_rails_daemon/version.rb +1 -1
- data/lib/fcl_rails_daemon.rb +10 -8
- metadata +10 -9
- data/lib/core/gerenciador.rb +0 -188
- data/lib/core/registrador.rb +0 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bb3a178dbd7dcf9df8c11820dcfcd11c572859eb
|
4
|
+
data.tar.gz: 8e0aaea8fdaff4feb3c9bf00b89b059a8b701d6a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9f171f3e50486485a04a2f7ec1b3c7e6461c0a6ff91ecf560242d4a38e30c5beea33e89c8019af707fce5fa302b61acfa8e1d1a3b698df52046d5065281f64e3
|
7
|
+
data.tar.gz: 7d23cc4f62491883d5595ecb4ec5dff587ce333596fd30d126467432dd5a0e6bdcfafc12137fa48e2ea8adccf22f80f3543750cbc2e7fec0c53bddef86b9278c
|
data/README.md
CHANGED
@@ -1,63 +1,79 @@
|
|
1
1
|
# FclRailsDaemon
|
2
2
|
|
3
|
-
|
4
|
-
executam determinados programas escritos em ruby.
|
3
|
+
This gem was developed to facilitate through the CLI management processes running ruby programs.
|
5
4
|
|
6
|
-
##
|
5
|
+
## Installation
|
7
6
|
|
8
|
-
|
7
|
+
From Gemfile
|
9
8
|
|
10
9
|
```ruby
|
11
10
|
gem 'fcl_rails_daemon'
|
12
11
|
```
|
13
12
|
|
14
|
-
|
13
|
+
Then run:
|
15
14
|
|
16
15
|
$ bundle
|
17
16
|
|
18
|
-
|
17
|
+
Or only install:
|
19
18
|
|
20
19
|
$ gem install fcl_rails_daemon
|
21
20
|
|
22
21
|
|
23
|
-
##
|
22
|
+
## Configuration
|
24
23
|
|
25
|
-
|
24
|
+
After installation you need to create the directories and configuration files for this run
|
26
25
|
|
27
26
|
$ fcld --configure
|
28
27
|
|
29
|
-
|
28
|
+
Will be created:
|
30
29
|
|
31
|
-
*
|
32
|
-
*
|
33
|
-
*
|
30
|
+
* *config/fcld_rails_daemon.rb* (File where the commands are recorded)
|
31
|
+
* *tmp/pids/fcld.yml* (Pids file where the commands are recorded)
|
32
|
+
* *lib/fcl_rails_daemon/command_sample.rb* (A command template)
|
34
33
|
|
35
34
|
|
36
|
-
##
|
35
|
+
## How to use?
|
37
36
|
|
38
|
-
####
|
37
|
+
#### [--create] Create a new command
|
39
38
|
|
40
|
-
$ fcld --create
|
39
|
+
$ fcld --create my_first_command
|
41
40
|
|
42
|
-
*
|
43
|
-
*
|
41
|
+
* Adds a command in lib/fcl_rails_daemon
|
42
|
+
* Records the command in config/fcl_rails_daemon.rb
|
44
43
|
|
45
44
|
|
46
|
-
####
|
45
|
+
#### [--help] Displays the manual for commands and options
|
47
46
|
|
48
47
|
$ fcld --help
|
49
48
|
|
50
49
|
|
51
|
-
####
|
50
|
+
#### [--pids] Displays the pids of the commands registered
|
52
51
|
|
53
52
|
$ fcld --pids
|
54
53
|
|
55
54
|
|
56
|
-
####
|
55
|
+
#### [--logs] Displays the logs files for each registered command
|
56
|
+
|
57
|
+
$ fcld --logs
|
58
|
+
|
59
|
+
|
60
|
+
#### [start|stop|restart|status] Performs action for all registered commands
|
57
61
|
|
58
62
|
$ fcld start
|
59
63
|
|
60
64
|
|
61
|
-
####
|
65
|
+
#### [--env] Sets the environment for Rails application
|
66
|
+
|
67
|
+
$ fcld --env production start
|
68
|
+
|
69
|
+
|
70
|
+
#### [--command] Individual action to run a registered command
|
71
|
+
|
72
|
+
$ fcld --command my_first_command start
|
73
|
+
|
74
|
+
|
75
|
+
#### [--process_name] Sets a name to be assigned to the process (by default the name is the name of the command)
|
76
|
+
|
77
|
+
$ fcld --command my_first_command --process_name foo_my_first_command start
|
78
|
+
|
62
79
|
|
63
|
-
$ fcld --task meu_primeiro_comando start
|
data/bin/fcld
CHANGED
@@ -1,61 +1,59 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
require 'fileutils'
|
3
3
|
require 'fcl_rails_daemon/config'
|
4
|
+
COMMAND['fcld'] = true
|
4
5
|
|
5
6
|
base = DAEMON_ROOT
|
6
|
-
config_dir =
|
7
|
-
config_dir = File.join(base, config_dir)
|
7
|
+
config_dir = File.join(base, DAEMON_CONFIG['config_path'])
|
8
8
|
FileUtils.mkdir_p(config_dir) unless File.directory?(config_dir)
|
9
|
-
|
9
|
+
commands_file = File.join(config_dir, 'fcl_rails_daemon.rb')
|
10
10
|
|
11
11
|
if ARGV.include? "--configure"
|
12
|
-
|
13
|
-
#
|
14
|
-
|
15
|
-
|
16
|
-
|
12
|
+
commands_file_content = <<-FILE
|
13
|
+
# To register commands use the FclRailsDaemon::Recorder class
|
14
|
+
# :command is the command name
|
15
|
+
# :class_reference is the class to which it is
|
16
|
+
#
|
17
|
+
# FclRailsDaemon::Recorder.add(command: 'command_sample', class_reference: CommandSample)
|
17
18
|
|
18
|
-
# Para registrar seus comandos é necessário adicioná-los conforme o exemplo abaixo
|
19
|
-
# É necessário que seja informado o nome do comando e qual a classe ao qual ele representa
|
20
|
-
# Ex:
|
21
|
-
# FclRailsDaemon::Registrador.add(comando: 'comando_exemplo', classe: ComandoExemplo)
|
22
19
|
FILE
|
23
20
|
|
24
|
-
File.open(
|
21
|
+
File.open(commands_file, 'wb') {|f| f.write(commands_file_content) } unless File.exist?(commands_file)
|
25
22
|
|
26
23
|
command_sample_content = <<-FILE
|
27
|
-
class
|
24
|
+
class CommandSample < FclRailsDaemon::Daemon
|
28
25
|
|
29
|
-
#
|
26
|
+
# Is necessary to implement the method "initialize"
|
30
27
|
def initialize
|
31
|
-
#
|
32
|
-
#
|
33
|
-
#
|
34
|
-
|
35
|
-
super(task: "comando_exemplo", log: "log/comando_exemplo.log")
|
28
|
+
# Set the parameter "command" (name that will be referenced in the command entered in the terminal)
|
29
|
+
# The parameter "log" is optional but suggest it is set a log for each command to prevent many commands write on deafult log (if you have many commands in your application)
|
30
|
+
# The parameter "process_name" is optional (is the name that will be assigned to the process)
|
31
|
+
super(command: "command_sample", log: "log/command_sample.log", process_name: "command_sample")
|
36
32
|
end
|
37
33
|
|
38
|
-
#
|
34
|
+
# Is necessary to implement the method "self.help"
|
39
35
|
def self.help
|
40
|
-
#
|
36
|
+
# Should return a hash with " description" and "example"
|
41
37
|
{
|
42
|
-
|
43
|
-
|
38
|
+
description: "This command is a sample - Run every 1 minute",
|
39
|
+
sample: ["--command command_sample |start|stop|restart|status|"]
|
44
40
|
}
|
45
41
|
end
|
46
42
|
|
47
|
-
#
|
43
|
+
# Is necessary to implement the method "run"
|
48
44
|
def run
|
49
|
-
#
|
45
|
+
# Call the run method of the parent class (super) through a block that will contain your code
|
50
46
|
super do
|
51
|
-
#
|
52
|
-
|
53
|
-
|
54
|
-
#
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
47
|
+
# If you want your command be running repeatedly put inside a loop
|
48
|
+
@counter_sample = 0
|
49
|
+
loop do
|
50
|
+
# Write your code here !!!
|
51
|
+
# Do not use Process.exit (true) , exit () , abort ( ) in your code because it infers the death of the Daemon process
|
52
|
+
|
53
|
+
@counter_sample += 1
|
54
|
+
puts "Running "+ @command +" for " + @counter_sample.to_s + " time :)"
|
55
|
+
|
56
|
+
# Wait in seconds before running your command again
|
59
57
|
sleep(10)
|
60
58
|
end
|
61
59
|
end
|
@@ -69,7 +67,7 @@ end
|
|
69
67
|
commands_dir = File.join(base, commands_dir)
|
70
68
|
FileUtils.mkdir_p(commands_dir) unless File.directory?(commands_dir)
|
71
69
|
|
72
|
-
command_file = File.join(commands_dir, '
|
70
|
+
command_file = File.join(commands_dir, 'command_sample.rb')
|
73
71
|
File.open(command_file, 'wb') {|f| f.write(command_sample_content) } unless File.exists?(command_file)
|
74
72
|
|
75
73
|
log_dir = "log"
|
@@ -79,18 +77,23 @@ end
|
|
79
77
|
pids_dir = File.join(base, "tmp/pids")
|
80
78
|
pids_file = File.join(base, DAEMON_CONFIG['pids_file'])
|
81
79
|
FileUtils.mkdir_p(pids_dir) unless File.directory?(pids_dir)
|
82
|
-
File.open(pids_file, 'wb') {|f| f << "
|
80
|
+
File.open(pids_file, 'wb') {|f| f << "fcl_rails_daemon:" } unless File.exist?(pids_file)
|
83
81
|
|
84
|
-
puts "
|
82
|
+
puts " ༼ つ ◕_◕ ༽つ OK... Gem has been set!!! "
|
85
83
|
exit
|
86
84
|
end
|
87
85
|
|
88
|
-
unless File.exist?(
|
89
|
-
puts "
|
86
|
+
unless File.exist?(commands_file)
|
87
|
+
puts " ༼ つ ◕_◕ ༽つ OOOPS... Gem has not yet been set. To set run 'fcld --configure' "
|
90
88
|
exit
|
91
89
|
end
|
92
90
|
|
93
91
|
require 'fcl_rails_daemon'
|
94
92
|
require 'fcl_rails_daemon/version'
|
95
93
|
|
96
|
-
|
94
|
+
begin
|
95
|
+
FclRailsDaemon::Manager.run(ARGV)
|
96
|
+
rescue => e
|
97
|
+
puts e.message
|
98
|
+
end
|
99
|
+
|
Binary file
|
data/fcl_rails_daemon.gemspec
CHANGED
@@ -16,9 +16,11 @@ Gem::Specification.new do |spec|
|
|
16
16
|
spec.test_files = `git ls-files -- {test,spec}/*`.split("\n")
|
17
17
|
spec.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
18
|
spec.require_paths = ["lib"]
|
19
|
+
spec.required_ruby_version = '>= 2.1.2'
|
19
20
|
|
20
|
-
spec.summary = %q{
|
21
|
-
spec.description = %q{
|
21
|
+
spec.summary = %q{Project designed to generate commands that run in the background (daemons).}
|
22
|
+
spec.description = %q{ This aims to make it easy to create commands ( ruby scripts) and run them in the background (daemon).
|
23
|
+
CLI has easy to use, where to control the processes related to commands.}
|
22
24
|
|
23
25
|
spec.add_dependency "bundler"
|
24
26
|
spec.add_dependency "activesupport"
|
data/lib/core/daemon.rb
CHANGED
@@ -1,16 +1,18 @@
|
|
1
1
|
module FclRailsDaemon
|
2
2
|
class Daemon
|
3
|
+
cattr_reader :commands_valid
|
4
|
+
attr_accessor :process_name
|
5
|
+
attr_accessor :log_file
|
3
6
|
|
4
|
-
cattr_reader :comandos_validos
|
5
7
|
@@pids_file = File.join(DAEMON_ROOT, DAEMON_CONFIG['pids_file'])
|
6
|
-
@@
|
8
|
+
@@commands_valid = ['start', 'stop', 'restart', 'status']
|
7
9
|
|
8
|
-
|
9
|
-
|
10
|
-
raise "Não foi definida o nome da task no construtor do comando" unless task
|
10
|
+
def initialize(command: nil, log: nil, process_name: nil)
|
11
|
+
raise " ༼ つ ◕_◕ ༽つ OOOPS... It was not set the 'command' name in the command initialize. " unless command
|
11
12
|
@daemon ||= nil
|
12
|
-
@
|
13
|
-
@
|
13
|
+
@command = command
|
14
|
+
@log_file = (log) ? File.join(DAEMON_ROOT, log) : File.join(DAEMON_ROOT, DAEMON_CONFIG['default_log'])
|
15
|
+
@process_name = (process_name) ? process_name : command
|
14
16
|
end
|
15
17
|
|
16
18
|
def self.pids
|
@@ -19,18 +21,24 @@ module FclRailsDaemon
|
|
19
21
|
end
|
20
22
|
|
21
23
|
def self.help
|
22
|
-
raise "
|
23
|
-
end
|
24
|
-
|
25
|
-
def log
|
26
|
-
puts "#{@log} (#{@task})"
|
24
|
+
raise " ༼ つ ◕_◕ ༽つ OOOPS... It was not implemented 'self.help' method in command "
|
27
25
|
end
|
28
26
|
|
29
27
|
def run(&block)
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
28
|
+
#Load environment file (rails project)
|
29
|
+
if COMMAND['fcld']
|
30
|
+
env_file = File.join(DAEMON_ROOT, "config", "environment.rb")
|
31
|
+
raise " ༼ つ ◕_◕ ༽つ OOOPS... Could not find the Rails environment file. " unless File.exist? env_file
|
32
|
+
require env_file
|
33
|
+
end
|
34
|
+
|
35
|
+
@daemon = Daemons.call(multiple: true, app_name: @process_name) do
|
36
|
+
#set process_name
|
37
|
+
Process.setproctitle(@process_name)
|
38
|
+
|
39
|
+
# Force the output to the defined log
|
40
|
+
$stdout.reopen(@log_file, 'a')
|
41
|
+
$stderr.reopen(@log_file, 'a')
|
34
42
|
$stdout.sync = true
|
35
43
|
|
36
44
|
block.call
|
@@ -38,20 +46,20 @@ module FclRailsDaemon
|
|
38
46
|
end
|
39
47
|
|
40
48
|
def start
|
41
|
-
pid = get_pid @
|
49
|
+
pid = get_pid @command
|
42
50
|
if running?(pid)
|
43
|
-
puts "
|
51
|
+
puts "#{@process_name}: process with pid #{pid} is already running."
|
44
52
|
return
|
45
53
|
end
|
46
54
|
run
|
47
|
-
set_pid(@
|
55
|
+
set_pid(@command, @daemon.pid.pid) if @daemon
|
48
56
|
end
|
49
57
|
|
50
58
|
def stop
|
51
|
-
pid = get_pid @
|
59
|
+
pid = get_pid @command
|
52
60
|
|
53
61
|
unless running? pid
|
54
|
-
puts "
|
62
|
+
puts "#{@process_name}: process is not running."
|
55
63
|
return
|
56
64
|
end
|
57
65
|
kill pid
|
@@ -63,23 +71,23 @@ module FclRailsDaemon
|
|
63
71
|
end
|
64
72
|
|
65
73
|
def status
|
66
|
-
pid = get_pid @
|
74
|
+
pid = get_pid @command
|
67
75
|
if running? pid
|
68
|
-
puts "
|
76
|
+
puts "#{@process_name}: process with pid #{pid} is running."
|
69
77
|
else
|
70
|
-
puts "
|
78
|
+
puts "#{@process_name}: process is not running."
|
71
79
|
end
|
72
80
|
end
|
73
81
|
|
74
82
|
private
|
75
|
-
def get_pid(
|
83
|
+
def get_pid(command)
|
76
84
|
pids = YAML.load_file(@@pids_file)
|
77
|
-
(pids.has_key?(
|
85
|
+
(pids.has_key?(command)) ? pids[command] : nil
|
78
86
|
end
|
79
87
|
|
80
|
-
def set_pid(
|
88
|
+
def set_pid(command, pid)
|
81
89
|
pids = YAML.load_file(@@pids_file)
|
82
|
-
pids[
|
90
|
+
pids[command] = pid
|
83
91
|
File.open(@@pids_file, 'wb') do |f|
|
84
92
|
f << pids.to_yaml
|
85
93
|
end
|
@@ -103,20 +111,20 @@ module FclRailsDaemon
|
|
103
111
|
if pid
|
104
112
|
begin
|
105
113
|
Process.kill("KILL", pid)
|
106
|
-
set_pid(@
|
114
|
+
set_pid(@command, nil)
|
107
115
|
status = true
|
108
116
|
rescue Errno::ESRCH
|
109
|
-
puts "
|
117
|
+
puts "#{@process_name}: there is no process with pid #{pid}."
|
110
118
|
status = false
|
111
119
|
ensure
|
112
120
|
if status == false
|
113
|
-
puts "
|
121
|
+
puts "#{@process_name}: process is not running."
|
114
122
|
else
|
115
|
-
puts "
|
123
|
+
puts "#{@process_name}: process was stopped."
|
116
124
|
end
|
117
125
|
end
|
118
126
|
else
|
119
|
-
puts "
|
127
|
+
puts "#{@process_name}: there is no process with pid #{pid}."
|
120
128
|
end
|
121
129
|
end
|
122
130
|
|
data/lib/core/manager.rb
ADDED
@@ -0,0 +1,219 @@
|
|
1
|
+
module FclRailsDaemon
|
2
|
+
class Manager
|
3
|
+
@@commands = FclRailsDaemon::Recorder.load
|
4
|
+
|
5
|
+
def self.run(argv)
|
6
|
+
COMMAND['fcld'] = true
|
7
|
+
self.set_env(argv) if argv.include?('--env')
|
8
|
+
self.pids if argv.include?('--pids')
|
9
|
+
self.logs if argv.include?('--logs')
|
10
|
+
self.set_process_name(argv) if (argv.include?('--command') && argv.include?('--process_name'))
|
11
|
+
self.create_command(argv) if argv.include?('--create')
|
12
|
+
self.help(ARGV) unless self.valid?(argv)
|
13
|
+
|
14
|
+
command ||= nil
|
15
|
+
action = argv.last
|
16
|
+
if argv.include?('--command')
|
17
|
+
i = argv.index('--command') + 1
|
18
|
+
command = argv[i]
|
19
|
+
end
|
20
|
+
registered = self.get_registered command
|
21
|
+
registered.each { |command| command.send(action) }
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.help(argv)
|
25
|
+
action = argv.last
|
26
|
+
command ||= nil
|
27
|
+
if (argv.include?('--command') && argv.include?('--help'))
|
28
|
+
i = argv.index('--command') + 1
|
29
|
+
command = argv[i]
|
30
|
+
end
|
31
|
+
helpers = self.get_helpers(command)
|
32
|
+
self.show_helpers helpers
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.valid?(argv)
|
36
|
+
Daemon.commands_valid.include?(argv.last) && !(argv.include?('--help'))
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def self.get_registered(command = nil)
|
42
|
+
list = []
|
43
|
+
@@registered ||= {}
|
44
|
+
if command
|
45
|
+
raise " ༼ つ ◕_◕ ༽つ OOOPS... Command '#{command}' is not registered. " unless @@commands.has_key? command
|
46
|
+
@@registered[command] = (@@registered.has_key? command) ? @@registered[command] : @@commands[command].new
|
47
|
+
list << @@registered[command]
|
48
|
+
return list
|
49
|
+
end
|
50
|
+
@@commands.each do |k, c|
|
51
|
+
list << @@registered[k] = (@@registered.has_key? k) ? @@registered[k] : c.new
|
52
|
+
end
|
53
|
+
list
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.get_helpers(command = nil)
|
57
|
+
list = []
|
58
|
+
@@helpers ||= {}
|
59
|
+
if command
|
60
|
+
raise " ༼ つ ◕_◕ ༽つ OOOPS... Command '#{command}' is not registered. " unless @@commands.has_key? command
|
61
|
+
@@helpers[command] = (@@helpers.has_key? command) ? @@helpers[command] : @@commands[command].help
|
62
|
+
list << @@helpers[command]
|
63
|
+
return list
|
64
|
+
end
|
65
|
+
@@commands.each do |k, c|
|
66
|
+
list << @@helpers[k] = c.help unless @@helpers.has_key?(k)
|
67
|
+
end
|
68
|
+
list
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.show_helpers(helpers)
|
72
|
+
prefix = DAEMON_CONFIG['command_prefix']
|
73
|
+
|
74
|
+
puts "\n------------------------------------------------------------------------------------------------------------\n"
|
75
|
+
puts " FCL_RAILS_DAEMON\n"
|
76
|
+
puts "------------------------------------------------------------------------------------------------------------\n"
|
77
|
+
puts " [start|stop|restart|status] option to control all processes at once"
|
78
|
+
puts " #{prefix} start\n\n"
|
79
|
+
puts " [--help] to view the manual \n"
|
80
|
+
puts " #{prefix} --help\n"
|
81
|
+
puts " [--command] to see the manual of a specific command\n"
|
82
|
+
puts " #{prefix} --command sample_command --help\n\n"
|
83
|
+
puts " [--env] to define the environment of the Rails application"
|
84
|
+
puts " #{prefix} --env production start\n\n"
|
85
|
+
puts " [--create] to create a new command"
|
86
|
+
puts " #{prefix} --create my_first_command\n\n"
|
87
|
+
puts " [--command] to control specific command"
|
88
|
+
puts " #{prefix} --command sample_command start\n"
|
89
|
+
puts " [--process_name] to define a name for the process"
|
90
|
+
puts " #{prefix} --command sample_command --process_name my_process start\n\n"
|
91
|
+
puts " [--pids] option to see pids process for each command"
|
92
|
+
puts " #{prefix} --pids\n\n"
|
93
|
+
puts " [--logs] option to see the log files set for each command"
|
94
|
+
puts " #{prefix} --logs\n\n"
|
95
|
+
puts "------------------------------------------------------------------------------------------------------------\n"
|
96
|
+
puts " COMMANDS REGISTERED\n"
|
97
|
+
puts "------------------------------------------------------------------------------------------------------------\n"
|
98
|
+
|
99
|
+
helpers.each do |h|
|
100
|
+
puts " #{h[:description]}"
|
101
|
+
h[:sample].each do |e|
|
102
|
+
puts " #{e}"
|
103
|
+
end
|
104
|
+
puts ""
|
105
|
+
end
|
106
|
+
exit
|
107
|
+
end
|
108
|
+
|
109
|
+
def self.set_env(argv)
|
110
|
+
i = argv.index('--env') + 1
|
111
|
+
env = argv[i]
|
112
|
+
ENV['RAILS_ENV'] = env
|
113
|
+
end
|
114
|
+
|
115
|
+
def self.create_command(argv)
|
116
|
+
i = argv.index('--create') + 1
|
117
|
+
command = argv[i]
|
118
|
+
unless command
|
119
|
+
puts " ༼ つ ◕_◕ ༽つ OOOPS... The command name has not been defined "
|
120
|
+
exit
|
121
|
+
end
|
122
|
+
if Daemon.commands_valid.include? command
|
123
|
+
puts " ༼ つ ◕_◕ ༽つ OOOPS... The command can not be named #{command} "
|
124
|
+
exit
|
125
|
+
end
|
126
|
+
|
127
|
+
command_camel = ActiveSupport::Inflector.camelize(command)
|
128
|
+
command_undescore = ActiveSupport::Inflector.underscore(command)
|
129
|
+
|
130
|
+
content = <<-FILE
|
131
|
+
class #{command_camel} < FclRailsDaemon::Daemon
|
132
|
+
|
133
|
+
# Is necessary to implement the method "initialize"
|
134
|
+
def initialize
|
135
|
+
# Set the parameter "command" (name that will be referenced in the command entered in the terminal)
|
136
|
+
# The parameter "log" is optional but suggest it is set a log for each command to prevent many commands write on deafult log (if you have many commands in your application)
|
137
|
+
# The parameter "process_name" is optional (is the name that will be assigned to the process)
|
138
|
+
super(command: "#{command_undescore}", log: "log/#{command_undescore}.log", process_name: "#{command_undescore}")
|
139
|
+
end
|
140
|
+
|
141
|
+
# Is necessary to implement the method "self.help"
|
142
|
+
def self.help
|
143
|
+
# Should return a hash with " description" and "example"
|
144
|
+
{
|
145
|
+
description: "This command is a sample - Run every 1 minute",
|
146
|
+
sample: ["--command #{command_undescore} |start|stop|restart|status|"]
|
147
|
+
}
|
148
|
+
end
|
149
|
+
|
150
|
+
# Is necessary to implement the method "run"
|
151
|
+
def run
|
152
|
+
# Call the run method of the parent class (super) through a block that will contain your code
|
153
|
+
super do
|
154
|
+
# If you want your command be running repeatedly put inside a loop
|
155
|
+
@counter_sample = 0
|
156
|
+
loop do
|
157
|
+
# Write your code here !!!
|
158
|
+
# Do not use Process.exit (true) , exit () , abort ( ) in your code because it infers the death of the Daemon process
|
159
|
+
|
160
|
+
@counter_sample += 1
|
161
|
+
puts "Running "+ @command +" for " + @counter_sample.to_s + " time :)"
|
162
|
+
|
163
|
+
# Wait in seconds before running your command again
|
164
|
+
sleep(10)
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
end
|
170
|
+
FILE
|
171
|
+
|
172
|
+
file = File.join(DAEMON_ROOT, DAEMON_CONFIG['command_path'], command_undescore + '.rb' )
|
173
|
+
|
174
|
+
if File.exist?(file)
|
175
|
+
puts " ༼ つ ◕_◕ ༽つ OOOPS... Command already exists. "
|
176
|
+
else
|
177
|
+
File.open(file, 'wb') {|f| f.write(content) }
|
178
|
+
|
179
|
+
file_record = File.join(DAEMON_ROOT, DAEMON_CONFIG['register_file'] )
|
180
|
+
content_to_register = "\nFclRailsDaemon::Recorder.add(command: '#{command_undescore}', ref_class: #{command_camel})"
|
181
|
+
File.open(file_record, 'a+') {|f| f << content_to_register }
|
182
|
+
|
183
|
+
puts " ༼ つ ◕_◕ ༽つ OK... Command created and registered!!! "
|
184
|
+
puts "New command: #{file} "
|
185
|
+
puts "Commands registered: #{file_record} "
|
186
|
+
end
|
187
|
+
exit
|
188
|
+
end
|
189
|
+
|
190
|
+
def self.set_process_name(argv)
|
191
|
+
i = argv.index('--command') + 1
|
192
|
+
command = argv[i]
|
193
|
+
i = argv.index('--process_name') + 1
|
194
|
+
name = argv[i]
|
195
|
+
if Daemon.commands_valid.include? command
|
196
|
+
puts " ༼ つ ◕_◕ ༽つ OOOPS... The command can not be named #{command} "
|
197
|
+
exit
|
198
|
+
end
|
199
|
+
if Daemon.commands_valid.include? name
|
200
|
+
puts " ༼ つ ◕_◕ ༽つ OOOPS... The process can not be named #{command} "
|
201
|
+
exit
|
202
|
+
end
|
203
|
+
registered = self.get_registered command
|
204
|
+
registered.each { |command| command.process_name = name }
|
205
|
+
end
|
206
|
+
|
207
|
+
def self.logs
|
208
|
+
registered = self.get_registered nil
|
209
|
+
registered.each { |command| puts "#{command.process_name}: #{command.log_file}" }
|
210
|
+
exit
|
211
|
+
end
|
212
|
+
|
213
|
+
def self.pids
|
214
|
+
puts Daemon.pids
|
215
|
+
exit
|
216
|
+
end
|
217
|
+
|
218
|
+
end
|
219
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module FclRailsDaemon
|
2
|
+
class Recorder
|
3
|
+
@@commands ||= {}
|
4
|
+
|
5
|
+
def self.add(command: nil, class_reference: nil)
|
6
|
+
raise " ༼ つ ◕_◕ ༽つ OOOPS... Attribute 'command' does not set." unless command
|
7
|
+
raise " ༼ つ ◕_◕ ༽つ OOOPS... Attribute 'ref_class' does not set." unless class_reference
|
8
|
+
@@commands[command] = class_reference
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.load
|
12
|
+
@@commands
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
@@ -1,9 +1,10 @@
|
|
1
|
+
COMMAND = {'fcld' => false}
|
1
2
|
DAEMON_ROOT = File.expand_path("")
|
2
|
-
|
3
3
|
DAEMON_CONFIG = {
|
4
4
|
"pids_file" => 'tmp/pids/fcld.yml',
|
5
5
|
"default_log" => 'log/fcld.log',
|
6
6
|
"command_prefix" => 'fcld',
|
7
|
-
"command_path" => 'lib/
|
8
|
-
"
|
7
|
+
"command_path" => 'lib/fcl_rails_daemon',
|
8
|
+
"config_path" => 'config',
|
9
|
+
"register_file" => 'config/fcl_rails_daemon.rb'
|
9
10
|
}
|
data/lib/fcl_rails_daemon.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require "rails"
|
1
2
|
require "fcl_rails_daemon/version"
|
2
3
|
require "fcl_rails_daemon/config"
|
3
4
|
require 'fileutils'
|
@@ -6,13 +7,14 @@ require 'daemons'
|
|
6
7
|
require 'active_support'
|
7
8
|
|
8
9
|
module FclRailsDaemon end
|
9
|
-
|
10
10
|
require_relative "core/daemon"
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
11
|
+
require_relative "core/recorder.rb"
|
12
|
+
require_relative "core/manager.rb"
|
13
|
+
|
14
|
+
# Load commands files
|
15
|
+
command_dir = File.join(DAEMON_ROOT, DAEMON_CONFIG['command_path'])
|
16
|
+
raise " ༼ つ ◕_◕ ༽つ OOOPS... Could not find the command directory. Run 'fcld --configure' " unless File.directory? command_dir
|
17
|
+
Dir[File.join(command_dir, "**/*.rb")].each {|file| require file }
|
18
|
+
|
19
|
+
require File.join(DAEMON_ROOT, "config", "fcl_rails_daemon.rb")
|
17
20
|
|
18
|
-
require File.join(DAEMON_ROOT, "config", "fcld_rails_daemon.rb")
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fcl_rails_daemon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Washington Silva
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-01-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -80,8 +80,9 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
|
-
description:
|
84
|
-
|
83
|
+
description: |2-
|
84
|
+
This aims to make it easy to create commands ( ruby scripts) and run them in the background (daemon).
|
85
|
+
CLI has easy to use, where to control the processes related to commands.
|
85
86
|
email:
|
86
87
|
- w-osilva@hotmail.com
|
87
88
|
executables:
|
@@ -104,6 +105,7 @@ files:
|
|
104
105
|
- fcl_rails_daemon-1.0.1.gem
|
105
106
|
- fcl_rails_daemon-1.0.10.gem
|
106
107
|
- fcl_rails_daemon-1.0.11.gem
|
108
|
+
- fcl_rails_daemon-1.0.12.gem
|
107
109
|
- fcl_rails_daemon-1.0.2.gem
|
108
110
|
- fcl_rails_daemon-1.0.3.gem
|
109
111
|
- fcl_rails_daemon-1.0.4.gem
|
@@ -114,8 +116,8 @@ files:
|
|
114
116
|
- fcl_rails_daemon-1.0.9.gem
|
115
117
|
- fcl_rails_daemon.gemspec
|
116
118
|
- lib/core/daemon.rb
|
117
|
-
- lib/core/
|
118
|
-
- lib/core/
|
119
|
+
- lib/core/manager.rb
|
120
|
+
- lib/core/recorder.rb
|
119
121
|
- lib/fcl_rails_daemon.rb
|
120
122
|
- lib/fcl_rails_daemon/config.rb
|
121
123
|
- lib/fcl_rails_daemon/version.rb
|
@@ -133,7 +135,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
133
135
|
requirements:
|
134
136
|
- - ">="
|
135
137
|
- !ruby/object:Gem::Version
|
136
|
-
version:
|
138
|
+
version: 2.1.2
|
137
139
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
138
140
|
requirements:
|
139
141
|
- - ">="
|
@@ -144,6 +146,5 @@ rubyforge_project: fcl_rails_daemon
|
|
144
146
|
rubygems_version: 2.4.8
|
145
147
|
signing_key:
|
146
148
|
specification_version: 4
|
147
|
-
summary:
|
148
|
-
(daemons).
|
149
|
+
summary: Project designed to generate commands that run in the background (daemons).
|
149
150
|
test_files: []
|
data/lib/core/gerenciador.rb
DELETED
@@ -1,188 +0,0 @@
|
|
1
|
-
module FclRailsDaemon
|
2
|
-
class Gerenciador
|
3
|
-
@@comandos = Registrador.load
|
4
|
-
|
5
|
-
def self.run(argv)
|
6
|
-
|
7
|
-
if argv.include?('--pids')
|
8
|
-
puts Daemon.pids
|
9
|
-
exit
|
10
|
-
end
|
11
|
-
|
12
|
-
if argv.include?('--logs')
|
13
|
-
registrados = self.get_registrados nil
|
14
|
-
registrados.each { |comando| comando.send('log') }
|
15
|
-
exit
|
16
|
-
end
|
17
|
-
|
18
|
-
self.criar_comando(argv) if argv.include?('--create')
|
19
|
-
|
20
|
-
self.help(ARGV) unless self.valid?(argv)
|
21
|
-
|
22
|
-
task ||= nil
|
23
|
-
acao = argv.last
|
24
|
-
if argv.include?('--task')
|
25
|
-
i = argv.index('--task') + 1
|
26
|
-
task = argv[i]
|
27
|
-
end
|
28
|
-
registrados = self.get_registrados task
|
29
|
-
registrados.each { |comando| comando.send(acao) }
|
30
|
-
end
|
31
|
-
|
32
|
-
|
33
|
-
def self.help(argv)
|
34
|
-
acao = argv.last
|
35
|
-
task ||= nil
|
36
|
-
if (argv.include?('--task') && argv.include?('--help'))
|
37
|
-
i = argv.index('--task') + 1
|
38
|
-
task = argv[i]
|
39
|
-
end
|
40
|
-
helpers = self.get_helpers(task)
|
41
|
-
self.show helpers
|
42
|
-
end
|
43
|
-
|
44
|
-
def self.valid?(argv)
|
45
|
-
Daemon.comandos_validos.include?(argv.last) && !(argv.include?('--help'))
|
46
|
-
end
|
47
|
-
|
48
|
-
private
|
49
|
-
def self.show(helpers)
|
50
|
-
prefixo = DAEMON_CONFIG['command_prefix']
|
51
|
-
|
52
|
-
puts "\n------------------------------------------------------------------------------------------------------------\n"
|
53
|
-
puts " FCL_RAILS_DAEMON\n"
|
54
|
-
puts "------------------------------------------------------------------------------------------------------------\n"
|
55
|
-
puts " * Use a opção --help para ver o manual (--task para ver o manual de um comando expecifico)\n"
|
56
|
-
puts " #{prefixo} --help\n"
|
57
|
-
puts " #{prefixo} --task comando --help\n\n"
|
58
|
-
puts " * Use a opção --create para criar um novo comando\n"
|
59
|
-
puts " #{prefixo} --create meu_primeiro_comando\n\n"
|
60
|
-
puts " * Use a opção start| stop | restart | status para controlar todos processos de uma vez\n"
|
61
|
-
puts " #{prefixo} start\n\n"
|
62
|
-
puts " * Use a opção --pids para ver os pids de processos\n"
|
63
|
-
puts " #{prefixo} --pids\n\n"
|
64
|
-
puts " * Use a opção --logs para ver os arquivos de log configurados para os comandos\n"
|
65
|
-
puts " #{prefixo} --logs\n\n"
|
66
|
-
puts " * Use a opção --task nome_comando start | stop | restart | status para controlar processos individualmente\n"
|
67
|
-
puts " #{prefixo} --task comando start\n\n"
|
68
|
-
puts "------------------------------------------------------------------------------------------------------------\n"
|
69
|
-
puts " LISTA DE COMANDOS REGISTRADOS\n"
|
70
|
-
puts "------------------------------------------------------------------------------------------------------------\n"
|
71
|
-
|
72
|
-
helpers.each do |h|
|
73
|
-
puts " * #{h[:descricao]}"
|
74
|
-
h[:exemplo].each do |e|
|
75
|
-
puts " #{e}"
|
76
|
-
end
|
77
|
-
puts ""
|
78
|
-
end
|
79
|
-
exit
|
80
|
-
end
|
81
|
-
|
82
|
-
def self.get_registrados(task = nil)
|
83
|
-
lista = []
|
84
|
-
@@registrados ||= {}
|
85
|
-
if task
|
86
|
-
raise "Comando não registrado # #{task} #" unless @@comandos.has_key? task
|
87
|
-
@@registrados[task] = (@@registrados.has_key? task) ? @@registrados[task] : @@comandos[task].new
|
88
|
-
lista << @@registrados[task]
|
89
|
-
return lista
|
90
|
-
end
|
91
|
-
@@comandos.each do |k, c|
|
92
|
-
lista << @@registrados[k] = (@@registrados.has_key? k) ? @@registrados[k] : c.new
|
93
|
-
end
|
94
|
-
lista
|
95
|
-
end
|
96
|
-
|
97
|
-
def self.get_helpers(task = nil)
|
98
|
-
lista = []
|
99
|
-
@@helpers ||= {}
|
100
|
-
if task
|
101
|
-
raise "Comando não registrado # #{task} #" unless @@comandos.has_key? task
|
102
|
-
@@helpers[task] = (@@helpers.has_key? task) ? @@helpers[task] : @@comandos[task].help
|
103
|
-
lista << @@helpers[task]
|
104
|
-
return lista
|
105
|
-
end
|
106
|
-
@@comandos.each do |k, c|
|
107
|
-
lista << @@helpers[k] = c.help unless @@helpers.has_key?(k)
|
108
|
-
end
|
109
|
-
lista
|
110
|
-
end
|
111
|
-
|
112
|
-
def self.criar_comando(argv)
|
113
|
-
|
114
|
-
i = argv.index('--create') + 1
|
115
|
-
comando = argv[i]
|
116
|
-
unless comando
|
117
|
-
puts "O nome do comando não foi definido"
|
118
|
-
exit
|
119
|
-
end
|
120
|
-
if Daemon.comandos_validos.include? comando
|
121
|
-
puts "O comando não pode ter o nome #{comando}"
|
122
|
-
exit
|
123
|
-
end
|
124
|
-
|
125
|
-
comando_camel = ActiveSupport::Inflector.camelize(comando)
|
126
|
-
comando_undescore = ActiveSupport::Inflector.underscore(comando)
|
127
|
-
|
128
|
-
conteudo = <<-FILE
|
129
|
-
class #{comando_camel} < FclRailsDaemon::Daemon
|
130
|
-
|
131
|
-
# Obrigatóriamente é necessário implementar o método "initialize"
|
132
|
-
def initialize
|
133
|
-
# Definir o parametro "task" (nome que será referenciado no comando digitado no terminal).
|
134
|
-
#
|
135
|
-
# O parametro "log" é opcional mas sugiro que seja definido um log para cada comando para evitar que muitos comandos
|
136
|
-
# escrevam no log deafult (caso tenha# muitos comandos)
|
137
|
-
super(task: "#{comando_undescore}", log: "log/#{comando_undescore}.log")
|
138
|
-
end
|
139
|
-
|
140
|
-
# Obrigatóriamente é necessário implementar o método self.help
|
141
|
-
def self.help
|
142
|
-
# Retornar Hash com "descricao" e "exemplo"
|
143
|
-
{
|
144
|
-
descricao: "Descrição do comando #{comando_undescore} :) - Executado a cada 1 min",
|
145
|
-
exemplo: ["--task #{comando_undescore} start | stop | restart | status"]
|
146
|
-
}
|
147
|
-
end
|
148
|
-
|
149
|
-
# Obrigatóriamente é necessário implementar o método run
|
150
|
-
def run
|
151
|
-
# Chamar o método run da classe pai (super) passando um bloco que vai conter seu código
|
152
|
-
super do
|
153
|
-
# Caso deseje que o seu comando fique executando repetidamente coloque dentro de um loop
|
154
|
-
loop do
|
155
|
-
# Escreva seu código aqui!!
|
156
|
-
# Não use Process.exit(true), exit(), abort() em seu codigo pois infere na morte do processo do Daemon
|
157
|
-
puts "Está executando #{comando_undescore}! :)"
|
158
|
-
|
159
|
-
# Espera em segundos antes de executar seu comando outra vez
|
160
|
-
# Util no caso de simular um processo cronológico (esse exemplo vai executar o comando a cada 10 segundos)
|
161
|
-
sleep(10)
|
162
|
-
end
|
163
|
-
end
|
164
|
-
end
|
165
|
-
|
166
|
-
end
|
167
|
-
FILE
|
168
|
-
|
169
|
-
arquivo = File.join(DAEMON_ROOT, DAEMON_CONFIG['command_path'], comando_undescore + '.rb' )
|
170
|
-
|
171
|
-
if File.exist?(arquivo)
|
172
|
-
puts "Comando já existe..."
|
173
|
-
else
|
174
|
-
File.open(arquivo, 'wb') {|f| f.write(conteudo) }
|
175
|
-
|
176
|
-
arquivo_registro = File.join(DAEMON_ROOT, DAEMON_CONFIG['register_file'] )
|
177
|
-
conteudo_registrar = "\nFclRailsDaemon::Registrador.add(comando: '#{comando_undescore}', classe: #{comando_camel})"
|
178
|
-
File.open(arquivo_registro, 'a+') {|f| f << conteudo_registrar }
|
179
|
-
|
180
|
-
puts "Comando criado e registrado... "
|
181
|
-
puts "Arquivo: #{arquivo} "
|
182
|
-
puts "Registro: #{arquivo_registro} "
|
183
|
-
end
|
184
|
-
exit
|
185
|
-
end
|
186
|
-
|
187
|
-
end
|
188
|
-
end
|
data/lib/core/registrador.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
module FclRailsDaemon
|
2
|
-
class Registrador
|
3
|
-
@@comandos ||= {}
|
4
|
-
|
5
|
-
def self.add(comando: nil, classe: nil)
|
6
|
-
raise "Comando não definido" unless comando
|
7
|
-
raise "Classe não definida" unless classe
|
8
|
-
@@comandos[comando] = classe
|
9
|
-
end
|
10
|
-
|
11
|
-
def self.load
|
12
|
-
@@comandos
|
13
|
-
end
|
14
|
-
|
15
|
-
end
|
16
|
-
end
|