Sha256: 3d79020355f935c0183645ca9db3c4ba76ab9786da0a30b8a30467db5ba87316

Contents?: true

Size: 989 Bytes

Versions: 14

Compression:

Stored size: 989 Bytes

Contents

require 'google-cloud-tasks'
require 'net/http'

module RailsCloudTasks
  module AppEngine
    class << self
      def project_id
        @project_id ||= metadata('project/project-id')
      end

      def service_account_email
        @service_account_email ||= metadata('instance/service-accounts/default/email')
      end

      def reset!
        @project_id = nil
        @service_account_email = nil
      end

      protected

      def metadata(path)
        return nil unless ::Google::Auth::GCECredentials.on_gce?

        uri = URI("http://metadata.google.internal/computeMetadata/v1/#{path}")

        req = Net::HTTP::Get.new(uri)
        req['Metadata-Flavor'] = 'Google'

        Net::HTTP.start(uri.hostname, uri.port) { |http| http.request(req) }.body
      rescue Errno::EHOSTDOWN
        # This error occurs sporadically when trying to resolve the metadata endpoint
        # locally. It is unlikely to occur when running on GCE.
        nil
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
rails-cloud-tasks-0.0.10 lib/rails_cloud_tasks/app_engine.rb
rails-cloud-tasks-0.0.9 lib/rails_cloud_tasks/app_engine.rb
rails-cloud-tasks-0.0.9.pre.rc lib/rails_cloud_tasks/app_engine.rb
rails-cloud-tasks-0.0.8 lib/rails_cloud_tasks/app_engine.rb
rails-cloud-tasks-0.0.7 lib/rails_cloud_tasks/app_engine.rb
rails-cloud-tasks-0.0.7.pre.rc lib/rails_cloud_tasks/app_engine.rb
rails-cloud-tasks-0.0.6 lib/rails_cloud_tasks/app_engine.rb
rails-cloud-tasks-0.0.5 lib/rails_cloud_tasks/app_engine.rb
rails-cloud-tasks-0.0.4 lib/rails_cloud_tasks/app_engine.rb
rails-cloud-tasks-0.0.4rc2 lib/rails_cloud_tasks/app_engine.rb
rails-cloud-tasks-0.0.4.rc1 lib/rails_cloud_tasks/app_engine.rb
rails-cloud-tasks-0.0.3 lib/rails_cloud_tasks/app_engine.rb
rails-cloud-tasks-0.0.2 lib/rails_cloud_tasks/app_engine.rb
rails-cloud-tasks-0.0.1 lib/rails_cloud_tasks/app_engine.rb