From b5659259bdcfe957c4f2399c443e9cbfbfe81b67 Mon Sep 17 00:00:00 2001 From: Hui Zhang Date: Wed, 15 May 2019 19:00:57 -0700 Subject: [PATCH 1/4] 1. Add rake and job files to support resend article recruit email 2. Add resend article recruit email to event and publication model 3. Add resend method in mailer 4. fix error in calling update method in publication model --- app/jobs/resend_email_article_recruit_job.rb | 40 ++++++++++++++++++++ app/mailers/article_recruit_mailer.rb | 6 +++ app/models/event.rb | 7 +++- app/models/publication.rb | 13 +++++-- lib/tasks/resend_article_recruit_email.rake | 14 +++++++ 5 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 app/jobs/resend_email_article_recruit_job.rb create mode 100644 lib/tasks/resend_article_recruit_email.rake diff --git a/app/jobs/resend_email_article_recruit_job.rb b/app/jobs/resend_email_article_recruit_job.rb new file mode 100644 index 0000000..da1ae52 --- /dev/null +++ b/app/jobs/resend_email_article_recruit_job.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +## +# Job for re-sending article recruit email +class ResendEmailArticleRecruitJob < ApplicationJob + # Defaults to 0 + # job_options retry: 0 + + def perform(publication:, current_user: nil, previous_event: nil) + system_email = ENV['ED2_EMAIL_FROM'] + event = previous_event || Event.create(Event::RESEND_EMAIL_ARTICLE_RECRUIT.merge(restartable: false, status: Event::STARTED[:name])) + + event.update( + publication: publication, + message: "Initiated by #{current_user ? current_user[:email] : system_email }", + restartable: false, + status: Event::STARTED[:name] + ) + + current_user.events << event if current_user + + emails = [system_email.split(',')].flatten + emails << current_user[:email] if current_user + emails << publication.author_publications.map(&:email) if Rails.env.production? + emails.each do |email| + logger.debug "ResendEmailArticleRecruitJob.perform: Emailing recruitment email to #{email}" + ArticleRecruitMailer.with(email: email, publication: publication).resend_recruit_email.deliver_now + end + event.completed( + message: "Resend Article author recruitment email completed by #{current_user ? current_user[:email] : system_email} at #{Time.now}", + restartable: false, + status: Event::EMAIL[:name] + ) + publication.await_claim! + rescue => e + msg = 'EmailArticleRecruitJob.perform' + NotificationManager.log_exception(logger, msg, e) + event.error(restartable: true, message: "#{msg} : #{e.message}") if event + end +end diff --git a/app/mailers/article_recruit_mailer.rb b/app/mailers/article_recruit_mailer.rb index 93115cc..4130c30 100644 --- a/app/mailers/article_recruit_mailer.rb +++ b/app/mailers/article_recruit_mailer.rb @@ -10,4 +10,10 @@ def recruit_email @author_publication = @publication.author_publications.where(email: @email).first mail(to: @email, subject: "Oregon State University Library invites you to deposit your recent publication: #{@publication.web_of_science_source_record[:uid]}") end + + def resend_recruit_email + @publication = params[:publication] + @author_publication = @publication.author_publications.where(email: @email).first + mail(to: @email, subject: "REMINDER: Oregon State University Library invites you to deposit your recent publication: #{@publication.web_of_science_source_record[:uid]}") + end end diff --git a/app/models/event.rb b/app/models/event.rb index f8daf20..510d2b4 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -17,7 +17,8 @@ class Event < ActiveRecord::Base email_published_work: EmailPublishedWorkJob.to_s, fetch_authors_directory_api: FetchAuthorsDirectoryApiJob.to_s, fetch_wos_content: FetchWosContentJob.to_s, - email_article_recruit: EmailArticleRecruitJob.to_s + email_article_recruit: EmailArticleRecruitJob.to_s, + resend_email_article_recruit: ResendEmailArticleRecruitJob } # Statuses @@ -53,6 +54,10 @@ class Event < ActiveRecord::Base status: STARTED[:name], restartable: true, restartable_state: JSON.dump(method: RESTARTABLE_METHODS[:email_article_recruit]) } + RESEND_EMAIL_ARTICLE_RECRUIT = { name: 'Resend article recruit with email saved in AuthorPublication', + status: STARTED[:name], + restartable: true, + restartable_state: JSON.dump(method: RESTARTABLE_METHODS[:email_article_recruit]) } def completed(options = nil) save_record(options.merge(status: options[:status] || COMPLETED[:name])) diff --git a/app/models/publication.rb b/app/models/publication.rb index 69eea82..72e59e8 100644 --- a/app/models/publication.rb +++ b/app/models/publication.rb @@ -85,6 +85,7 @@ def add_author_emails(people) state :publication_exists state :publishing_failed state :published + state :resend_recruiting_authors event :fetch_authors do after do @@ -100,14 +101,14 @@ def add_author_emails(people) transitions from: :fetching_authors, to: :recruiting_authors, guard: :completed_fetching_authors? end event :await_claim do - transitions from: :recruiting_authors, to: :awaiting_claim + transitions from: [:recruiting_authors, :resend_recruiting_authors], to: :awaiting_claim end event :await_attachments do transitions from: :awaiting_claim, to: :awaiting_attachments end event :publish_exists do after do - update(pub_at: Time.now) + update(ids, pub_at: Time.now) end transitions from: [:awaiting_attachments, :publishing_failed], to: :publication_exists end @@ -116,10 +117,16 @@ def add_author_emails(people) end event :publish do after do - update(pub_at: Time.now) + update(ids, pub_at: Time.now) end transitions from: [:awaiting_attachments, :publishing_failed, :publication_exists], to: :published, guard: :ready_to_publish? end + event :resend_recruiting_authors do + after do + ResendEmailArticleRecruitJob.perform_later(publication: self) + end + transitions from: :await_claim, to: :resend_recruiting_authors + end end private diff --git a/lib/tasks/resend_article_recruit_email.rake b/lib/tasks/resend_article_recruit_email.rake new file mode 100644 index 0000000..9972701 --- /dev/null +++ b/lib/tasks/resend_article_recruit_email.rake @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +namespace :resend_article_recruit_email do + desc 'Re-send article recruit email for publications without attachment' + task resendarticlerecruitemail: :environment do + Publication.all.each do |pub| + # Find publications without attachment + if pub.publication_files.empty? + # Call re-send article recruit email + ResendEmailArticleRecruitJob.perform_later(pub) + end + end + end +end \ No newline at end of file From 23410fb86aa562cb5b19103700033e86f6860a68 Mon Sep 17 00:00:00 2001 From: Hui Zhang Date: Wed, 15 May 2019 19:42:33 -0700 Subject: [PATCH 2/4] Update CircleCI config to fix 404 error for debian --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 5ff8b55..7537f3f 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -22,7 +22,7 @@ jobs: echo "deb http://http.debian.net/debian jessie-backports main" | sudo tee -a /etc/apt/sources.list sudo apt-get update -qq sudo apt-get upgrade -qq - sudo apt-get install -y -f software-properties-common build-essential default-libmysqlclient-dev mysql-client nodejs make apt-utils + sudo apt-get update && sudo apt-get install -y -f software-properties-common build-essential default-libmysqlclient-dev mysql-client nodejs make apt-utils - checkout - restore_cache: key: gemfile-{{ checksum "Gemfile.lock" }} From 0b4b5e71132edc7a22b6f7de7f49b3870e881e4d Mon Sep 17 00:00:00 2001 From: Hui Zhang Date: Wed, 15 May 2019 19:51:28 -0700 Subject: [PATCH 3/4] fix for CircleCI --- .circleci/config.yml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 7537f3f..b5fd995 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -19,10 +19,13 @@ jobs: - run: name: Update Debian Packages command: | - echo "deb http://http.debian.net/debian jessie-backports main" | sudo tee -a /etc/apt/sources.list + echo "deb http://archive.debian.org/debian/ jessie-backports main" | sudo tee -a /etc/apt/sources.list + echo "deb-src http://archive.debian.org/debian/ jessie-backports main" | sudo tee -a /etc/apt/sources.list + echo "Acquire::Check-Valid-Until false;" | sudo tee -a /etc/apt/apt.conf.d/10-nocheckvalid + echo 'Package: *\nPin: origin "archive.debian.org"\nPin-Priority: 500' | sudo tee -a /etc/apt/preferences.d/10-archive-pin sudo apt-get update -qq sudo apt-get upgrade -qq - sudo apt-get update && sudo apt-get install -y -f software-properties-common build-essential default-libmysqlclient-dev mysql-client nodejs make apt-utils + sudo apt-get install -y -f software-properties-common build-essential default-libmysqlclient-dev mysql-client nodejs make apt-utils - checkout - restore_cache: key: gemfile-{{ checksum "Gemfile.lock" }} From 0d1c1ea540c7fe0f89019f1c9346616bcda11fee Mon Sep 17 00:00:00 2001 From: Hui Zhang Date: Sun, 19 May 2019 21:22:48 -0700 Subject: [PATCH 4/4] update publication model --- app/models/event.rb | 4 ++-- app/models/publication.rb | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/app/models/event.rb b/app/models/event.rb index 510d2b4..831ff17 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -18,7 +18,7 @@ class Event < ActiveRecord::Base fetch_authors_directory_api: FetchAuthorsDirectoryApiJob.to_s, fetch_wos_content: FetchWosContentJob.to_s, email_article_recruit: EmailArticleRecruitJob.to_s, - resend_email_article_recruit: ResendEmailArticleRecruitJob + resend_email_article_recruit: ResendEmailArticleRecruitJob.to_s } # Statuses @@ -57,7 +57,7 @@ class Event < ActiveRecord::Base RESEND_EMAIL_ARTICLE_RECRUIT = { name: 'Resend article recruit with email saved in AuthorPublication', status: STARTED[:name], restartable: true, - restartable_state: JSON.dump(method: RESTARTABLE_METHODS[:email_article_recruit]) } + restartable_state: JSON.dump(method: RESTARTABLE_METHODS[:resend_email_article_recruit]) } def completed(options = nil) save_record(options.merge(status: options[:status] || COMPLETED[:name])) diff --git a/app/models/publication.rb b/app/models/publication.rb index 72e59e8..44cd2fe 100644 --- a/app/models/publication.rb +++ b/app/models/publication.rb @@ -80,12 +80,12 @@ def add_author_emails(people) state :initialized, initial: true state :fetching_authors state :recruiting_authors + state :resending_recruiting_authors state :awaiting_claim state :awaiting_attachments state :publication_exists state :publishing_failed state :published - state :resend_recruiting_authors event :fetch_authors do after do @@ -100,15 +100,21 @@ def add_author_emails(people) end transitions from: :fetching_authors, to: :recruiting_authors, guard: :completed_fetching_authors? end + event :resend_recruit_authors do + after do + ResendEmailArticleRecruitJob.perform_later(publication: self) + end + transitions from: :await_claim, to: :resending_recruiting_authors + end event :await_claim do - transitions from: [:recruiting_authors, :resend_recruiting_authors], to: :awaiting_claim + transitions from: [:recruiting_authors, :resending_recruiting_authors], to: :awaiting_claim end event :await_attachments do transitions from: :awaiting_claim, to: :awaiting_attachments end event :publish_exists do after do - update(ids, pub_at: Time.now) + update(pub_at: Time.now) end transitions from: [:awaiting_attachments, :publishing_failed], to: :publication_exists end @@ -117,16 +123,10 @@ def add_author_emails(people) end event :publish do after do - update(ids, pub_at: Time.now) + update(pub_at: Time.now) end transitions from: [:awaiting_attachments, :publishing_failed, :publication_exists], to: :published, guard: :ready_to_publish? end - event :resend_recruiting_authors do - after do - ResendEmailArticleRecruitJob.perform_later(publication: self) - end - transitions from: :await_claim, to: :resend_recruiting_authors - end end private