From 08e64aebfe1ee216c7eabda4bd92cda1b4272f2c Mon Sep 17 00:00:00 2001 From: Adrien Date: Wed, 13 Oct 2021 18:02:06 +0200 Subject: [PATCH] feature: rake task to seed/update pokemons alongside with sidekiq --- Gemfile | 19 +++++-------------- Gemfile.lock | 28 ++++++++++++++++++++++++++++ app/workers/hard_worker.rb | 22 ++++++++++++++++++++++ app/workers/pokemon_worker.rb | 7 +++++++ config/routes.rb | 4 +++- lib/tasks/pokemons/update.rake | 11 +++++++++++ 6 files changed, 76 insertions(+), 15 deletions(-) create mode 100644 app/workers/hard_worker.rb create mode 100644 app/workers/pokemon_worker.rb create mode 100644 lib/tasks/pokemons/update.rake diff --git a/Gemfile b/Gemfile index 6d6eb59..14e4945 100644 --- a/Gemfile +++ b/Gemfile @@ -3,26 +3,14 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" } ruby '3.0.2' -# Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main' gem 'rails', '~> 6.1.4', '>= 6.1.4.1' -# Use sqlite3 as the database for Active Record gem 'sqlite3', '~> 1.4' -# Use Puma as the app server gem 'puma', '~> 5.0' -# Use SCSS for stylesheets gem 'sass-rails', '>= 6' -# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 2.7' -# Use Redis adapter to run Action Cable in production -# gem 'redis', '~> 4.0' -# Use Active Model has_secure_password -# gem 'bcrypt', '~> 3.1.7' - -# Use Active Storage variant -# gem 'image_processing', '~> 1.2' - -# Reduces boot times through caching; required in config/boot.rb gem 'bootsnap', '>= 1.4.4', require: false +gem 'active_model_serializers', '~> 0.10.0' +gem 'sidekiq' group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console @@ -46,6 +34,9 @@ group :test do gem 'selenium-webdriver' # Easy installation and use of web drivers to run system tests with browsers gem 'webdrivers' + gem 'rexml' # necessary if using ruby 3+ + gem 'webmock' + gem 'vcr' end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem diff --git a/Gemfile.lock b/Gemfile.lock index d7499c5..166dc73 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -39,6 +39,11 @@ GEM erubi (~> 1.4) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.1, >= 1.2.0) + active_model_serializers (0.10.12) + actionpack (>= 4.1, < 6.2) + activemodel (>= 4.1, < 6.2) + case_transform (>= 0.2) + jsonapi-renderer (>= 0.1.1.beta1, < 0.3) activejob (6.1.4.1) activesupport (= 6.1.4.1) globalid (>= 0.3.6) @@ -75,17 +80,24 @@ GEM rack-test (>= 0.6.3) regexp_parser (>= 1.5, < 3.0) xpath (~> 3.2) + case_transform (0.2) + activesupport childprocess (3.0.0) concurrent-ruby (1.1.9) + connection_pool (2.2.5) + crack (0.4.5) + rexml crass (1.0.6) erubi (1.10.0) ffi (1.15.4) globalid (0.5.2) activesupport (>= 5.0) + hashdiff (1.0.1) i18n (1.8.10) concurrent-ruby (~> 1.0) jbuilder (2.11.2) activesupport (>= 5.0.0) + jsonapi-renderer (0.2.2) listen (3.7.0) rb-fsevent (~> 0.10, >= 0.10.3) rb-inotify (~> 0.9, >= 0.9.10) @@ -141,7 +153,9 @@ GEM rb-fsevent (0.11.0) rb-inotify (0.10.1) ffi (~> 1.0) + redis (4.4.0) regexp_parser (2.1.1) + rexml (3.2.5) rubyzip (2.3.2) sass-rails (6.0.0) sassc-rails (~> 2.1, >= 2.1.1) @@ -156,6 +170,10 @@ GEM selenium-webdriver (3.142.7) childprocess (>= 0.5, < 4.0) rubyzip (>= 1.2.2) + sidekiq (6.2.2) + connection_pool (>= 2.2.2) + rack (~> 2.0) + redis (>= 4.2.0) spring (3.0.0) sprockets (4.0.2) concurrent-ruby (~> 1.0) @@ -169,6 +187,7 @@ GEM tilt (2.0.10) tzinfo (2.0.4) concurrent-ruby (~> 1.0) + vcr (6.0.0) web-console (4.1.0) actionview (>= 6.0.0) activemodel (>= 6.0.0) @@ -178,6 +197,10 @@ GEM nokogiri (~> 1.6) rubyzip (>= 1.3.0) selenium-webdriver (>= 3.0, < 4.0) + webmock (3.14.0) + addressable (>= 2.8.0) + crack (>= 0.3.2) + hashdiff (>= 0.4.0, < 2.0.0) websocket-driver (0.7.5) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.5) @@ -189,6 +212,7 @@ PLATFORMS x86_64-linux DEPENDENCIES + active_model_serializers (~> 0.10.0) bootsnap (>= 1.4.4) byebug capybara (>= 3.26) @@ -197,13 +221,17 @@ DEPENDENCIES puma (~> 5.0) rack-mini-profiler (~> 2.0) rails (~> 6.1.4, >= 6.1.4.1) + rexml sass-rails (>= 6) selenium-webdriver + sidekiq spring sqlite3 (~> 1.4) tzinfo-data + vcr web-console (>= 4.1.0) webdrivers + webmock RUBY VERSION ruby 3.0.2p107 diff --git a/app/workers/hard_worker.rb b/app/workers/hard_worker.rb new file mode 100644 index 0000000..c011a1f --- /dev/null +++ b/app/workers/hard_worker.rb @@ -0,0 +1,22 @@ +class HardWorker + include Sidekiq::Worker + + def perform id + data = Pokeapi::Pokemon.get id + + created_pokemon = Pokemon.find_or_create_by(name: data["name"]) do |p| + p.base_experience = data["base_experience"] + p.height = data["height"] + p.is_default = data["is_default"] + p.order = data["order"] + p.weight = data["weight"] + end + + data["types"].map do |item| + type = Type.find_or_create_by(name: item["type"]["name"]) + created_pokemon.pokemon_types.find_or_create_by(type_id: type.id) do |pt| + pt.slot = item["slot"] + end + end + end +end diff --git a/app/workers/pokemon_worker.rb b/app/workers/pokemon_worker.rb new file mode 100644 index 0000000..10feb18 --- /dev/null +++ b/app/workers/pokemon_worker.rb @@ -0,0 +1,7 @@ +class PokemonWorker + include Sidekiq::Worker + + def perform(*args) + # Do something + end +end diff --git a/config/routes.rb b/config/routes.rb index c06383a..1c65f1d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,5 @@ +require 'sidekiq/web' + Rails.application.routes.draw do - # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html + mount Sidekiq::Web => "/sidekiq" end diff --git a/lib/tasks/pokemons/update.rake b/lib/tasks/pokemons/update.rake new file mode 100644 index 0000000..8ef6964 --- /dev/null +++ b/lib/tasks/pokemons/update.rake @@ -0,0 +1,11 @@ +require 'pokeapi' + +namespace :pokemons do + task update: :environment do + pokemons = Pokeapi::Pokemon.get_all + + pokemons.each do |pokemon| + HardWorker.perform_async pokemon["url"].split("/").last + end + end +end