Update rates in a separate thread in main app

This commit is contained in:
Hakan Ensari 2017-12-31 16:44:08 +00:00
parent 3e78e0ab6e
commit f3e1827f2c
12 changed files with 74 additions and 53 deletions

View File

@ -6,5 +6,5 @@ before_install:
env:
- RACK_ENV=test
rvm:
- 2.4.2
- 2.5.0
sudo: false

View File

@ -1,4 +1,4 @@
FROM ruby:2.4.2
FROM ruby:2.5.0
RUN mkdir /app
WORKDIR /app
@ -6,4 +6,4 @@ ADD Gemfile /app/Gemfile
ADD Gemfile.lock /app/Gemfile.lock
RUN bundle install --without development test
ADD . /app
CMD unicorn -c ./config/unicorn.rb
CMD ["unicorn", "-c", "./config/unicorn.rb"]

View File

@ -1,15 +0,0 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
require_relative '../config/environment'
require 'rake'
require 'rufus-scheduler'
schedule = Rufus::Scheduler.new
schedule.cron '*/15 15,16,17 * * 1-5' do
load 'tasks/rates.rake'
Rake::Task['rates:update'].execute
end
schedule.join

View File

@ -2,5 +2,6 @@
require './config/environment'
require 'api'
require 'schedule'
run Sinatra::Application

22
app/lib/bank.rb Normal file
View File

@ -0,0 +1,22 @@
# frozen_string_literal: true
require 'currency'
require 'fixer'
module Bank
def self.fetch_all_rates!
Currency.db.transaction do
Currency.dataset.delete
data = Fixer.historical
Currency.multi_insert(data.to_a)
end
end
def self.fetch_current_rates!
Currency.db.transaction do
Fixer.current.each do |hsh|
Currency.find_or_create(hsh)
end
end
end
end

10
app/lib/schedule.rb Normal file
View File

@ -0,0 +1,10 @@
# frozen_string_literal: true
require 'bank'
require 'rufus-scheduler'
schedule = Rufus::Scheduler.new
schedule.cron '*/15 15,16,17 * * 1-5' do
Bank.fetch_current_rates!
end

View File

@ -1,25 +1,15 @@
# frozen_string_literal: true
namespace :rates do
desc 'Load all rates'
task load: :environment do
require 'currency'
require 'fixer'
Currency.db.transaction do
Currency.dataset.delete
data = Fixer::Feed.new(:historical)
Currency.multi_insert(data.to_a)
end
desc 'Reload all rates'
task reload: :environment do
require 'bank'
Bank.fetch_all_rates!
end
desc 'Update rates'
desc 'Update current rates'
task update: :environment do
require 'currency'
require 'fixer'
Fixer::Feed.new.each do |hsh|
Currency.find_or_create(hsh)
end
require 'bank'
Bank.fetch_current_rates!
end
end

View File

@ -1,6 +1,6 @@
# frozen_string_literal: true
return unless ENV['RACK_ENV'] == 'test'
return if ENV['RACK_ENV'] == 'production'
require 'rake/testtask'
require 'rubocop/rake_task'
@ -13,4 +13,4 @@ end
RuboCop::RakeTask.new
task default: %w[db:migrate rates:load test rubocop]
task default: %w[db:migrate rates:reload test rubocop]

22
app/spec/bank_spec.rb Normal file
View File

@ -0,0 +1,22 @@
# frozen_string_literal: true
require_relative 'helper'
require 'bank'
describe Bank do
around do |test|
Currency.db.transaction do
test.call
raise Sequel::Rollback
end
end
before do
Currency.dataset.delete
end
it 'fetches current rates' do
Bank.fetch_current_rates!
Currency.count.must_be :positive?
end
end

View File

@ -1,6 +1,8 @@
version: '3'
services:
web:
build:
context: ./app
environment:
RACK_ENV: development
VIRTUAL_HOST: localhost

View File

@ -1,16 +1,15 @@
version: '3'
services:
db:
restart: always
restart: unless-stopped
web:
env_file: .env
image: hakanensari/fixer
logging:
options:
max-size: "50m"
max-file: "10"
restart: always
scheduler:
restart: always
restart: unless-stopped
nginx-proxy:
labels:
- "com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy=true"
@ -21,7 +20,7 @@ services:
ports:
- "80:80"
- "443:443"
restart: always
restart: unless-stopped
volumes:
- /etc/nginx/conf.d
- ./limit_req.conf:/etc/nginx/conf.d/limit_req.conf:ro
@ -32,7 +31,7 @@ services:
image: jrcs/letsencrypt-nginx-proxy-companion
depends_on:
- nginx-proxy
restart: always
restart: unless-stopped
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- certs:/etc/nginx/certs:rw

View File

@ -5,8 +5,6 @@ services:
volumes:
- data:/var/lib/postgresql/data
web:
build:
context: ./app
command: unicorn -c config/unicorn.rb
environment:
DATABASE_URL: postgres://postgres@db/postgres
@ -15,14 +13,6 @@ services:
- '8080'
links:
- db
scheduler:
build:
context: ./app
command: bin/schedule
environment:
DATABASE_URL: postgres://postgres@db/postgres
links:
- db
nginx-proxy:
image: jwilder/nginx-proxy
volumes: