Skip to content

Commit

Permalink
Add start and stop callbacks (#1913)
Browse files Browse the repository at this point in the history
This PR adds lifecycle events to Sinatra. Related issue: #1184

Co-authored-by: Patrik Ragnarsson <patrik@starkast.net>
  • Loading branch information
jevin and dentarg authored Jul 11, 2023
1 parent 6d8f180 commit 3fe6297
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 3 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ pick up if available.
- [Configuration](#configuration)
- [Configuring attack protection](#configuring-attack-protection)
- [Available Settings](#available-settings)
- [Lifecycle Events](#lifecycle-events)
- [Environments](#environments)
- [Error Handling](#error-handling)
- [Not Found](#not-found)
Expand Down Expand Up @@ -2223,6 +2224,24 @@ set :protection, :session => true
</dd>
</dl>

## Lifecycle Events

There are 2 lifecycle events currently exposed by Sinatra. One when the server starts and one when it stops.

They can be used like this:

```ruby
on_start do
puts "===== Booting up ====="
end

on_stop do
puts "===== Shutting down ====="
end
```

Note that these callbacks only work when using Sinatra to start the web server.

## Environments

There are three predefined `environments`: `"development"`,
Expand Down
20 changes: 20 additions & 0 deletions examples/lifecycle_events.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env ruby -I ../lib -I lib
# frozen_string_literal: true

require 'sinatra'

get('/') do
'This shows how lifecycle events work'
end

on_start do
puts "=============="
puts " Booting up"
puts "=============="
end

on_stop do
puts "================="
puts " Shutting down"
puts "================="
end
16 changes: 13 additions & 3 deletions lib/sinatra/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,7 @@ class << self
%r{zeitwerk/kernel\.rb} # Zeitwerk kernel#require decorator
].freeze

attr_reader :routes, :filters, :templates, :errors
attr_reader :routes, :filters, :templates, :errors, :on_start_callback, :on_stop_callback

def callers_to_ignore
CALLERS_TO_IGNORE
Expand Down Expand Up @@ -1470,6 +1470,14 @@ def add_filter(type, path = /.*/, **options, &block)
filters[type] << compile!(type, path, block, **options)
end

def on_start(&on_start_callback)
@on_start_callback = on_start_callback
end

def on_stop(&on_stop_callback)
@on_stop_callback = on_stop_callback
end

# Add a route condition. The route is considered non-matching when the
# block returns false.
def condition(name = "#{caller.first[/`.*'/]} condition", &block)
Expand Down Expand Up @@ -1559,6 +1567,8 @@ def quit!
warn '== Sinatra has ended his set (crowd applauds)' unless suppress_messages?
set :running_server, nil
set :handler_name, nil

on_stop_callback.call unless on_stop_callback.nil?
end

alias stop! quit!
Expand Down Expand Up @@ -1646,7 +1656,7 @@ def start_server(handler, server_settings, handler_name)
set :running_server, server
set :handler_name, handler_name
server.threaded = settings.threaded if server.respond_to? :threaded=

on_start_callback.call unless on_start_callback.nil?
yield server if block_given?
end
end
Expand Down Expand Up @@ -2037,7 +2047,7 @@ def self.delegate(*methods)
delegate :get, :patch, :put, :post, :delete, :head, :options, :link, :unlink,
:template, :layout, :before, :after, :error, :not_found, :configure,
:set, :mime_type, :enable, :disable, :use, :development?, :test?,
:production?, :helpers, :settings, :register
:production?, :helpers, :settings, :register, :on_start, :on_stop

class << self
attr_accessor :target
Expand Down
36 changes: 36 additions & 0 deletions test/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,42 @@ def teardown
@app.run!
end

context "event hooks" do
dummy_class = Class.new do
def self.start_hook; end
def self.stop_hook; end
end

it "runs the provided code when the server starts" do
@app.on_start do
dummy_class.start_hook
end
mock = MiniTest::Mock.new
mock.expect(:call, nil)

dummy_class.stub(:start_hook, mock) do
@app.run!
end

assert_mock mock
end

it "runs the provided code when the server stops" do
@app.on_stop do
dummy_class.stop_hook
end
mock = MiniTest::Mock.new
mock.expect(:call, nil)

dummy_class.stub(:stop_hook, mock) do
@app.run!
@app.quit!
end

assert_mock mock
end
end

it "sets options on the app before running" do
@app.run! :sessions => true
assert @app.sessions?
Expand Down

0 comments on commit 3fe6297

Please sign in to comment.