Coder Social home page Coder Social logo

fastly-rails's Introduction

Fastly Rails Plugin Build Status

Deprecation Notice

This repository has been deprecated. The official Fastly Ruby client is the fastly-ruby gem.

Although this repository is no longer supported, it could be useful as a reference.


Introduction

Fastly dynamic caching integration for Rails.

To use Fastly dynamic caching, you tag any response you wish to cache with unique Surrogate-Key HTTP Header(s) and then hit the Fastly API purge endpoint with the surrogate key when the response changes. The purge instantly replaces the cached response with a fresh response from origin.

This plugin provides three main things:

  • Instance and class methods on ActiveRecord (or Mongoid) objects for surrogate keys and purging
  • Controller helpers to set Cache-Control and Surrogate-Control response headers
  • A controller helper to set Surrogate-Key headers on responses

If you're not familiar with Fastly Surrogate Keys, you might want to check out API Caching and Fastly Surrogate Keys for a primer.

Setup

Add to your Gemfile

gem 'fastly-rails'

Configuration

For information about how to find your Fastly API Key or a Fastly Service ID, refer to our documentation.

Create an initializer for Fastly configuration

FastlyRails.configure do |c|
  c.api_key = ENV['FASTLY_API_KEY']  # Fastly api key, required
  c.max_age = 86400                  # time in seconds, optional, defaults to 2592000 (30 days)
  c.stale_while_revalidate = 86400   # time in seconds, optional, defaults to nil
  c.stale_if_error = 86400           # time in seconds, optional, defaults to nil
  c.service_id = ENV['SERVICE_ID']   # The Fastly service you will be using, required
  c.purging_enabled = !Rails.env.development? # No need to configure a client locally (AVAILABLE ONLY AS OF 0.4.0)
end

Note: purging only requires that you authenticate with your api_key. However, you can provide a user and password if you are using other endpoints in fastly-ruby that require full-auth. Also, you must provide a service_id for purges to work.

Usage

Surrogate Keys

Surrogate keys are what Fastly uses to purge groups of individual objects from our caches.

This plugin adds a few methods to generate surrogate keys automatically. table_key and record_key methods are added to any ActiveRecord::Base instance. table_key is also added to any ActiveRecord::Base class. In fact, table_key on an instance just calls table_key on the class.

We've chosen a simple surrogate key pattern by default. It is:

table_key: self.class.table_key # calls table_name on the class
record_key: "#{table_key}/#{self.id}"

e.g. If you have an ActiveRecord Model named Book.

table key: books
record key: books/1, books/2, books/3, etc...

You can easily override these methods in your models to use custom surrogate keys that may fit your specific application better:

def self.table_key
  "my_custom_table_key"
end

def record_key
  "my_custom_record_key"# Ensure these are unique for each record
end

Headers

This plugin adds a set_cache_control_headers method to ActionController. You'll need to add this in a before_filter or after_filter see note on cookies below to any controller action that you wish to edge cache (see example below). The method sets Cache-Control and Surrogate-Control HTTP Headers with a default of 30 days (remember you can configure this, see the initializer setup above).

It's up to you to set Surrogate-Key headers for objects that you want to be able to purge.

To do this use the set_surrogate_key_header method on GET actions.

class BooksController < ApplicationController
  # include this before_filter in controller endpoints that you wish to edge cache
  before_filter :set_cache_control_headers, only: [:index, :show]
  # This can be used with any customer actions. Set these headers for GETs that you want to cache
  # e.g. before_filter :set_cache_control_headers, only: [:index, :show, :my_custom_action]

  def index
    @books = Book.all
    set_surrogate_key_header 'books', @books.map(&:record_key)
  end

  def show
    @book = Book.find(params[:id])
    set_surrogate_key_header @book.record_key
  end
end

Purges

Any object that inherits from ActiveRecord will have purge_all, soft_purge_all, and table_key class methods available as well as purge, soft_purge, purge_all, and soft_purge_all instance methods.

Example usage is show below.

class BooksController < ApplicationController

  def create
    @book = Book.new(params)
    if @book.save
      @book.purge_all
      render @book
    end
  end

  def update
    @book = Book.find(params[:id])
    if @book.update(params)
      @book.purge
      render @book
    end
  end

  def delete
    @book = Book.find(params[:id])
    if @book.destroy
      @book.purge # purge the record
      @book.purge_all # purge the collection so the record is no longer there
    end
  end
end

To simplify controller methods, you could use ActiveRecord callbacks. e.g.

class Book < ActiveRecord::Base
  after_create :purge_all
  after_save :purge
  after_destroy :purge, :purge_all
  ...

end

We have left these out intentially, as they could potentially cause issues when running locally or testing. If you do use these, pay attention, as using callbacks could also inadvertently overwrite HTTP Headers like Cache-Control or Set-Cookie and cause responses to not be properly cached.

Service id

One thing to note is that currently we expect a service_id to be defined in your FastlyRails.configuration. However, we've added localized methods so that your models can override your global service_id, if you needed to operate on more than one for any reason. NOTE: As of 0.3.0, we've renamed the class-level and instance-level service_id methods to fastly_service_identifier in the active_record and mongoid mix-ins. See the CHANGELOG for a link to the Github issue.

Currently, this would require you to basically redefine fastly_service_identifier on the class level of your model:

class Book < ActiveRecord::Base
  def self.fastly_service_identifier
    'MYSERVICEID'
  end
end

Sessions, Cookies, and private data

By default, Fastly will not cache any response containing a Set-Cookie header. In general, this is beneficial because caching responses that contain sensitive data is typically not done on shared caches.

In this plugin the set_cache_control_headers method removes the Set-Cookie header from a request. In some cases, other libraries, particularily middleware, may insert or modify HTTP Headers outside the scope of where the set_cache_control_heades method is invoked in a controller action. For example, some authentication middleware will add a Set-Cookie header into requests after fastly-rails removes it.

This can cause some requests that can (and should) be cached to not be cached due to the presence of Set-Cookie.

In order to remove the Set-Cookie header in these cases, fastly-rails provides an optional piece of middleware that removes Set-Cookie when the Surrogate-Control or Surrogate-Key header is present (the Surrogate-Control header is also inserted by the set_cache_control_headers method and indicates that you want the endpoint to be cached by Fastly and do not need cookies).

fastly-rails middleware to delete Set-Cookie

To override a piece of middleware in Rails, insert custom middleware before it. Once you've identified which middleware is inserting the Set-Cookie header, add the following (in this example, ExampleMiddleware is what we are trying to override`:

# config/application.rb

  config.middleware.insert_before(
    ExampleMiddleware,
    "FastlyRails::Rack::RemoveSetCookieHeader"
  )

Example

Check out our example todo app which has a full example of fastly-rails integration in a simple rails app.

Future Work

  • Add an option to send purges in batches.

This will cut down on response delay from waiting for large amounts of purges to happen. This would primarily be geared towards write-heavy apps.

  • Your feedback

Testing

First, install all required gems:

$ appraisal install

This engine is capable of testing against multiple versions of Rails. It uses the appraisal gem. To make this happen, use the appraisal command in lieu of rake test:

$ appraisal rake test # tests against all the defined versions in the Appraisals file

$ appraisal rails-3 rake test # finds a defined version in the Appraisals file called "rails-3" and only runs tests against this version

Supported Platforms

We run tests using all combinations of the following versions of Ruby and Rails:

Ruby:

  • 1.9.3
  • 2.1.1

Rails:

  • v3.2.18
  • v4.0.5
  • v4.1.1

Other Platforms

As of v0.1.2, experimental Mongoid support was added by @joshfrench of Upworthy.

Credits

This plugin was developed by Fastly with lots of help from our friend at Hotel Tonight, Harlow Ward. Check out his blog about Cache Invalidation with Fastly Surrogate Keys which is where many of the ideas used in this plugin originated.

-- This project rocks and uses MIT-LICENSE.

fastly-rails's People

Contributors

annawinkler avatar aspires avatar blithe avatar caueguerra avatar chaslemley avatar clupprich avatar drbrain avatar ezkl avatar gabebw avatar gingermusketeer avatar gja avatar gschorkopf avatar jessieay avatar joshfrench avatar lanej avatar leklund avatar mmay avatar phantomwhale avatar sobbybutter avatar thommahoney avatar tricknotes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fastly-rails's Issues

Rails 6 Support

Rails 6 has been officially released.

The current gem version does not support rails 6, due to the lock on to railties < 6 in the gemspec.

Is there any incompatibilities with railties 6?

FastlyRails shouldn't raise error when api key isn't set

Currently, an error is raised when the API key isn't defined. This is pretty annoying during development because I obviously don't want my local development app to issue a real purge request.

Setting a dummy API key also doesn't help, because then Fastly throws an Unauthorized error.

rake fastly:purge_everything task

Would be cool to have something like that - doesn't look like the DSL supports that:

# lib/tasks/purge_fastly_cache.rake

namespace :fastly do
  desc 'Purge entire Fastly cache'
  task :purge do
    require Rails.root.join('config/initializers/fastly')
    FastlyRails.client.purge_everything
    puts 'Cache purged'
  end
end

Opt-in vs Opt-out for Edge Caching

Wanted to get an opinion on opt-in vs opt-out for edge-caching. cc @mmay

Currently if fastly-rails is included in a project all endpoints will be edge cached.

https://github.com/fastly/fastly-rails/blob/master/lib/fastly-rails/action_controller/cache_control_headers.rb#L5-L7

$ curl http://0.0.0.0:3000 -v -o /dev/null
< HTTP/1.1 200 OK
< Cache-Control: public, no-cache
< Surrogate-Control: max-age=2592000

I suspect this is not the desired behavior for any Rails apps with authentication.

The beauty of using Varnish with default Rails headers is nothing will be cached. This then gives the developer the opportunity to opt-in the endpoints they'd like cached.

# default Rails cache control
Cache-Control: max-age=0, private, must-revalidate

For example I wouldn't /admin/users edge-cached in my application.

With this setup i'd need to opt-out which seems like it could cause some issues if new developers don't know about the default public nature of the pages.

Does not work with rails-api

Was not able to workout why this was not working in a rails-api app. turns out that it is because the base controller is ActionController::API not ActionController.

This means that all the controller methods this gem provides are not available

Version 0.1.3 breaks Namespaced routes in my app

I'm new to using fastly and this is probably a super edge case, but I had an issue over the past few days that broke an app of mine. It's a rails 4.1 app that is multi-tenant using postgresql scemas.

I had a machine failure, so I needed to build my dev environment from scratch again and while restoring my dev environment I ran into several issues using this version 0.1.3 of the gem. When I first did bundle on the new machine, I was prompted that fastly 1.02 doesn't exist in any of the sources. So I ran bundle update --sources fastly-rails which then updated my gemfile to the latest fastly. This then manifested itself in two different ways:

  1. On my dev environment OSX 10.9.3 (on both my mac pro and macbook pro) any namespaced controller (including devise) throws a 'Uninitialized Constant error'. All other controllers seem to route just fine. Here's an example of the error
Started GET "/d/users/sign_in" for 127.0.0.1 at 2014-06-10 13:18:33 -0700

ActionController::RoutingError - uninitialized constant Devise::SessionsController:
  actionpack (4.1.0) lib/action_dispatch/routing/route_set.rb:69:in `rescue in controller'
  actionpack (4.1.0) lib/action_dispatch/routing/route_set.rb:64:in `controller'
  actionpack (4.1.0) lib/action_dispatch/routing/route_set.rb:44:in `call'
  actionpack (4.1.0) lib/action_dispatch/routing/mapper.rb:45:in `call'
  actionpack (4.1.0) lib/action_dispatch/journey/router.rb:71:in `block in call'
  actionpack (4.1.0) lib/action_dispatch/journey/router.rb:59:in `call'
  actionpack (4.1.0) lib/action_dispatch/routing/route_set.rb:676:in `call'
  meta_request (0.2.9) lib/meta_request/middlewares/app_request_handler.rb:13:in `call'
  rack-contrib (1.1.0) lib/rack/contrib/response_headers.rb:17:in `call'
  meta_request (0.2.9) lib/meta_request/middlewares/headers.rb:16:in `call'
  meta_request (0.2.9) lib/meta_request/middlewares/meta_request_handler.rb:13:in `call'
  bullet (4.8.0) lib/bullet/rack.rb:10:in `call'
  request_store (1.0.5) lib/request_store/middleware.rb:9:in `call'
  warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
  warden (1.2.3) lib/warden/manager.rb:34:in `call'
  rack (1.5.2) lib/rack/etag.rb:23:in `call'
  rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
  rack (1.5.2) lib/rack/head.rb:11:in `call'
  remotipart (1.2.1) lib/remotipart/middleware.rb:27:in `call'
  actionpack (4.1.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.1.0) lib/action_dispatch/middleware/flash.rb:254:in `call'
  rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.1.0) lib/action_dispatch/middleware/cookies.rb:560:in `call'
  activerecord (4.1.0) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.1.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
  activerecord (4.1.0) lib/active_record/migration.rb:380:in `call'
  actionpack (4.1.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.1.0) lib/active_support/callbacks.rb:82:in `run_callbacks'
  actionpack (4.1.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.1.0) lib/action_dispatch/middleware/reloader.rb:73:in `call'
  actionpack (4.1.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
  better_errors (1.1.0) lib/better_errors/middleware.rb:84:in `protected_app_call'
  better_errors (1.1.0) lib/better_errors/middleware.rb:79:in `better_errors_call'
  better_errors (1.1.0) lib/better_errors/middleware.rb:56:in `call'
  actionpack (4.1.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  actionpack (4.1.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.1.0) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.1.0) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.1.0) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.1.0) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.1.0) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.1.0) lib/rails/rack/logger.rb:20:in `call'
  quiet_assets (1.0.2) lib/quiet_assets.rb:18:in `call_with_quiet_assets'
  actionpack (4.1.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.1.0) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  actionpack (4.1.0) lib/action_dispatch/middleware/static.rb:64:in `call'
  rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
  rack-mini-profiler (0.9.1) lib/mini_profiler/profiler.rb:300:in `call'
  railties (4.1.0) lib/rails/engine.rb:514:in `call'
  railties (4.1.0) lib/rails/application.rb:144:in `call'
  /Users/eschalon/Library/Application Support/Pow/Versions/0.4.3/node_modules/nack/lib/nack/server.rb:155:in `handle'
  /Users/eschalon/Library/Application Support/Pow/Versions/0.4.3/node_modules/nack/lib/nack/server.rb:109:in `rescue in block (2 levels) in start'
  /Users/eschalon/Library/Application Support/Pow/Versions/0.4.3/node_modules/nack/lib/nack/server.rb:106:in `block (2 levels) in start'
  /Users/eschalon/Library/Application Support/Pow/Versions/0.4.3/node_modules/nack/lib/nack/server.rb:96:in `block in start'
  /Users/eschalon/Library/Application Support/Pow/Versions/0.4.3/node_modules/nack/lib/nack/server.rb:76:in `start'
  /Users/eschalon/Library/Application Support/Pow/Versions/0.4.3/node_modules/nack/lib/nack/server.rb:12:in `run'
  /Users/eschalon/Library/Application Support/Pow/Versions/0.4.3/node_modules/nack/bin/nack_worker:4:in `<main>'
  1. On my staging environment (Ubuntu 13.10) any namespaced controller throws a ActionView::MissingTemplate error like such
An ActionView::MissingTemplate occurred in sessions#new: 

Missing template users/sessions/new, devise::_sessions/new, devise/new, application/new with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee, :haml]}. Searched in: 
* "/home/deployer/apps/blocks/releases/20140609015205/app/views" 
* "/home/deployer/apps/blocks/shared/bundle/ruby/1.9.1/gems/the_sortable_tree-2.3.3/app/views" 
* "/home/deployer/apps/blocks/shared/bundle/ruby/1.9.1/bundler/gems/the_role_bootstrap3_ui-22e1797c0bad/app/views" 
* "/home/deployer/apps/blocks/shared/bundle/ruby/1.9.1/gems/devise-3.2.4/app/views" 
* "/home/deployer/apps/blocks/shared/bundle/ruby/1.9.1/bundler/gems/twitter-bootstrap-rails-cee3c805830b/app/views" 

app/models/tenant.rb:61:in `scope_schema' 

For now, I've commented out the fastly gem and my app is working again. I'd love to use it again, so if there's any other information you need from me to help track down the cause of this please let me know.

Documentation for middleware has some outdated syntax.

There shouldn't be quotes around the FastlyRails middleware.

# config/application.rb

  config.middleware.insert_before(
    ExampleMiddleware,
    "FastlyRails::Rack::RemoveSetCookieHeader"
  )

Should be:

# config/application.rb

  config.middleware.insert_before(
    ExampleMiddleware,
    FastlyRails::Rack::RemoveSetCookieHeader
  )

Enable use of current version of mime-types

fastly-rails version 0.5.0 requires "mime-types (>= 1.16, < 3)", but the current version of mime-types is 3.0. Please make it possible to use the current version of mime-types.

Thanks!

soft_purge fails with - ArgumentError - wrong number of arguments (given 2, expected 1)

Ruby ruby 2.3.1p112 (2016-04-26 revision 54768) [i686-linux]
Rails 4.2.7

All my soft purges are failing with a multi-argument error:

ArgumentError - wrong number of arguments (given 2, expected 1):
  fastly-rails (0.7.0) lib/fastly-rails/client.rb:11:in `purge_by_key'
  fastly-rails (0.7.0) lib/fastly-rails.rb:26:in `purge_by_key'
  fastly-rails (0.7.0) lib/fastly-rails/active_record/surrogate_key.rb:41:in `soft_purge'

I'm calling FastlyRails myself in some cases, and in others using the suggest approach:

class License < ActiveRecord::Base
    attr_accessor :state_action

    # == soft purge - invalidates without deleting
    after_update :soft_purge
    after_destroy :soft_purge
end

I'm suspecting it's possibly an change/issue with ruby 2.3 and the use of the DelegateClass in fastly-rails/client.rb

As far as I can tell, the rails-fastly surrogate_key wrapper for ActiveRecords seems to never defer to the fastly/client.rb to carry out purges (which handles soft=true)

 # Purge the specified path from your cache.
  def purge(url, soft=false)
    client.purge(url, soft ? { headers: { 'Fastly-Soft-Purge' => "1"} } : {})
  end

It seems to do it all itself with a POST from purge_by_key

module FastlyRails
  # A simple wrapper around the fastly-ruby client.
  class Client < DelegateClass(Fastly)
    def initialize(opts={})
      super(Fastly.new(opts))
    end

    def purge_by_key(key)
      client.require_key!
      client.post purge_url(key)
    end

    def purge_url(key)
      "/service/#{FastlyRails.service_id}/purge/#{URI.escape(key)}"
    end
  end
end

Have different configuration for a particular controller?

I have following default configuration in my initializer directory. I want to have different configurations for a particular controller. What should I do? It's not mentioned in the readme.

# frozen_string_literal: true
FastlyRails.configure do |c|
  c.api_key = ENV['FASTLY_API_KEY'] # Fastly api key, required
  c.max_age = 300                  # time in seconds, optional, defaults to 2592000 (30 days)
  c.stale_while_revalidate = 300   # time in seconds, optional, defaults to nil
  c.stale_if_error = 300           # time in seconds, optional, defaults to nil
  c.service_id = ENV['FASTLY_SERVICE_ID'] # The Fastly service you will be using, required
  c.purging_enabled = !Rails.env.development? # No need to configure a client locally (AVAILABLE ONLY AS OF 0.4.0)
end

Failing purges with fastly-ruby v1.1.5

Changes introduced to purging in fastly-ruby v1.1.5 do not play nice with fastly-rails v0.2.0.
Reproduced on ruby 2.1.0

Note v0.2.0 bumps the fastly-ruby dep to 1.1.4.

stack trace

Fastly::Error: Bad Request 
from /app/vendor/bundle/ruby/2.1.0/gems/fastly-1.1.5/lib/fastly/client.rb:95:in `post_and_put' 
from /app/vendor/bundle/ruby/2.1.0/gems/fastly-1.1.5/lib/fastly/client.rb:74:in `post' 
from /app/vendor/bundle/ruby/2.1.0/gems/fastly-rails-0.2.0/lib/fastly-rails/client.rb:12:in `purge_by_key'
from /app/vendor/bundle/ruby/2.1.0/gems/fastly-rails-0.2.0/lib/fastly-rails/active_record/surrogate_key.rb:33:in `purge'

Internal Server Error

Hi,

I've configured fastly-rails gem in my application as described in README.

But whenever I try to purge anything, I get this error:

2.0.0-p353 :001 > FastlyRails.client
 => #<Fastly:0x007fb5bcb83d68 @client=#<Fastly::Client:0x007fb5bcb83ca0 @api_key="xxx", @user="xxx", @password="xxx", @http=#<Fastly::Client::Curl:0x007fb5bcb828a0 @host="api.fastly.com", @port=443, @protocol="https">, @cookie="fastly.session=xxx; Expires=Tue, 17 Jun 2014 14:09:43 GMT;Path=/; HttpOnly; secure">> 
2.0.0-p353 :002 > FastlyRails.client.purge("test")
Fastly::Error: Internal Server Error
    from /Users/scaryguy/.rvm/gems/ruby-2.0.0-p353@kuretv/gems/fastly-1.02/lib/fastly/client.rb:90:in `post_and_put'
    from /Users/scaryguy/.rvm/gems/ruby-2.0.0-p353@kuretv/gems/fastly-1.02/lib/fastly/client.rb:73:in `post'
    from /Users/scaryguy/.rvm/gems/ruby-2.0.0-p353@kuretv/gems/fastly-1.02/lib/fastly.rb:86:in `purge'
    from (irb):2
    from /Users/scaryguy/.rvm/gems/ruby-2.0.0-p353@kuretv/gems/railties-4.0.5/lib/rails/commands/console.rb:90:in `start'
    from /Users/scaryguy/.rvm/gems/ruby-2.0.0-p353@kuretv/gems/railties-4.0.5/lib/rails/commands/console.rb:9:in `start'
    from /Users/scaryguy/.rvm/gems/ruby-2.0.0-p353@kuretv/gems/railties-4.0.5/lib/rails/commands.rb:62:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

As I'm using it in my controllers just like @instance.purge it's giving the same error.

In my models, when I use callbacks to call :purge , same error.

But when I try to purge something using curl everything is okay:

$ curl -X PURGE -H "Authenticate: xxx"  https://api.fastly.com/http://xx/xx
#=> {"status": "ok", "id": "76-1401693893-93505"}

Removing cookies messes up with flash

The notices/alerts I set in the flash are somehow persisted across requests when cookies are unset, whether it's through use of a before_filter or rack middleware. The result is that the same message is shown over and over, until the user reaches an action that doesn't remove cookies.

Incompatibilities with "service_id" as a column name

At the moment, this gem isn't compatible with any rails application that has a table with a column named "service_id".

It throws

ActiveRecord::DangerousAttributeError:
       service_id is defined by Active Record. Check to make sure that you don't have an attribute or method with the same name.

My application has a table with a column name of "service_id", so this is problematic.

soft_purge doesn't actually issue soft header - it's a hard purge

Same code as #61. Running fastly-rails#0.7.1 using httplog to watch requests.

I don't see any Fastly-Soft-Purge:1 header being sent with the request as the docs indicate. It looks like fastly-rails/client.rb is keeping it too simple and not deferring the the fastly api gem to do this correctly.

after_save :soft_purge results in

DEBUG -- : [httplog] Sending: POST http://api.fastly.com:443/service/ABC123/purge/mykey/123
DEBUG -- : [httplog] Data: 
DEBUG -- : [httplog] Status: 200
DEBUG -- : [httplog] Benchmark: 0.12983361499937018 seconds
DEBUG -- : [httplog] Response: {"status": "ok", "id": "577-xxxx-32"}

From the docs I'm expecting to see a PURGE request and a header sent

Soft Purge fails on 0.7.0

Soft purge on an ActiveRecord backed model fails and spits this out:

To reproduce - call any ActiveRecord model with:

some_object.soft_purge

or

some_model.soft_purge_all
ArgumentError: wrong number of arguments (2 for 1)
  from /Users/my_username_redacted/.rvm/gems/ruby-2.2.2@maker/gems/fastly-rails-0.7.0/lib/fastly-rails/client.rb:11:in `purge_by_key'
  from /Users/my_username_redacted/.rvm/gems/ruby-2.2.2@maker/gems/fastly-rails-0.7.0/lib/fastly-rails.rb:26:in `purge_by_key'
  from /Users/my_username_redacted/.rvm/gems/ruby-2.2.2@maker/gems/fastly-rails-0.7.0/lib/fastly-rails/active_record/surrogate_key.rb:41:in `soft_purge'
  from (irb):7
  from /Users/my_username_redacted/.rvm/gems/ruby-2.2.2@maker/gems/railties-4.2.4/lib/rails/commands/console.rb:110:in `start'
  from /Users/my_username_redacted/.rvm/gems/ruby-2.2.2@maker/gems/railties-4.2.4/lib/rails/commands/console.rb:9:in `start'
  from /Users/my_username_redacted/.rvm/gems/ruby-2.2.2@maker/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:68:in `console'
  from /Users/my_username_redacted/.rvm/gems/ruby-2.2.2@maker/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
  from /Users/my_username_redacted/.rvm/gems/ruby-2.2.2@maker/gems/railties-4.2.4/lib/rails/commands.rb:17:in `<top (required)>'
  from bin/rails:4:in `require'
  from bin/rails:4:in `<main>'

I'm on Rails 4.2.4 + Ruby 2.2.2.

Fwiw, I was previously doing this before the soft_purge support:

FastlyRails.client.get_service(ENV.fetch("FASTLY_SERVICE_ID")).purge_by_key(some_object.record_key, true)

(I originally brought it up here - #56)

Rails 5 Support

Currently, fastly-rails does not support Rails 5 due to being locked to railtie < 5 in the gemspec. Adding fastly-rails to a rails project silently resolves to a really old version, fastly-rails (0.4.1), the last version before the lock on railtie was added.

This seems to work fine in newer versions of rails, would it be possible to remove the dependency on railtie version, and mimetype?

Ruby 2.5.0, fastly-rails 0.8.0 errors

In upgrading our Rails App, I also updated the fastly-rails gem from 0.2 to 0.8.0. But in doing so, I am now seeing a whole string of new failures related to the fastly-rails gem. Roughly ~800 failures in a 24 hour period from a single worker. We also updated Ruby from 2.3.1 to 2.5.0. Perhaps, related?

Environment:

  • Rails 5.1.5
  • Ruby 2.5.0
  • fastly-rails 0.8.0

Here is the assortment of errors we are seeing with relevant stacktraces.

Anyone else seeing anything like this? Any suggestions?

  • JSON::ParserError: Empty input at line 1, column 1 [parse.c:926] in 'HTTP/1.1 200 OK Content-Type: application/json C
1 File "/app/vendor/bundle/ruby/2.5.0/gems/fastly-1.15.0/lib/fastly/client.rb" line 124 in parse
2 File "/app/vendor/bundle/ruby/2.5.0/gems/fastly-1.15.0/lib/fastly/client.rb" line 124 in post_and_put
3 File "/app/vendor/bundle/ruby/2.5.0/gems/fastly-1.15.0/lib/fastly/client.rb" line 87 in post
4 File "/app/vendor/bundle/ruby/2.5.0/gems/fastly-1.15.0/lib/fastly/service.rb" line 74 in purge_by_key
5 File "/app/vendor/bundle/ruby/2.5.0/gems/fastly-rails-0.8.0/lib/fastly-rails/client.rb" line 12 in purge_by_key
6 File "/app/vendor/bundle/ruby/2.5.0/gems/fastly-rails-0.8.0/lib/fastly-rails.rb" line 26 in purge_by_key
7 File "/app/vendor/bundle/ruby/2.5.0/gems/fastly-rails-0.8.0/lib/fastly-rails/active_record/surrogate_key.rb" line 37 in purge
  • Net::HTTPBadResponse: wrong status line: "ntent-Length: 50โ€
1 File "/app/vendor/ruby-2.5.0/lib/ruby/2.5.0/net/http/response.rb" line 42 in read_status_line
2 File "/app/vendor/ruby-2.5.0/lib/ruby/2.5.0/net/http/response.rb" line 29 in read_new
3 File "/app/vendor/ruby-2.5.0/lib/ruby/2.5.0/net/http.rb" line 1494 in block in transport_request
4 File "/app/vendor/ruby-2.5.0/lib/ruby/2.5.0/net/http.rb" line 1491 in catch
5 File "/app/vendor/ruby-2.5.0/lib/ruby/2.5.0/net/http.rb" line 1491 in transport_request
6 File "/app/vendor/ruby-2.5.0/lib/ruby/2.5.0/net/http.rb" line 1464 in request
16 File "/app/vendor/ruby-2.5.0/lib/ruby/2.5.0/net/http.rb" line 1478 in send_entity
17 File "/app/vendor/ruby-2.5.0/lib/ruby/2.5.0/net/http.rb" line 1266 in post
18 File "/app/vendor/bundle/ruby/2.5.0/gems/fastly-1.15.0/lib/fastly/client.rb" line 122 in post_and_put
19 File "/app/vendor/bundle/ruby/2.5.0/gems/fastly-1.15.0/lib/fastly/client.rb" line 87 in post
20 File "/app/vendor/bundle/ruby/2.5.0/gems/fastly-1.15.0/lib/fastly/service.rb" line 74 in purge_by_key
21 File "/app/vendor/bundle/ruby/2.5.0/gems/fastly-rails-0.8.0/lib/fastly-rails/client.rb" line 12 in purge_by_key
22 File "/app/vendor/bundle/ruby/2.5.0/gems/fastly-rails-0.8.0/lib/fastly-rails.rb" line 26 in purge_by_key
23 File "/app/vendor/bundle/ruby/2.5.0/gems/fastly-rails-0.8.0/lib/fastly-rails/active_record/surrogate_key.rb" line 37 in purge
  • Errno::EBADF: Bad file descriptor
1 File "/app/vendor/ruby-2.5.0/lib/ruby/2.5.0/net/protocol.rb" line 181 in wait_readable
2 File "/app/vendor/ruby-2.5.0/lib/ruby/2.5.0/net/protocol.rb" line 181 in rbuf_fill
3 File "/app/vendor/ruby-2.5.0/lib/ruby/2.5.0/net/protocol.rb" line 125 in read
4 File "/app/vendor/ruby-2.5.0/lib/ruby/2.5.0/net/http/response.rb" line 293 in block in read_body_0
5 File "/app/vendor/ruby-2.5.0/lib/ruby/2.5.0/net/http/response.rb" line 278 in inflater
6 File "/app/vendor/ruby-2.5.0/lib/ruby/2.5.0/net/http/response.rb" line 283 in read_body_0
7 File "/app/vendor/ruby-2.5.0/lib/ruby/2.5.0/net/http/response.rb" line 204 in read_body
8 File "/app/vendor/ruby-2.5.0/lib/ruby/2.5.0/net/http.rb" line 1479 in block in send_entity
9 File "/app/vendor/ruby-2.5.0/lib/ruby/2.5.0/net/http.rb" line 1503 in block in transport_request
10 File "/app/vendor/ruby-2.5.0/lib/ruby/2.5.0/net/http/response.rb" line 165 in reading_body
11 File "/app/vendor/ruby-2.5.0/lib/ruby/2.5.0/net/http.rb" line 1502 in transport_request
12 File "/app/vendor/ruby-2.5.0/lib/ruby/2.5.0/net/http.rb" line 1464 in request
20 File "/app/vendor/ruby-2.5.0/lib/ruby/2.5.0/net/http.rb" line 1478 in send_entity
21 File "/app/vendor/ruby-2.5.0/lib/ruby/2.5.0/net/http.rb" line 1266 in post
22 File "/app/vendor/bundle/ruby/2.5.0/gems/fastly-1.15.0/lib/fastly/client.rb" line 122 in post_and_put
23 File "/app/vendor/bundle/ruby/2.5.0/gems/fastly-1.15.0/lib/fastly/client.rb" line 87 in post
24 File "/app/vendor/bundle/ruby/2.5.0/gems/fastly-1.15.0/lib/fastly/service.rb" line 74 in purge_by_key
25 File "/app/vendor/bundle/ruby/2.5.0/gems/fastly-rails-0.8.0/lib/fastly-rails/client.rb" line 12 in purge_by_key
26 File "/app/vendor/bundle/ruby/2.5.0/gems/fastly-rails-0.8.0/lib/fastly-rails.rb" line 26 in purge_by_key
27 File "/app/vendor/bundle/ruby/2.5.0/gems/fastly-rails-0.8.0/lib/fastly-rails/active_record/surrogate_key.rb" line 37 in purge
  • IOError: closed stream
1 File "/app/vendor/ruby-2.5.0/lib/ruby/2.5.0/net/protocol.rb" line 181 in wait_readable
2 File "/app/vendor/ruby-2.5.0/lib/ruby/2.5.0/net/protocol.rb" line 181 in rbuf_fill
3 File "/app/vendor/ruby-2.5.0/lib/ruby/2.5.0/net/protocol.rb" line 157 in readuntil
4 File "/app/vendor/ruby-2.5.0/lib/ruby/2.5.0/net/protocol.rb" line 167 in readline
5 File "/app/vendor/ruby-2.5.0/lib/ruby/2.5.0/net/http/response.rb" line 40 in read_status_line
6 File "/app/vendor/ruby-2.5.0/lib/ruby/2.5.0/net/http/response.rb" line 29 in read_new
7 File "/app/vendor/ruby-2.5.0/lib/ruby/2.5.0/net/http.rb" line 1494 in block in transport_request
8 File "/app/vendor/ruby-2.5.0/lib/ruby/2.5.0/net/http.rb" line 1491 in catch
9 File "/app/vendor/ruby-2.5.0/lib/ruby/2.5.0/net/http.rb" line 1491 in transport_request
10 File "/app/vendor/ruby-2.5.0/lib/ruby/2.5.0/net/http.rb" line 1464 in request
20 File "/app/vendor/ruby-2.5.0/lib/ruby/2.5.0/net/http.rb" line 1478 in send_entity
21 File "/app/vendor/ruby-2.5.0/lib/ruby/2.5.0/net/http.rb" line 1266 in post
22 File "/app/vendor/bundle/ruby/2.5.0/gems/fastly-1.15.0/lib/fastly/client.rb" line 122 in post_and_put
23 File "/app/vendor/bundle/ruby/2.5.0/gems/fastly-1.15.0/lib/fastly/client.rb" line 87 in post
24 File "/app/vendor/bundle/ruby/2.5.0/gems/fastly-1.15.0/lib/fastly/service.rb" line 74 in purge_by_key
25 File "/app/vendor/bundle/ruby/2.5.0/gems/fastly-rails-0.8.0/lib/fastly-rails/client.rb" line 12 in purge_by_key
26 File "/app/vendor/bundle/ruby/2.5.0/gems/fastly-rails-0.8.0/lib/fastly-rails.rb" line 26 in purge_by_key
27 File "/app/vendor/bundle/ruby/2.5.0/gems/fastly-rails-0.8.0/lib/fastly-rails/active_record/surrogate_key.rb" line 37 in purge

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.