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: env:
- RACK_ENV=test - RACK_ENV=test
rvm: rvm:
- 2.4.2 - 2.5.0
sudo: false sudo: false

View File

@ -1,4 +1,4 @@
FROM ruby:2.4.2 FROM ruby:2.5.0
RUN mkdir /app RUN mkdir /app
WORKDIR /app WORKDIR /app
@ -6,4 +6,4 @@ ADD Gemfile /app/Gemfile
ADD Gemfile.lock /app/Gemfile.lock ADD Gemfile.lock /app/Gemfile.lock
RUN bundle install --without development test RUN bundle install --without development test
ADD . /app 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 './config/environment'
require 'api' require 'api'
require 'schedule'
run Sinatra::Application 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 # frozen_string_literal: true
namespace :rates do namespace :rates do
desc 'Load all rates' desc 'Reload all rates'
task load: :environment do task reload: :environment do
require 'currency' require 'bank'
require 'fixer' Bank.fetch_all_rates!
Currency.db.transaction do
Currency.dataset.delete
data = Fixer::Feed.new(:historical)
Currency.multi_insert(data.to_a)
end
end end
desc 'Update rates' desc 'Update current rates'
task update: :environment do task update: :environment do
require 'currency' require 'bank'
require 'fixer' Bank.fetch_current_rates!
Fixer::Feed.new.each do |hsh|
Currency.find_or_create(hsh)
end
end end
end end

View File

@ -1,6 +1,6 @@
# frozen_string_literal: true # frozen_string_literal: true
return unless ENV['RACK_ENV'] == 'test' return if ENV['RACK_ENV'] == 'production'
require 'rake/testtask' require 'rake/testtask'
require 'rubocop/rake_task' require 'rubocop/rake_task'
@ -13,4 +13,4 @@ end
RuboCop::RakeTask.new 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' version: '3'
services: services:
web: web:
build:
context: ./app
environment: environment:
RACK_ENV: development RACK_ENV: development
VIRTUAL_HOST: localhost VIRTUAL_HOST: localhost

View File

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

View File

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