Skip to content

Commit

Permalink
Merge pull request #9 from bdieu178/master
Browse files Browse the repository at this point in the history
WIP Pull Request for Issue #7 #8 ( Bryan + Minja)
  • Loading branch information
Bad-Science authored Sep 29, 2018
2 parents 106aaaa + 727d8c9 commit 0808acc
Show file tree
Hide file tree
Showing 18 changed files with 171,136 additions and 15 deletions.
7 changes: 5 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
FROM ruby:2.5.1-alpine

RUN apk add --update ruby-dev build-base
RUN apk update && apk add bash

ENV INSTALL_PATH /usr/src/app/
RUN mkdir -p $INSTALL_PATH

WORKDIR $INSTALL_PATH

COPY Gemfile Gemfile.lock $INSTALL_PATH
COPY Gemfile $INSTALL_PATH
RUN bundle install

COPY . $INSTALL_PATH

CMD bundle exec karafka server
CMD ["/bin/sh","entrypoint.sh"]
#make an entrypoint.sh for executing iodine & karafka server

6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@ source 'https://rubygems.org'
ruby '2.5.1'
gem 'karafka'

# include the basic plezi framework and server
gem 'plezi', '~>0.15.0'
## Redis servers are used to allow websocket scaling.
## Plezi can be configured to automatically use Redis for easy scaling.
# gem 'redis'
gem 'iodine', '~>0.4.0' # may need to fix the version
24 changes: 18 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ GEM
minitest (~> 5.1)
tzinfo (~> 1.1)
concurrent-ruby (1.0.5)
delivery_boy (0.2.6)
delivery_boy (0.2.7)
king_konf (~> 0.2)
ruby-kafka (~> 0.5)
dry-configurable (0.7.0)
concurrent-ruby (~> 1.0)
dry-container (0.6.0)
concurrent-ruby (~> 1.0)
dry-configurable (~> 0.1, >= 0.1.3)
dry-core (0.4.6)
dry-core (0.4.7)
concurrent-ruby (~> 1.0)
dry-equalizer (0.2.1)
dry-events (0.1.0)
Expand All @@ -39,7 +39,7 @@ GEM
dry-equalizer (~> 0.2)
dry-inflector (~> 0.1, >= 0.1.2)
dry-logic (~> 0.4, >= 0.4.2)
dry-validation (0.12.0)
dry-validation (0.12.1)
concurrent-ruby (~> 1.0)
dry-configurable (~> 0.1, >= 0.1.3)
dry-core (~> 0.2, >= 0.2.1)
Expand All @@ -50,7 +50,9 @@ GEM
dry-inflector (~> 0.1)
i18n (1.0.1)
concurrent-ruby (~> 1.0)
karafka (1.2.4)
iodine (0.4.19)
rack (>= 1.0, < 3.0)
karafka (1.2.5)
activesupport (>= 4.0)
dry-configurable (~> 0.7)
dry-inflector (~> 0.1.1)
Expand All @@ -60,13 +62,18 @@ GEM
multi_json (>= 1.12)
rake (>= 11.3)
require_all (>= 1.4)
ruby-kafka (>= 0.5.3)
ruby-kafka (>= 0.6.4)
thor (~> 0.19)
waterdrop (~> 1.2)
king_konf (0.3.6)
minitest (5.11.3)
multi_json (1.13.1)
null-logger (0.1.5)
plezi (0.15.1)
bundler (~> 1.14)
iodine (>= 0.4, < 0.5)
rack (>= 2.0.0)
rack (2.0.5)
rake (12.3.1)
require_all (2.0.0)
rouge (2.2.1)
Expand All @@ -86,7 +93,12 @@ PLATFORMS
ruby

DEPENDENCIES
iodine (~> 0.4.0)
karafka
plezi (~> 0.15.0)

RUBY VERSION
ruby 2.5.1p57

BUNDLED WITH
1.16.2
1.16.3
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
YACS Event Stream Websocket API
---
Implemented using Kafka & Plezi
53 changes: 51 additions & 2 deletions app.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,52 @@
require 'karafka'
require 'plezi'
require 'iodine'
require_relative 'app/controllers/eventstream'

puts 'Hello world'
## Set environment, working directory, load gems and create logs
#ENV['ENV'] ||= ENV['RACK_ENV'] ||= ENV['RAILS_ENV'] # production ENV will render SASS as compressed.
## Using pathname extentions for setting public folder
require 'pathname'
## Set up root object, it might be used by the environment and\or the plezi extension gems.
Root ||= Pathname.new(File.dirname(__FILE__)).expand_path
## If this app is independant, use bundler to load gems (including the plezi gem).
## otherwise, use the original app's Gemfile and Plezi will automatically switch to Rack mode.
require 'bundler'
Bundler.require(:default, ENV['ENV'].to_s.to_sym)
# Load all the code from a subfolder called 'app'
Dir[File.join '{controllers}', '**', '*.rb'].each { |file| load File.expand_path(file) }
# Load all the code from a subfolder called 'lib'
Dir[File.join '{lib}', '**', '*.rb'].each { |file| load File.expand_path(file) }


#Setting the port
#Iodine::DEFAULT_HTTP_ARGS[:port] = 4860

## Logging
#::Iodine::DEFAULT_HTTP_ARGS[:log] = 1 if ::Iodine::DEFAULT_HTTP_ARGS[:log].nil?
#Iodine::DEFAULT_HTTP_ARGS[:public] ||= './public'


# # Optional Scaling (across processes or machines):
ENV['PL_REDIS_URL'] ||= ENV['REDIS_URL'] ||
ENV['REDISCLOUD_URL'] ||
ENV['REDISTOGO_URL'] ||
nil # "redis://:[email protected]:6389/0"
# # redis channel name should be changed IF using the same Plezi code within
# # more then one application (i.e., using both Rails and Plezi together).
# Plezi.app_name = 'yacs_websocket_a027e2d668a7dc21c59fe56f018432ea'

# Map the views folder to the template root (for the {#render} function).
#Plezi.templates = Root.join('views').to_s

# load routes.
load Root.join('routes.rb').to_s

App = Proc.new do |env|
if(env['rack.upgrade?'.freeze] == :websocket)
env['rack.upgrade'.freeze] = EventStream.new
[0, {}, []]
else
[200, {"Content-Length" => "12", "Content-Type" => "text/plain"}, ["Hello World!"]]
end

end
13 changes: 13 additions & 0 deletions app/consumers/courses_consumer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'karafka'
require 'iodine'
require_relative 'application_consumer'

class CourseConsumer < ApplicationConsumer
def consume
unless params.nil?
@notifications = params.to_s
::Iodine::publish channel: "notifications", message: "#{@notifications}"
puts "SectionsConsumer sent message to websocket"
end
end
end
11 changes: 8 additions & 3 deletions app/consumers/sections_consumer.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
require 'karafka'
require 'iodine'
require_relative 'application_consumer'

class SectionConsumer < ApplicationConsumer
def consume
puts params #print out the single message received
end
def consume
unless params.nil?
@notifications = params.to_s
::Iodine::publish channel: "notifications", message: "#{@notifications}"
puts "SectionsConsumer sent message to websocket"
end
end
end
28 changes: 28 additions & 0 deletions app/controllers/eventstream.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'plezi'
require 'iodine'

class EventStream
#@auto_dispatch = true
def index
render 'client'
end

def on_open
puts "WS connection open"
::Iodine::subscribe channel:"notifications" do
puts "I'm in!"
end
end

#TODO
def on_message data
#::Iodine::write "#{@data}"
#puts data
end

def on_close
::Iodine::unsubscribe("notifications")
end
end
#test location: ws://localhost:3000/notifications/

7 changes: 7 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Default Rack interface
# encoding: UTF-8
# load the application
load ::File.expand_path(File.join('..', 'app.rb'), __FILE__)

run App
run Plezi.app
2 changes: 2 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
nohup bundler exec iodine -p 4860 -www ./public -v > iodine.out 2>&1 &
bundle exec karafka server
20 changes: 20 additions & 0 deletions iodine.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
* Listening on port 4860

Server is running 2 workers X 2 threads, press ^C to stop
Running Plezi version: 0.15.1

Starting up Iodine HTTP Server on port 4860:
* Ruby v.2.5.1
* Iodine v.0.4.19
* 1048576 max concurrent connections / open files
* Serving static files from ./public

* 6 is running.
* 14 is running.
172.18.0.1 - - [Tue, 6 Aug 2018 03:14:14 GMT] "GET /notifications HTTP/1.1" 101 -- 300ms
Iodine caught an unprotected exception - LocalJumpError: no block given
/usr/src/app/app/controllers/eventstream.rb:12:in `subscribe'
/usr/src/app/app/controllers/eventstream.rb:12:in `on_open'
Iodine caught an unprotected exception - TypeError: wrong argument type String (expected Hash)
/usr/src/app/app/controllers/eventstream.rb:26:in `publish'
/usr/src/app/app/controllers/eventstream.rb:26:in `on_close'
11 changes: 9 additions & 2 deletions karafka.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
require 'bundler/setup'
Bundler.require(:default, ENV['KARAFKA_ENV'])
Karafka::Loader.load(Karafka::App.root)
require_relative 'app/consumers/sections_consumer.rb'
require_relative 'app/consumers/sections_consumer'
require_relative 'app/consumers/courses_consumer'

# Ruby on Rails setup
# Remove whole non-Rails setup that is above and uncomment the 4 lines below
Expand All @@ -27,7 +28,6 @@ class KarafkaApp < Karafka::App
# config.logger = Rails.logger
end


after_init do |config|
end

Expand All @@ -38,6 +38,13 @@ class KarafkaApp < Karafka::App
consumer SectionConsumer #Single message from section_change
end
end
topic :course_change do
consumer CourseConsumer
end
end
end




KarafkaApp.boot!
Loading

0 comments on commit 0808acc

Please sign in to comment.