epi 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ module Epi
2
+ module Server
3
+ module Responders
4
+ class Shutdown < Responder
5
+
6
+ attr_accessor :command, :arguments
7
+
8
+ def run
9
+ raise Exceptions::Shutdown
10
+ end
11
+
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,52 @@
1
+ module Epi
2
+ module Server
3
+ module Responders
4
+ # noinspection RubyStringKeysInHashInspection
5
+ class Status < Responder
6
+
7
+ def run
8
+ Jobs.beat!
9
+ YAML.dump stats
10
+ end
11
+
12
+ private
13
+
14
+ def stats
15
+ {
16
+ 'Running as' => `whoami`.chomp,
17
+ 'Since' => Server.start_time.strftime('%c'),
18
+ 'Jobs' => jobs
19
+ }
20
+ end
21
+
22
+ def jobs
23
+ all = Jobs.map { |id, j| ["#{j.job_description.name} [#{id}]", job(j)] }.to_h
24
+ all.count > 0 ? all : 'none'
25
+ end
26
+
27
+ def job(j)
28
+ all = processes(j.pids).merge processes(j.dying_pids, 'dying')
29
+ all.count > 0 ? all : 'paused'
30
+ end
31
+
32
+ def processes(pids, state = nil)
33
+ pids.values.map do |pid|
34
+ name = 'PID ' << pid.to_s
35
+ name << " [#{state}]" if state
36
+ [name, process(pid)]
37
+ end.to_h
38
+ end
39
+
40
+ def process(pid)
41
+ rp = ProcessStatus[pid]
42
+ {
43
+ 'Since' => rp.started_at.strftime('%c'),
44
+ 'CPU' => '%0.1f%' % rp.cpu_percentage,
45
+ 'Memory' => '%0.1f%' % rp.memory_percentage
46
+ }
47
+ end
48
+
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,64 @@
1
+ require 'eventmachine'
2
+ require 'bson'
3
+
4
+ module Epi
5
+ module Server
6
+ class Sender < EventMachine::Connection
7
+ include Exceptions
8
+
9
+ # Send a message to the Epi server
10
+ #
11
+ # @example Get Epi's status
12
+ # Sender.send command: {command: 'status', arguments: []}
13
+ #
14
+ # @example Shut down the server
15
+ # Sender.send :shutdown
16
+ #
17
+ # @param what [Hash|Symbol] Either a symbol being the message type, or a hash
18
+ # with a single key (a symbol) being the message type, and value (a hash) being the message.
19
+ def self.send(what)
20
+
21
+ raise ArgumentError, 'Expected a hash with one key (a symbol) and value (a hash)' unless
22
+ Symbol === what ||
23
+ (Hash === what && what.count == 1 && Symbol === what.keys.first && Hash === what.values.first)
24
+
25
+ data = case what
26
+ when Symbol then {type: what}
27
+ when Hash then what.values.first.merge(type: what.keys.first)
28
+ else nil
29
+ end
30
+
31
+ EventMachine.connect Server.socket_path.to_s, Sender, data
32
+
33
+ end
34
+
35
+ def initialize(data)
36
+ send_data data.to_bson
37
+ end
38
+
39
+ def receive_data(data)
40
+ data = Hash.from_bson StringIO.new data
41
+
42
+ if data['result']
43
+ puts data['result']
44
+
45
+ elsif data['error']
46
+ error = data['error']
47
+ if error['class'] == Fatal.name
48
+ STDERR << error['message']
49
+ STDERR << "\n"
50
+ else
51
+ puts "#{error['class']}: #{error['message']}"
52
+ error['backtrace'].each { |x| puts ' ' << x }
53
+ end
54
+ end
55
+
56
+ if data['complete']
57
+ close_connection
58
+ EventMachine.stop_event_loop
59
+ end
60
+ end
61
+
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,3 @@
1
+ module Epi
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: epi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Neil E. Pearson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: eventmachine
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bson
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.3'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.3'
41
+ description: Manage your background processes
42
+ email:
43
+ - neil@helium.net.au
44
+ executables:
45
+ - epi
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - bin/epi
50
+ - lib/epi.rb
51
+ - lib/epi/cli.rb
52
+ - lib/epi/cli/command.rb
53
+ - lib/epi/cli/commands/config.rb
54
+ - lib/epi/cli/commands/job.rb
55
+ - lib/epi/cli/commands/server.rb
56
+ - lib/epi/cli/commands/status.rb
57
+ - lib/epi/configuration_file.rb
58
+ - lib/epi/core_ext.rb
59
+ - lib/epi/core_ext/inflector.rb
60
+ - lib/epi/data.rb
61
+ - lib/epi/exceptions.rb
62
+ - lib/epi/exceptions/base.rb
63
+ - lib/epi/exceptions/fatal.rb
64
+ - lib/epi/exceptions/invalid_configuration_file.rb
65
+ - lib/epi/exceptions/shutdown.rb
66
+ - lib/epi/job.rb
67
+ - lib/epi/job_description.rb
68
+ - lib/epi/jobs.rb
69
+ - lib/epi/launch.rb
70
+ - lib/epi/process_status.rb
71
+ - lib/epi/running_process.rb
72
+ - lib/epi/server.rb
73
+ - lib/epi/server/receiver.rb
74
+ - lib/epi/server/responder.rb
75
+ - lib/epi/server/responders/command.rb
76
+ - lib/epi/server/responders/config.rb
77
+ - lib/epi/server/responders/job.rb
78
+ - lib/epi/server/responders/shutdown.rb
79
+ - lib/epi/server/responders/status.rb
80
+ - lib/epi/server/sender.rb
81
+ - lib/epi/version.rb
82
+ homepage: https://github.com/hx/epi
83
+ licenses:
84
+ - Apache License, Version 2.0
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.2.2
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: Epinephrine
106
+ test_files: []