Sha256: ecf412566ba56f34b498608e0670b80fa81b5b2271f04f67b1a397f6b89c16ab

Contents?: true

Size: 1.92 KB

Versions: 3

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

require "fileutils"

require "pakyow/support/silenceable"
Pakyow::Support::Silenceable.silence_warnings do
  require "filewatcher"
end

require "pakyow/support/extension"

module Pakyow
  class Application
    module Behavior
      # Handles triggering restarts in the parent process.
      #
      module Restarting
        extend Support::Extension

        apply_extension do
          configurable :process do
            setting :watched_paths, []
            setting :excluded_paths, []
            setting :restartable, false

            defaults :development do
              setting :restartable, true
            end

            defaults :prototype do
              setting :restartable, true
            end
          end

          after "initialize" do
            setup_for_restarting
          end

          # Setting up for restarting even after the app fails to initialize lets
          # the developer fix the problem and let the server restart on its own.
          #
          after "rescue" do
            setup_for_restarting
          end
        end

        private

        def setup_for_restarting
          if config.process.restartable
            config.process.watched_paths << File.join(config.src, "**/*.rb")
            config.process.watched_paths << File.join(config.lib, "**/*.rb")

            # FIXME: this doesn't need to be hardcoded, but instead determined
            # from the source location when registered with the environment
            config.process.watched_paths << "./config/application.rb"

            Thread.new do
              Filewatcher.new(
                config.process.watched_paths,
                exclude: config.process.excluded_paths
              ).watch do |_path, _event|
                FileUtils.mkdir_p "./tmp"
                FileUtils.touch "./tmp/restart.txt"
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pakyow-core-1.0.2 lib/pakyow/application/behavior/restarting.rb
pakyow-core-1.0.1 lib/pakyow/application/behavior/restarting.rb
pakyow-core-1.0.0 lib/pakyow/application/behavior/restarting.rb