Skip to content

Commit

Permalink
Recommend people to use cron tasks instead of background jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
fschuindt committed Apr 29, 2017
1 parent 855bcbc commit bbd48d4
Showing 1 changed file with 34 additions and 17 deletions.
51 changes: 34 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ gem install firebase_id_token

or in your Gemfile
```
gem 'firebase_id_token', '~> 1.2.1'
gem 'firebase_id_token', '~> 1.2.2'
```
then
```
Expand Down Expand Up @@ -113,33 +113,50 @@ FirebaseIdToken::Certificates.find('ec8f292sd30224afac5c55540df66d1f999d')

#### Downloading in Rails

If you are using Rails it's expected you to download certificates in a background job, you can use [ActiveJob](http://guides.rubyonrails.org/active_job_basics.html).
If you are using Rails, it's clever to download certificates in a cron task, you can use [whenever](https://github.com/javan/whenever).

```ruby
class RequestCertificatesJob < ApplicationJob
queue_as :default
**Example**

*Read whenever's guide on how to set it up.*

def perform
FirebaseIdToken::Certificates.request_anyway
Create your task in `lib/tasks/firebase.rake`:
```ruby
namespace :firebase do
namespace :certificates do
desc "Request Google's x509 certificates when Redis is empty"
task request: :environment do
FirebaseIdToken::Certificates.request
end

desc "Request Google's x509 certificates and override Redis"
task request_anyway: :environment do
FirebaseIdToken::Certificates.request_anyway
end
end
end
```

Then set it as a cron job, I recommend running it once every hour or every 30 minutes, it's up to you. Normally the certificates expiration time is around 5 to 6 hours, but it's good to perform it in a small fraction of this time.

You can use [whenever](https://github.com/javan/whenever) to do this.
And in your `config/schedule.rb` you might have:
```ruby
every 1.hour do
rake 'firebase:certificates:request_anyway'
end
```

It's clever to execute this job every time the application starts too.
You might have a `config/initializers/firebase_id_token.rb`:
Then:
```
$ whenever --update-crontab
```

```ruby
RequestCertificatesJob.perform_later
I recommend running it once every hour or every 30 minutes, it's up to you. Normally the certificates expiration time is around 4 to 6 hours, but it's good to perform it in a small fraction of this time.

FirebaseIdToken.configure do |config|
config.project_ids = ['your-firebase-project-id']
end
When developing and testing you should just run the task:
```
$ rake firebase:certificates:request
```

*And remember, you need the Redis server to be running.*

### Verifying Tokens

Pass the Firebase ID Token to `FirebaseIdToken::Signature.verify` and it will return the token payload if everything is ok:
Expand Down

0 comments on commit bbd48d4

Please sign in to comment.