firefly_server 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: 503edf23c62eb6a1a859d9c4f6ef6482cc8db055
4
+ data.tar.gz: f64555becfe12ca611be5af632ae8bb0e9912b35
5
+ SHA512:
6
+ metadata.gz: 96582493832c8d2db7b2eee7e9c0569627d21fe55140d4768b9004f57573b8f6e7c87a37ae66ef98151742cc9b185a5e78d724e411e94456a5b0ec3e8d8097f4
7
+ data.tar.gz: 172b1dd44d161a5d593cda10049d124e8a14ab59b1b23fd4ca1d870250e7d6d51a3b8b4632e273e632fd4874a9982759606fcb4d9c1e4fd9b94f9c458a07f2af
data/.byebug_history ADDED
@@ -0,0 +1,23 @@
1
+ c
2
+ q
3
+ `echo 'blah' > /tmp/blah.txt`
4
+ file_watcher.listener.processing?
5
+ c
6
+ `echo 'blah' > /tmp/blah.txt`
7
+ file_watcher.listener.processing?
8
+ file_watcher.listener.state
9
+ file_watcher.listener
10
+ c
11
+ file_watcher.listener.processing?
12
+ file_watcher.listener
13
+ c
14
+ listener.processing?
15
+ listener.transition
16
+ listener.processing?
17
+ listener.paused?
18
+ listener.state
19
+ `echo 'blah' > /tmp/blah.txt`
20
+ listener.public_methods.sort - Object.instance_methods
21
+ listener.started?
22
+ listener.start
23
+ listener
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /tests/tmp/*
11
+ *.swp
12
+ *.gem
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.3
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at griffithchaffee@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in rails-firefly_server.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Griffith Chaffee
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # FireflyServer
2
+
3
+ Restarts a web server when watched files or directories are changed. Useful for rails applications the cache classes in development. The server runs in the foreground for easy logging and debugger output.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'firefly_server'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install firefly_server
20
+
21
+ ## Usage
22
+
23
+ Create script `bin/firefly_server`
24
+
25
+ ```ruby
26
+ #!/usr/bin/env ruby
27
+ require "bundler/setup"
28
+ require "firefly_server"
29
+
30
+ server = FireflyServer.new.configure do |config|
31
+ rails_root = File.expand_path("../..", __FILE__)
32
+ # file watcher
33
+ config.watch_paths = %w[ app lib config vendor db/schemas bin ].map do |rails_dir|
34
+ "#{rails_root}/#{rails_dir}"
35
+ end
36
+ config.ignore_paths = %w[ app/views app/emails ].map do |rails_dir|
37
+ "#{rails_root}/#{rails_dir}"
38
+ end
39
+ # server
40
+ config.start_server = "rails server -p 8080 -b 0.0.0.0"
41
+ config.stop_server = "pkill -INT -f 'puma'"
42
+ config.pid_file = "#{rails_root}/tmp/pids/server.pid"
43
+ end
44
+
45
+ server.start!
46
+ ```
47
+
48
+ Make script executable: `chmod 700 bin/firefly_server`
49
+
50
+ Start server: `./bin/firefly_server`
51
+
52
+ ## License
53
+
54
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "firefly_server"
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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -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 "firefly_server/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "firefly_server"
8
+ spec.version = FireflyServer::VERSION
9
+ spec.authors = ["Griffith Chaffee"]
10
+ spec.email = ["griffithchaffee@gmail.com"]
11
+
12
+ spec.summary = %q(Restarts a web server when watched files or directories are changed.)
13
+ spec.description = %q(Restarts a web server when watched files or directories are changed. Useful for rails applications the cache classes in development.)
14
+ spec.homepage = "https://github.com/griffithchaffee/firefly_server"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.15"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "minitest", "~> 5.0"
27
+ end
@@ -0,0 +1,70 @@
1
+ require "listen"
2
+ require "firefly_server/version"
3
+ require "firefly_server/configuration"
4
+ require "firefly_server/file_watcher"
5
+
6
+ class FireflyServer
7
+ attr_accessor(*%w[ configuration file_watcher ])
8
+
9
+ def initialize(params = {})
10
+ self.configuration = Configuration.new
11
+ self.file_watcher = FileWatcher.new(configuration)
12
+ end
13
+
14
+ def configure
15
+ yield(configuration)
16
+ self
17
+ end
18
+
19
+ def start!
20
+ configuration.validate!
21
+ # trap signals and exit
22
+ configuration.exit_signals.each do |signal|
23
+ Signal.trap(signal) do
24
+ puts "\rStopping Firefly Server"
25
+ exit 130
26
+ end
27
+ end
28
+ # file_watcher loop
29
+ file_watcher.watch! do |files|
30
+ puts "Ignored: #{files.ignored.join(", ")}" if files.ignored?
31
+ puts "Modified: #{files.modified.join(", ")}" if files.modified?
32
+ puts "Added: #{files.added.join(", ")}" if files.added?
33
+ puts "Removed: #{files.removed.join(", ")}" if files.removed?
34
+ # stop server
35
+ if files.modified? || files.added? || files.removed?
36
+ puts "Stopping Server: #{configuration.stop_server}"
37
+ %x(#{configuration.stop_server})
38
+ end
39
+ end
40
+ # server loop
41
+ restart_attempt = 0
42
+ loop do
43
+ # delete stale PID file
44
+ File.delete(configuration.pid_file) if File.file?(configuration.pid_file)
45
+ # start server
46
+ puts "Starting Server: #{configuration.start_server}"
47
+ # new fork will have nil initial pid
48
+ server_pid = fork
49
+ # new fork
50
+ if server_pid == nil
51
+ exec(configuration.start_server)
52
+ else
53
+ Process.wait(server_pid)
54
+ server_pid_status = $? # Process.wait sets $? to pid status
55
+ end
56
+ # normal restart
57
+ if server_pid_status.success?
58
+ restart_attempt = 0
59
+ # failed restart
60
+ else
61
+ restart_attempt += 1
62
+ # throttle restart attempts to prevent high CPU usage
63
+ if restart_attempt >= configuration.restart_attempt_throttle_threshold
64
+ puts "Throttling restart attempts by sleeping #{configuration.restart_attempt_throttle_sleep} seconds"
65
+ sleep(configuration.restart_attempt_throttle_sleep)
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,49 @@
1
+ class FireflyServer
2
+ class Configuration
3
+ attr_reader(*%w[
4
+ exit_signals
5
+ watch_paths
6
+ ignore_paths
7
+ file_change_callbacks
8
+ ])
9
+ attr_accessor(*%w[
10
+ start_server
11
+ stop_server
12
+ pid_file
13
+ restart_attempt_throttle_threshold
14
+ restart_attempt_throttle_sleep
15
+ ])
16
+
17
+ def initialize(params = {})
18
+ # defaults
19
+ self.restart_attempt_throttle_threshold = 3
20
+ self.restart_attempt_throttle_sleep = 3
21
+ self.exit_signals = %w[ SIGINT ]
22
+ # watcher defaults
23
+ self.watch_paths = []
24
+ self.ignore_paths = []
25
+ self.file_change_callbacks = []
26
+ # override defaults
27
+ params.each do |key, value|
28
+ send("#{key}=", value)
29
+ end
30
+ end
31
+
32
+ %w[ exit_signals watch_paths ignore_paths file_change_callbacks ].each do |accessor|
33
+ define_method("#{accessor}=") { |paths| instance_variable_set("@#{accessor}", Array(paths)) }
34
+ end
35
+
36
+ def validate!
37
+ # validate require options set
38
+ %w[ start_server stop_server pid_file ].each do |attribute|
39
+ if !send(attribute)
40
+ raise(ArgumentError, "#{attribute} option must be provided")
41
+ end
42
+ end
43
+ if watch_paths.empty?
44
+ raise(ArgumentError, "watch_paths option must be provided")
45
+ end
46
+ self
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,77 @@
1
+ class FireflyServer
2
+ class FileWatcher
3
+ attr_reader(*%w[ listener configuration ])
4
+
5
+ def initialize(configuration)
6
+ @configuration = configuration
7
+ @listener = nil
8
+ end
9
+
10
+ def on_change(&block)
11
+ configuration.file_change_callbacks << block if block
12
+ self
13
+ end
14
+
15
+ def watch!(&file_change_callback)
16
+ # prevent multiple listeners
17
+ listener.stop if listener
18
+ # always mute the Listen logger due to verbosity
19
+ if !Listen.logger
20
+ Listen.logger = Logger.new(STDOUT)
21
+ Listen.logger.level = Logger::WARN
22
+ end
23
+ @listener = Listen.to(*configuration.watch_paths) do |modified, added, removed|
24
+ ignored = []
25
+ [modified, added, removed].each do |paths|
26
+ # remove ignored paths
27
+ paths.reject! do |path|
28
+ configuration.ignore_paths.any? do |ignore_path|
29
+ if ignore_path.is_a?(String)
30
+ ignored << path if path.start_with?(ignore_path)
31
+ true
32
+ elsif ignore_path.is_a?(Regexp)
33
+ ignored << path if path =~ ignore_path
34
+ true
35
+ else
36
+ raise(
37
+ ArgumentError,
38
+ "unknown ignore path (expected string or regex): #{ignore_path.class} - #{ignore_path.inspect}"
39
+ )
40
+ end
41
+ end
42
+ end
43
+ end
44
+ # trigger change callbacks
45
+ callbacks = (configuration.file_change_callbacks + [file_change_callback]).compact
46
+ if !callbacks.empty?
47
+ change_event = ChangeEvent.new(ignored: ignored, modified: modified, added: added, removed: removed)
48
+ callbacks.each do |callback|
49
+ callback.call(change_event)
50
+ end
51
+ end
52
+ end
53
+ listener.start
54
+ self
55
+ end
56
+
57
+ class ChangeEvent
58
+ CATEGORIES = %w[ ignored modified added removed ]
59
+ attr_writer(*CATEGORIES)
60
+
61
+ def initialize(params)
62
+ params.each do |key, value|
63
+ send("#{key}=", value)
64
+ end
65
+ end
66
+
67
+ CATEGORIES.each do |accessor|
68
+ define_method("#{accessor}?") { send(accessor).any? }
69
+ define_method("#{accessor}") { Array(instance_variable_get("@#{accessor}")) }
70
+ end
71
+
72
+ def to_h
73
+ CATEGORIES.map { |category| [category.to_sym, send(category)] }.to_h
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,3 @@
1
+ class FireflyServer
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: firefly_server
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Griffith Chaffee
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-05-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: Restarts a web server when watched files or directories are changed.
56
+ Useful for rails applications the cache classes in development.
57
+ email:
58
+ - griffithchaffee@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".byebug_history"
64
+ - ".gitignore"
65
+ - ".travis.yml"
66
+ - CODE_OF_CONDUCT.md
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - firefly_server.gemspec
74
+ - lib/firefly_server.rb
75
+ - lib/firefly_server/configuration.rb
76
+ - lib/firefly_server/file_watcher.rb
77
+ - lib/firefly_server/version.rb
78
+ homepage: https://github.com/griffithchaffee/firefly_server
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.6.11
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Restarts a web server when watched files or directories are changed.
102
+ test_files: []