recurring loading working now
parent
5eaad428ff
commit
98b28bcd4f
|
@ -14,7 +14,8 @@ main {
|
|||
padding-left: 3vw;
|
||||
padding-right: 3vw;
|
||||
box-sizing: border-box;
|
||||
margin-top: 4.5em;
|
||||
margin-top: 1.5em;
|
||||
margin-bottom: 2.5em;
|
||||
}
|
||||
|
||||
.dashboard-widget {
|
||||
|
|
|
@ -45,6 +45,7 @@ class SpotifyController < ApplicationController
|
|||
begin
|
||||
token_response = SpotifyClient.new(current_user).token_response_from_code(code)
|
||||
Login.find_or_create_for_response!(current_user, token_response)
|
||||
LoadUserActivitiesJob.perform_later(current_user)
|
||||
true
|
||||
rescue RestClient::Exception, JSON::ParserError => e
|
||||
Rails.logger.error("Spotify callback error: #{e.class} - #{e.message}")
|
||||
|
|
|
@ -1,4 +0,0 @@
|
|||
class LoadActivitiesJob < ApplicationJob
|
||||
def perform
|
||||
end
|
||||
end
|
|
@ -0,0 +1,13 @@
|
|||
class LoadAllActivitiesJob < ApplicationJob
|
||||
def perform
|
||||
User.all.each do |user|
|
||||
begin
|
||||
next if user.logins.alive.empty?
|
||||
LoadUserActivitiesJob.perform_later(user)
|
||||
logger.info("Loading for #{user.id} scheduled")
|
||||
rescue StandardError => e
|
||||
Rails.logger.error("Loading for #{user.id} failed: #{e.class} - #{e.message}")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,32 @@
|
|||
class LoadUserActivitiesJob < ApplicationJob
|
||||
attr_reader :user
|
||||
|
||||
def perform(user)
|
||||
logger.info("Loading for #{user.id} started")
|
||||
@user = user
|
||||
user.logins.alive.each do |login|
|
||||
update_spotify(login)
|
||||
end
|
||||
end
|
||||
|
||||
def update_spotify(login)
|
||||
client = SpotifyClient.new(login)
|
||||
last_spotify_activity_at = latest_activity_iso(login.platform)
|
||||
new_activities = client.load_since(last_spotify_activity_at)
|
||||
create_action = CreateSpotifyActivity.new(user, new_activities)
|
||||
create_action.perform
|
||||
logger.info("Loading for #{user.id} finished with #{new_activities.size} new activities")
|
||||
rescue StandardError => e
|
||||
Rails.logger.error(e)
|
||||
end
|
||||
|
||||
def latest_activity(platform)
|
||||
latest = Activity.where(user: user, platform: platform).order(started_at: :desc).limit(1).last
|
||||
latest.started_at&.to_datetime if latest.present?
|
||||
end
|
||||
|
||||
def latest_activity_iso(platform)
|
||||
latest = latest_activity(platform)
|
||||
latest.strftime("%Q") if latest.present?
|
||||
end
|
||||
end
|
|
@ -8,21 +8,23 @@ class CreateSpotifyActivity
|
|||
|
||||
# rubocop:disable Metrics/AbcSize
|
||||
def perform
|
||||
response['items'].reverse.each do |play|
|
||||
counts = response["items"].size
|
||||
Rails.logger.info("Saving #{counts} activities")
|
||||
response["items"].reverse.each do |play|
|
||||
activity = new_activity
|
||||
activity.item_ref = play['track']['id']
|
||||
artist_names = play['track']['artists'].map { |a| a['name'] }
|
||||
artists = artist_names.join(', ')
|
||||
activity.item_ref = play["track"]["id"]
|
||||
artist_names = play["track"]["artists"].map { |a| a["name"] }
|
||||
artists = artist_names.join(", ")
|
||||
title = "#{artists} - #{play['track']['name']}"
|
||||
activity.item_title = title
|
||||
activity.item_length = play['track']['duration'].to_s
|
||||
activity.started_at = DateTime.parse(play['played_at'])
|
||||
activity.save
|
||||
activity.item_length = play["track"]["duration"].to_s
|
||||
activity.started_at = DateTime.parse(play["played_at"])
|
||||
activity.save!
|
||||
end
|
||||
end
|
||||
# rubocop:enable Metrics/AbcSize
|
||||
|
||||
def new_activity
|
||||
Activity.new(user: user, platform: 'spotify')
|
||||
Activity.new(user: user, platform: "spotify")
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,53 +0,0 @@
|
|||
class LoadPlays
|
||||
attr_reader :user
|
||||
|
||||
def initialize(user)
|
||||
@user = user
|
||||
end
|
||||
|
||||
def perform
|
||||
user.logins.alive.each do |login|
|
||||
if login.spotify?
|
||||
update_spotify(login)
|
||||
elsif login.netflix?
|
||||
update_netflix(login)
|
||||
else
|
||||
puts "login #{login.id}, #{login.type} unknown"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def update_spotify(login)
|
||||
client = Spotify.new(login)
|
||||
last_spotify_activity_at = latest_activity_iso(login.platform)
|
||||
new_activities = client.load_since(last_spotify_activity_at)
|
||||
create_action = CreateSpotifyActivity.new(user, new_activities)
|
||||
create_action.perform
|
||||
rescue StandardError => e
|
||||
Rails.logger.error(e)
|
||||
LogLoadFailedWorker.perform_async(login.id)
|
||||
end
|
||||
|
||||
def update_netflix(login)
|
||||
client = Netflix.new(login)
|
||||
latest_netflix_activity = latest_activity(login.platform)
|
||||
new_activites = client.viewingactivity
|
||||
create_action = CreateNetflixActivity.new(user, latest_netflix_activity, new_activites)
|
||||
create_action.perform
|
||||
rescue StandardError => e
|
||||
Rails.logger.error(e)
|
||||
LogLoadFailedWorker.perform_async(login.id)
|
||||
# Rails.logger.info("trying to update shakti")
|
||||
# ReloadShaktiPath.new.perform
|
||||
end
|
||||
|
||||
def latest_activity(platform)
|
||||
latest = Activity.where(user: user, platform: platform).order(started_at: :desc).limit(1).last
|
||||
latest.started_at&.to_datetime if latest.present?
|
||||
end
|
||||
|
||||
def latest_activity_iso(platform)
|
||||
latest = latest_activity(platform)
|
||||
latest.strftime('%Q') if latest.present?
|
||||
end
|
||||
end
|
|
@ -24,12 +24,12 @@
|
|||
|
||||
<body>
|
||||
<%= render partial: 'layouts/header' %>
|
||||
<%= yield %>
|
||||
<% if notice %>
|
||||
<p class="notice"><%= notice %></p>
|
||||
<% end %>
|
||||
<% if alert %>
|
||||
<p class="alert"><%= alert %></p>
|
||||
<% end %>
|
||||
<%= yield %>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -1,17 +1,11 @@
|
|||
# production:
|
||||
# periodic_cleanup:
|
||||
# class: CleanSoftDeletedRecordsJob
|
||||
# queue: background
|
||||
# args: [ 1000, { batch_size: 500 } ]
|
||||
# schedule: every hour
|
||||
# periodic_command:
|
||||
# command: "SoftDeletedRecord.due.delete_all"
|
||||
# priority: 2
|
||||
# schedule: at 5am every day
|
||||
# development:
|
||||
# periodic_command:
|
||||
# class: TestJob
|
||||
# queue: background
|
||||
# schedule: every minute
|
||||
|
||||
|
||||
production:
|
||||
recurring_load_all_activities:
|
||||
class: LoadAllActivitiesJob
|
||||
queue: background
|
||||
schedule: every 5 minutes
|
||||
|
||||
development:
|
||||
recurring_load_all_activities:
|
||||
class: LoadAllActivitiesJob
|
||||
queue: background
|
||||
schedule: every 15 seconds
|
||||
|
|
Loading…
Reference in New Issue