lita-external 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3900706ff8b10e7d86a74899d25e36df607b7d6c
4
+ data.tar.gz: 626e997b2b81504a11cc74aaf019369049f70910
5
+ SHA512:
6
+ metadata.gz: 5de778f172eed7431062c816cd9b7fc8ef32698a47d0d90422589fd7d03cd6397622a77db040189d3b9daa211c228584403b34a49e70af0d784707319730b6d1
7
+ data.tar.gz: c3f170c2f6b0c307fe9bbad00bc37bb16de68a9202b8d2da6bf05aad3ce141949afcb120f4a37cc39772cef80786e46f02ce8c4a7a1ddd30f51df5ecebaaf0c5
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in lita-external.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Shopify
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # Lita::External
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/lita/external`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'lita-external'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install lita-external
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/lita-external.
36
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "lita/external"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(File.realpath(__FILE__)) + '/../lib'))
4
+
5
+ require 'lita/external'
6
+
7
+ exit Lita::External::CLI.run(ARGV) || 0
@@ -0,0 +1 @@
1
+ require 'lita/external'
@@ -0,0 +1,74 @@
1
+ module Lita
2
+ module Adapters
3
+ class External < Adapter
4
+ config :min_threads
5
+ config :max_threads
6
+ def initialize(*)
7
+ super
8
+ @thread_pool = ::Puma::ThreadPool.new((config.min_threads || 8).to_i, (config.max_threads || 16).to_i) do |message|
9
+ log.debug("processing inbound message from: #{message.user.mention_name}")
10
+ robot.receive(message)
11
+ end
12
+ end
13
+
14
+ def real_adapter
15
+ raise NotImplementedError, 'You need to instanciate the real adapter'
16
+ end
17
+
18
+ # Starts the connection.
19
+ def run
20
+ return if @running || @stopping
21
+
22
+ robot.trigger(:worker_loaded)
23
+
24
+ @running = true
25
+ @stopping = false
26
+ log.info("Listening to redis queue: `messages:inbound`")
27
+ robot.trigger(:connected)
28
+ until @stopping
29
+ begin
30
+ if result = Lita::External.blocking_redis.blpop('messages:inbound', timeout: 1)
31
+ handle_inbound_message(result.last)
32
+ end
33
+ rescue => error
34
+ Lita.logger.error("Inbound message failed: #{error.class}: #{error.message}")
35
+ Lita.config.robot.error_handler(error)
36
+ end
37
+ end
38
+ end
39
+
40
+ def send_messages(target, strings)
41
+ rpc(:send_messages, target, strings)
42
+ end
43
+
44
+ def set_topic(target, topic)
45
+ rpc(:set_topic, target, topic)
46
+ end
47
+
48
+ def shut_down
49
+ return unless @running
50
+
51
+ log.info("Shutting down")
52
+ @stopping = true
53
+ @thread_pool.shutdown
54
+ robot.trigger(:disconnected)
55
+ end
56
+
57
+ private
58
+
59
+ def handle_inbound_message(payload)
60
+ message = ::Lita::External.load_message(payload, robot: robot)
61
+ log.debug("enqueuing inbound message from: #{message.user.mention_name}")
62
+ @thread_pool << message
63
+ end
64
+
65
+ def rpc(method, *args)
66
+ Lita.logger.info("Putting outbound message into the queue: #{method}(#{args.map(&:inspect).join(', ')})")
67
+ Lita.redis.rpush('messages:outbound', Marshal.dump([method, args]))
68
+ end
69
+ end
70
+
71
+ Lita.register_adapter(:external, External)
72
+ end
73
+ end
74
+
@@ -0,0 +1,31 @@
1
+ require 'lita'
2
+
3
+ require 'lita/external/version'
4
+ require 'lita/external/robot'
5
+ require 'lita/external/cli'
6
+ require 'lita/adapters/external'
7
+
8
+ module Lita
9
+ module External
10
+ extend self
11
+
12
+ # It's important to use another redis connection than Lita's one, because we are using long blocking calls
13
+ # and redis use a mutex around these
14
+ def blocking_redis
15
+ @blocking_redis ||= Redis::Namespace.new(Lita::REDIS_NAMESPACE, redis: Redis.new(Lita.config.redis))
16
+ end
17
+
18
+ def dump_message(message)
19
+ # The robot instance contains Proc and other non serializable attributes
20
+ # Also, it's a singleton and should be set again in the receiving process anyway
21
+ message.instance_variable_set(:@robot, nil)
22
+ Marshal.dump(message)
23
+ end
24
+
25
+ def load_message(payload, robot: )
26
+ message = Marshal.load(payload)
27
+ message.instance_variable_set(:@robot, robot)
28
+ message
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,62 @@
1
+ require 'optparse'
2
+
3
+ module Lita
4
+ module External
5
+ module CLI
6
+ extend self
7
+
8
+ def parse_options(argv)
9
+ config = {config_file: 'lita_config.rb'}
10
+ parser = OptionParser.new do |opts|
11
+ opts.banner = "Standalone daemon to run a lita adapter"
12
+ opts.separator ""
13
+ opts.separator "Usage: messenger [options]"
14
+ opts.separator ""
15
+ opts.separator "Main options:"
16
+
17
+ opts.on("-c", "--config FILE", "Lita config file to load") do |file|
18
+ config[:config_file] = File.expand_path(file)
19
+ end
20
+
21
+ opts.on("-a", "--adapter ADAPTER", "Lita adapter to use") do |adapter|
22
+ config[:adapter] = adapter.to_sym
23
+ end
24
+ end
25
+
26
+ parser.parse(argv)
27
+
28
+ config
29
+ end
30
+
31
+ def load_lita_config(options)
32
+ require options[:config_file]
33
+ end
34
+
35
+ def set_adapter(options)
36
+ if options[:adapter]
37
+ Lita.config.robot.adapter = options[:adapter]
38
+ end
39
+
40
+ if Lita.config.robot.adapter == :external || Lita.config.robot.adapter == 'external'
41
+ STDERR.puts "You must specify the adapter to use"
42
+ exit 1
43
+ end
44
+
45
+ begin
46
+ require "lita-#{Lita.config.robot.adapter}"
47
+ rescue LoadError
48
+ end
49
+ end
50
+
51
+ def run(argv)
52
+ options = parse_options(argv)
53
+ load_lita_config(options)
54
+ set_adapter(options)
55
+ robot = Lita::External::Robot.new
56
+ p robot.send(:adapter)
57
+ robot.run
58
+ 0
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,48 @@
1
+ module Lita
2
+ module External
3
+ class Robot < ::Lita::Robot
4
+ def receive(message)
5
+ Lita.logger.debug("Put inbound message from #{message.user.mention_name} into the queue")
6
+ Lita.redis.rpush('messages:inbound', External.dump_message(message))
7
+ end
8
+
9
+ def run
10
+ @stopping = false
11
+
12
+ trigger(:master_loaded)
13
+
14
+ watch_outbound_queue
15
+ super
16
+ end
17
+
18
+ def shut_down
19
+ @stopping = true
20
+ super
21
+ end
22
+
23
+ def watch_outbound_queue
24
+ Thread.start do
25
+ Lita.logger.info("Watching outbound queue")
26
+ until @stopping
27
+ begin
28
+ if command = External.blocking_redis.blpop('messages:outbound', timeout: 1)
29
+ process_outbound_command(command.last)
30
+ end
31
+ rescue => error
32
+ Lita.logger.error("Outbound message failed: #{error.class}: #{error.message}")
33
+ if Lita.config.robot.error_handler
34
+ Lita.config.robot.error_handler.call(error)
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ def process_outbound_command(payload)
42
+ command, args = Marshal.load(payload)
43
+ Lita.logger.debug("Triggering #{command}")
44
+ adapter.public_send(command, *args)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,5 @@
1
+ module Lita
2
+ module External
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'lita/external/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "lita-external"
8
+ spec.version = Lita::External::VERSION
9
+ spec.authors = ["Jean Boussier"]
10
+ spec.email = ["jean.boussier@shopify.com"]
11
+ spec.license = "MIT"
12
+
13
+ spec.summary = %q{Meta Lita adapter that use a redis queue}
14
+ spec.description = %q{Meta adapter that allow Lita to spawn multiple processes and load balance the work}
15
+ spec.homepage = "https://github.com/shopify/lita-external"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency 'lita', '~> 4.6'
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.10"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec"
27
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lita-external
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jean Boussier
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: lita
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Meta adapter that allow Lita to spawn multiple processes and load balance
70
+ the work
71
+ email:
72
+ - jean.boussier@shopify.com
73
+ executables:
74
+ - lita-external-messenger
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - exe/lita-external-messenger
87
+ - lib/lita-external.rb
88
+ - lib/lita/adapters/external.rb
89
+ - lib/lita/external.rb
90
+ - lib/lita/external/cli.rb
91
+ - lib/lita/external/robot.rb
92
+ - lib/lita/external/version.rb
93
+ - lita-external.gemspec
94
+ homepage: https://github.com/shopify/lita-external
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.4.6
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Meta Lita adapter that use a redis queue
118
+ test_files: []