diff --git a/.travis.yml b/.travis.yml index 5402c12..883a6f1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,5 +6,5 @@ before_install: env: - RACK_ENV=test rvm: - - 2.4.2 + - 2.5.0 sudo: false diff --git a/app/Dockerfile b/app/Dockerfile index f8ce4e0..31c9450 100644 --- a/app/Dockerfile +++ b/app/Dockerfile @@ -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"] diff --git a/app/bin/schedule b/app/bin/schedule deleted file mode 100755 index f259ab9..0000000 --- a/app/bin/schedule +++ /dev/null @@ -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 diff --git a/app/config.ru b/app/config.ru index 9450151..56396f9 100644 --- a/app/config.ru +++ b/app/config.ru @@ -2,5 +2,6 @@ require './config/environment' require 'api' +require 'schedule' run Sinatra::Application diff --git a/app/lib/bank.rb b/app/lib/bank.rb new file mode 100644 index 0000000..eaa0c9f --- /dev/null +++ b/app/lib/bank.rb @@ -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 diff --git a/app/lib/schedule.rb b/app/lib/schedule.rb new file mode 100644 index 0000000..90064c7 --- /dev/null +++ b/app/lib/schedule.rb @@ -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 diff --git a/app/lib/tasks/rates.rake b/app/lib/tasks/rates.rake index ff03a99..6ff02f2 100644 --- a/app/lib/tasks/rates.rake +++ b/app/lib/tasks/rates.rake @@ -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 diff --git a/app/lib/tasks/test.rake b/app/lib/tasks/test.rake index 1e65fae..0a1c5ba 100644 --- a/app/lib/tasks/test.rake +++ b/app/lib/tasks/test.rake @@ -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] diff --git a/app/spec/bank_spec.rb b/app/spec/bank_spec.rb new file mode 100644 index 0000000..6e155ee --- /dev/null +++ b/app/spec/bank_spec.rb @@ -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 diff --git a/docker-compose.override.yml b/docker-compose.override.yml index 82b9bf1..c3e164a 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -1,6 +1,8 @@ version: '3' services: web: + build: + context: ./app environment: RACK_ENV: development VIRTUAL_HOST: localhost diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index bafccaf..755af93 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -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 diff --git a/docker-compose.yml b/docker-compose.yml index a37f5d0..09993f7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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: