slnky 0.9.5 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c51d287908f0a2b0cb4091a29bc8a814eee19342
4
- data.tar.gz: 93ae42c86e7e592ef2d5aa6680481ecd26d6cfac
3
+ metadata.gz: d9205994e57ed75ad10a0fd88f243ef7e0fd95ed
4
+ data.tar.gz: d6314951e4589c72fcbbba5955d20cb34bf144cd
5
5
  SHA512:
6
- metadata.gz: 8239559a093106ade370ad12a47beac436ed0e1df4429834e959a5519a2796627ecbf3fd7d934adc632c070a2de80096591fcf9fad4d82b3f95fdd4b1ea71da5
7
- data.tar.gz: bb8cd8812ea59dc29edc272a3890b4e52abdfbb968ab91d58720551ebb3c5064b24255f2a9ed848cdac31d3a9a3206ae8756e4e1b4121e21b60b9169f2fd7537
6
+ metadata.gz: d531aecffb5b4c89d24a3c93765da9311cd3b361e519d6c4ef96006e3f0a1e41a0fdffeb75285a511a2ab41eabadb3fa62f3ce8fff0c90c05fb146bb94f52286
7
+ data.tar.gz: 0925cc60acbb0c0c15202f55a2e5df4df099674686f646335356493341a4dd72133a1fadf4c0b3467f3ffb0006ce690f482aaec2962826314ddb2f49e40d0f8c
@@ -1,6 +1,7 @@
1
1
  module Slnky
2
2
  module CLI
3
3
  class Command < Base
4
+ # option %w{-s --server}, '[SERVER]', 'set server url', environment_variable: 'SLNKY_SERVER'
4
5
  option %w{-n --dry-run}, :flag, "just output the event, don't send"
5
6
  option %w{-t --timeout}, '[TIMEOUT]', "time to wait for response in seconds", default: 10 do |t|
6
7
  Integer(t)
@@ -15,13 +16,13 @@ module Slnky
15
16
 
16
17
  def execute
17
18
  @name = service
19
+ Slnky::Config.configure(@name, environment: environment)
18
20
  data = {
19
21
  name: "slnky.#{service}.command",
20
22
  command: service == 'help' ? nil : command,
21
23
  args: args,
22
24
  response: "command-#{$$}",
23
25
  }
24
- Slnky::Config.configure('cli')
25
26
  msg = Slnky::Message.new(data)
26
27
  puts JSON.pretty_generate(msg.to_h) if dry_run?
27
28
  amqp(msg) unless dry_run?
@@ -6,7 +6,6 @@ module Slnky
6
6
  n.gsub(/^slnky-/, '')
7
7
  end
8
8
  # option %w{-f --force}, :flag, "force overwrite of files"
9
- option %w{-e --environment}, '[ENV]', 'the environment to run under', default: 'development', environment_variable: 'SLNKY_ENV'
10
9
  def execute
11
10
  lib = File.expand_path("#{Dir.pwd}/lib", __FILE__)
12
11
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
data/lib/slnky/cli.rb CHANGED
@@ -14,13 +14,15 @@ module Slnky
14
14
  exit(0)
15
15
  end
16
16
 
17
- option %w{-c --config}, '[CONFIG]', 'set config directory location', default: '~/.slnky', environment_variable: 'SLNKY_CONFIG' do |c|
18
- p = File.expand_path("#{c}/config.yaml")
19
- Slnky.load_config(p)
20
- c
17
+ option %w{-c --config}, '[CONFIG]', 'set config directory location', default: '~/.slnky/config.yaml', environment_variable: 'SLNKY_CONFIG' do |c|
18
+ ENV['SLNKY_CONFIG'] = c
19
+ end
20
+ option %w{-s --server}, '[URL]', 'set server url', environment_variable: 'SLNKY_URL' do |u|
21
+ ENV['SLNKY_URL'] = u
22
+ end
23
+ option %w{-e --environment}, '[ENV]', 'the environment to run under', default: 'development', environment_variable: 'SLNKY_ENV' do |e|
24
+ ENV['SLNKY_ENV'] = e
21
25
  end
22
-
23
- option %w{-s --server}, '[URL]', 'set server url', environment_variable: 'SLNKY_URL'
24
26
  end
25
27
 
26
28
  class Main < Base
@@ -32,7 +34,9 @@ module Slnky
32
34
  FileUtils.mkdir_p(dir)
33
35
  defaults = {
34
36
  slnky: {
35
- url: server
37
+ development: {
38
+ url: server
39
+ }
36
40
  }
37
41
  }.deep_stringify_keys
38
42
  File.open("#{dir}/config.yaml", "w+") { |f| f.write(defaults.to_yaml) }
@@ -0,0 +1,23 @@
1
+ module Slnky
2
+ module Client
3
+ class Base
4
+ def initialize
5
+
6
+ end
7
+
8
+ protected
9
+
10
+ def config
11
+ Slnky.config
12
+ end
13
+
14
+ def log
15
+ Slnky.log
16
+ end
17
+ end
18
+
19
+ class Mock < Base
20
+
21
+ end
22
+ end
23
+ end
@@ -4,8 +4,9 @@ module Slnky
4
4
  attr_reader :log
5
5
 
6
6
  def initialize(route, service)
7
+ Slnky.config.service = service
7
8
  @route = route
8
- @service = Slnky::System.pid(service)
9
+ @service = Slnky::System.pid
9
10
  @started = false
10
11
  @log = []
11
12
  end
data/lib/slnky/config.rb CHANGED
@@ -16,16 +16,16 @@ module Slnky
16
16
  class Config < Data
17
17
  class << self
18
18
  def configure(name, config={})
19
+ config.deep_stringify_keys!
19
20
  @name = name
20
- @config ||= begin
21
- config['service'] = name
22
- config['environment'] ||= 'development'
23
- file = ENV['SLNKY_CONFIG']||"~/.slnky/config.yaml"
24
- config.merge!(config_file(file))
25
- server = ENV['SLNKY_URL']||config['url']
26
- config.merge!(config_server(server))
27
- self.new(config)
28
- end
21
+ @environment = ENV['SLNKY_ENV'] || config['environment'] || 'development'
22
+ config['service'] = name
23
+ config['environment'] = @environment
24
+ file = ENV['SLNKY_CONFIG']||"~/.slnky/config.yaml"
25
+ config.merge!(config_file(file))
26
+ server = ENV['SLNKY_URL']||config['url']
27
+ config.merge!(config_server(server))
28
+ @config = self.new(config)
29
29
  end
30
30
 
31
31
  # def load_file(file)
@@ -52,8 +52,10 @@ module Slnky
52
52
  return {} unless File.exists?(path)
53
53
  template = Tilt::ERBTemplate.new(path)
54
54
  output = template.render(self, {})
55
- d = YAML.load(output)
56
- d['slnky'] ? d['slnky'] : d
55
+ cfg = YAML.load(output)
56
+ cfg = cfg['slnky'] || cfg
57
+ cfg = cfg[@environment] || cfg
58
+ cfg
57
59
  rescue => e
58
60
  puts "failed to load file #{file}: #{e.message}"
59
61
  {}
data/lib/slnky/log.rb CHANGED
@@ -22,7 +22,7 @@ module Slnky
22
22
  @response = false
23
23
  case @config.environment
24
24
  when 'production'
25
- @local = false
25
+ @local = Slnky::Log::Local.new
26
26
  @service = Slnky::Log::Service.new
27
27
  when 'test'
28
28
  @local = false
@@ -35,6 +35,10 @@ module Slnky
35
35
 
36
36
  [:debug, :info, :warn, :error].each do |l|
37
37
  define_method(l) do |message|
38
+ # bl = @local ? 'x' : 'o'
39
+ # br = @remote ? 'x' : 'o'
40
+ # bs = @response ? 'x' : 'o'
41
+ # puts "#{l}:#{bl}#{br}#{bs} #{message}"
38
42
  log(l, message)
39
43
  end
40
44
  end
@@ -69,6 +73,9 @@ module Slnky
69
73
  def initialize
70
74
  super
71
75
  @logger = Logger.new(STDOUT)
76
+ @logger.formatter = proc do |severity, datetime, progname, msg|
77
+ "%-5s %s: %s: %s\n" % [severity, datetime, Slnky::System.pid, msg]
78
+ end
72
79
  end
73
80
 
74
81
  protected
@@ -85,28 +92,31 @@ module Slnky
85
92
 
86
93
  def initialize
87
94
  super
88
- @service = Slnky::System.pid('unknown')
95
+ @service = Slnky::System.pid
89
96
  @hostname = Slnky::System.hostname
90
97
  @ipaddress = Slnky::System.ipaddress
91
- @transport = Slnky::Transport.instance
92
- @exchange = nil
93
- if @transport
94
- @exchange = @transport.exchanges['logs']
95
- end
96
98
  end
97
99
 
98
100
  protected
99
101
 
100
102
  def log(level, message)
101
- return unless @exchange
103
+ return unless exchange
102
104
  data = {
103
- service: "#{@name}-#{$$}",
105
+ service: @service,
104
106
  level: level,
105
107
  hostname: @hostname,
106
108
  ipaddress: @ipaddress,
107
109
  message: message
108
110
  }
109
- @exchange.publish(msg(data))
111
+ exchange.publish(msg(data))
112
+ end
113
+
114
+ def transport
115
+ @transport ||= Slnky::Transport.instance
116
+ end
117
+
118
+ def exchange
119
+ @exchange ||= transport.exchanges['logs']
110
120
  end
111
121
 
112
122
  def msg(data)
data/lib/slnky/service.rb CHANGED
@@ -13,13 +13,12 @@ module Slnky
13
13
 
14
14
  def initialize
15
15
  config.service = name
16
- @environment = config.environment
17
16
  @server_down = false
18
17
  end
19
18
 
20
19
  def start
21
20
  transport.start!(self) do |_|
22
- log.info "running"
21
+ log.info "#{config.service} running #{config.environment}"
23
22
  run
24
23
 
25
24
  subscriber.add "slnky.#{name}.command", :handle_command
data/lib/slnky/system.rb CHANGED
@@ -3,7 +3,8 @@ require 'socket'
3
3
  module Slnky
4
4
  class System
5
5
  class << self
6
- def pid(service)
6
+ def pid
7
+ service = Slnky.config.service || 'unknown'
7
8
  "#{ipaddress}/#{service}-#{$$}"
8
9
  end
9
10
 
@@ -1,11 +1,12 @@
1
1
  module Slnky
2
2
  module <%= cap %>
3
- class Client
4
- def initialize(config)
5
-
3
+ class Client < Slnky::Client::Base
4
+ def initialize
5
+ # you can load any configurations from the server here
6
+ # @param = config.<%= name %>.param
6
7
  end
7
8
 
8
-
9
+ # implement communications with supporting services
9
10
  end
10
11
  end
11
12
  end
@@ -6,9 +6,24 @@ module Slnky
6
6
  @client ||= Slnky::<%= cap %>::Client.new(config)
7
7
  end
8
8
 
9
- def echo(request, response)
10
- args = request.arguments||[]
11
- response.output = args.join(" ")
9
+ # use docopt to define arguments and options
10
+ command :echo, 'respond with the given arguments', <<-USAGE.strip_heredoc
11
+ Usage: echo [options] ARGS...
12
+
13
+ -h --help print help.
14
+ -x --times=TIMES print x times [default: 1].
15
+ USAGE
16
+
17
+ # handler methods receive request, response, and options objects
18
+ def handle_echo(request, response, opts)
19
+ # parameters (non-option arguments) are available as accessors
20
+ args = opts.args
21
+ # as are the options themselves (by their 'long' name)
22
+ 1.upto(opts.times.to_i) do |i|
23
+ # just use the log object to respond, it will automatically send it
24
+ # to the correct channel.
25
+ log.info args.join(" ")
26
+ end
12
27
  end
13
28
  end
14
29
  end
@@ -1,11 +1,13 @@
1
1
  module Slnky
2
2
  module <%= cap %>
3
- class Mock
4
- def initialize(config)
3
+ class Mock < Slnky::<%= cap %>::Client
4
+ # unless there's something special you need to do in the initializer
5
+ # use the one provided by the actual client object
6
+ # def initialize
7
+ #
8
+ # end
5
9
 
6
- end
7
-
8
-
10
+ # override methods of the client here to mock them for testing
9
11
  end
10
12
  end
11
13
  end
@@ -6,12 +6,12 @@ module Slnky
6
6
  @client ||= Slnky::<%= cap %>::Client.new(config)
7
7
  end
8
8
 
9
- subscribe 'slnky.service.test', :handler
9
+ subscribe 'slnky.service.test', :handle_test
10
10
  # you can also subscribe to heirarchies, this gets
11
11
  # all events under something.happened
12
12
  # subscribe 'something.happened.*', :other_handler
13
13
 
14
- def handler(name, data)
14
+ def handle_test(name, data)
15
15
  name == 'slnky.service.test' && data.hello == 'world!'
16
16
  end
17
17
  end
@@ -12,7 +12,9 @@ describe Slnky::<%= cap %>::Command do
12
12
  let(:help_response) { slnky_response('test-route', 'spec') }
13
13
 
14
14
  it 'handles echo' do
15
+ # make sure the command handler does not raise an error
15
16
  expect { subject.handle(echo.name, echo.payload, echo_response) }.to_not raise_error
17
+ # validate that the correct output is available in the response object
16
18
  expect(echo_response.log).to include("test echo")
17
19
  end
18
20
 
@@ -9,6 +9,7 @@ describe Slnky::<%= cap %>::Service do
9
9
  let(:test_event) { event_load('test')}
10
10
 
11
11
  it 'handles event' do
12
- expect(subject.handler(test_event.name, test_event.payload)).to eq(true)
12
+ # test that the handler method receives and responds correctly
13
+ expect(subject.handle_test(test_event.name, test_event.payload)).to eq(true)
13
14
  end
14
15
  end
@@ -3,5 +3,5 @@
3
3
  # this file is processed through ERB, you can inject
4
4
  # values into the config from the environment, by specifying them
5
5
  # in the .env file
6
- environment: development # should specify this
6
+ environment: test # should specify this
7
7
  service: <%= name %> # just an example
data/lib/slnky/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Slnky
2
- VERSION = "0.9.5"
2
+ VERSION = "0.10.0"
3
3
  end
data/lib/slnky.rb CHANGED
@@ -9,8 +9,9 @@ require 'slnky/config'
9
9
  require 'slnky/log'
10
10
  require 'slnky/system'
11
11
  require 'slnky/message'
12
- require 'slnky/service'
13
12
  require 'slnky/transport'
13
+ require 'slnky/service'
14
+ require 'slnky/client'
14
15
  require 'slnky/command'
15
16
 
16
17
  module Slnky
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slnky
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.5
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shawn Catanzarite
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-13 00:00:00.000000000 Z
11
+ date: 2016-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -207,6 +207,7 @@ files:
207
207
  - lib/slnky/cli/generate.rb
208
208
  - lib/slnky/cli/notify.rb
209
209
  - lib/slnky/cli/service.rb
210
+ - lib/slnky/client.rb
210
211
  - lib/slnky/command.rb
211
212
  - lib/slnky/command/request.rb
212
213
  - lib/slnky/command/response.rb