Skip to content

Commit

Permalink
Add Whenever
Browse files Browse the repository at this point in the history
  • Loading branch information
the-teacher committed Jan 21, 2023
1 parent 8782ca4 commit e1caf3f
Show file tree
Hide file tree
Showing 16 changed files with 151 additions and 20 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ gem "sanitize", "6.0.0"
# Pagination
gem "kaminari", "1.2.2"

# Cron based periodical jobs
gem "whenever", "1.0.0", require: false

group :development, :test do
# fake data for development and testing
gem "faker", "3.1.0"
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ GEM
activesupport (>= 5.2)
elasticsearch (>= 7.12.0, < 7.14.0)
elasticsearch-dsl
chronic (0.10.2)
concurrent-ruby (1.1.10)
connection_pool (2.3.0)
crass (1.0.6)
Expand Down Expand Up @@ -303,6 +304,8 @@ GEM
websocket-driver (0.7.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
whenever (1.0.0)
chronic (>= 0.6.3)
xpath (3.2.0)
nokogiri (~> 1.8)
zeitwerk (2.6.6)
Expand Down Expand Up @@ -333,6 +336,7 @@ DEPENDENCIES
tzinfo-data
web-console (= 4.2.0)
webdrivers (= 5.2.0)
whenever (= 1.0.0)

RUBY VERSION
ruby 3.2.0p0
Expand Down
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ Since now if you have Ruby and Docker then you have working Rails environment in
<td><b><a href="https://www.ruby-toolbox.com/categories/pagination">Kaminari</a></b></td>
<td>Pagination solution</td>
</tr>
<tr>
<td><img width="100px" alt="whenever" src="docs/whenever.png"></td>
<td><b><a href="https://www.ruby-toolbox.com/categories/scheduling">whenever</a></b></td>
<td>Linux Cron based periodical tasks</td>
</tr>
</tbody>
</table>

Expand All @@ -101,6 +106,11 @@ What I'm going to add...
</tr>
</thead>
<tbody>
<tr>
<td><img width="100px" alt="letter_opener" src="docs/mailcatcher.png"></td>
<td><b><a href="https://www.ruby-toolbox.com/categories/EMail_Fake_Servers">mailcatcher</a></b></td>
<td>Email previwer for development</td>
</tr>
<tr>
<td><img width="100px" alt="devise" src="docs/devise.png"></td>
<td><b><a href="https://www.ruby-toolbox.com/categories/rails_authentication">Devise</b></a></td>
Expand All @@ -116,16 +126,6 @@ What I'm going to add...
<td><b>Devise and Action&nbsp;Mailer</b></td>
<td>Sending emails for account confirmations</td>
</tr>
<tr>
<td><img width="100px" alt="letter_opener" src="docs/mailcatcher.png"></td>
<td><b><a href="https://www.ruby-toolbox.com/categories/EMail_Fake_Servers">mailcatcher</a></b></td>
<td>Email previwer for development</td>
</tr>
<tr>
<td><img width="100px" alt="whenever" src="docs/whenever.png"></td>
<td><b><a href="https://www.ruby-toolbox.com/categories/scheduling">whenever</a></b></td>
<td>Linux Cron based periodical tasks</td>
</tr>
<tr>
<td><img width="100px" alt="rspec" src="docs/rspec.png"></td>
<td><b><a href="https://www.ruby-toolbox.com/categories/testing_frameworks">RSpec</a></b></td>
Expand Down
27 changes: 27 additions & 0 deletions app/models/currency_rate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# rails g model currency_rate
# from:string
# to:string
# rate:float

# rails runner CurrencyRate.get
# rails runner 'CurrencyRate.get("KGS", "USD")'
# rails runner "CurrencyRate.get('USD', 'EUR')"

class CurrencyRate < ApplicationRecord
API = "https://api.exchangerate.host/latest"

def self.get(from = "USD", to = "EUR")
uri = URI(CurrencyRate::API + "?base=#{from}&symbols=#{to}")

response = Net::HTTP.get(uri)
response_obj = JSON.parse(response)

create!(
from: from, to: to,
rate: response_obj['rates'][to]
)

puts "[#{DateTime.now.strftime('%d.%m.%Y %H.%M.%S')}]: " + \
"#{from}:#{to} currency rate: " + response_obj['rates'][to].to_s
end
end
5 changes: 5 additions & 0 deletions bin/exec
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env ruby
require_relative "./helpers.rb"

to_exec = (ARGV[0] || 'containers_information').strip
eval to_exec
23 changes: 22 additions & 1 deletion bin/helpers.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
DELAY = 3
ENV_NAME = ENV.fetch("RAILS_ENV") { 'development' }

$steps_counter = 1
$steps_messages = []
Expand All @@ -23,7 +24,7 @@ def system!(*args)
end

def container_exec(container_name = 'rails', cmd = 'ls')
to_exec = "docker compose -f docker/docker-compose.yml exec #{container_name} #{cmd}"
to_exec = "docker compose -f docker/docker-compose.yml exec -e='RAILS_ENV=#{ENV_NAME}' #{container_name} #{cmd}"
puts to_exec
system(to_exec)
end
Expand Down Expand Up @@ -127,3 +128,23 @@ def sidekiq_stop
puts "Stopping SIDEKIQ"
container_bash_exec('rails', 'pkill -f sidekiq')
end

def cron_start
container_exec('--user root rails', '/etc/init.d/cron start')
end

def cron_stop
container_exec('--user root rails', '/etc/init.d/cron stop')
end

def whenever_start
container_exec('rails', "bundle exec whenever --update-crontab --load-file config/_SCHEDULE.rb -i lucky")
end

def whenever_show
container_exec('rails', 'crontab -l')
end

def whenever_stop
container_exec('rails', 'crontab -r')
end
8 changes: 3 additions & 5 deletions bin/start
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#!/usr/bin/env ruby
require_relative "./helpers.rb"

start_all_containers
rails_install_gems
rails_db_migrate

puma_start
sidekiq_start

cron_start
chewy_index

containers_information
sidekiq_start
puma_start
6 changes: 6 additions & 0 deletions bin/start_all
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env ruby
require_relative "./helpers.rb"

start_all_containers
system('bin/start')
containers_information
3 changes: 1 addition & 2 deletions bin/stop
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
require_relative "./helpers.rb"

puma_stop
cron_stop
sidekiq_stop

containers_information
1 change: 1 addition & 0 deletions bin/stop_all
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env ruby
require_relative "./helpers.rb"
stop_all_containers
containers_information
27 changes: 27 additions & 0 deletions config/_SCHEDULE.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Learn more: http://github.com/javan/whenever
#
# How to initialize whenever
# `bundle exec wheneverize .`
#
# How to start cron service
# as ROOT: /etc/init.d/cron start
#
# How to start whenever
# whenever --update-crontab --load-file config/_SCHEDULE.rb -i lucky
#
# How to see current crontab
# crontab -l

env :GEM_HOME, ENV['GEM_HOME']
set :environment, ENV.fetch("RAILS_ENV") { 'development' }

set :output, {
standard: '/home/lucky/app/log/cron.log',
error: '/home/lucky/app/log/cron.error.log'
}

job_type :rails, "cd :path && :environment_variable=:environment bundle exec rails :task :output"

every 1.minute do
rails "runner \"CurrencyRate.get('USD', 'EUR')\""
end
11 changes: 11 additions & 0 deletions db/migrate/20230121113818_create_currency_rates.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateCurrencyRates < ActiveRecord::Migration[7.0]
def change
create_table :currency_rates do |t|
t.string :from
t.string :to
t.float :rate

t.timestamps
end
end
end
10 changes: 9 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ FROM --platform=$BUILDPLATFORM ruby:3.2
ARG BUILDPLATFORM
RUN echo "$BUILDPLATFORM" > /BUILDPLATFORM

RUN apt-get update && apt-get install --yes \
cron \
vim

SHELL ["/bin/bash", "--login", "-c"]

RUN gem update --system 3.4.2
Expand Down Expand Up @@ -48,7 +52,6 @@ RUN chown -R lucky:lucky /usr/local/bin/ruby
RUN chown -R lucky:lucky /usr/local/bundle

# rails credentials:edit
RUN apt-get update && apt-get install vim --yes
ENV EDITOR="vim"

# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/currency_rates.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
from: MyString
to: MyString
rate: 1.5

two:
from: MyString
to: MyString
rate: 1.5
7 changes: 7 additions & 0 deletions test/models/currency_rate_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class CurrencyRateTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit e1caf3f

Please sign in to comment.