Skip to content

Commit

Permalink
Execute LCSS job when have new molecule (#770)
Browse files Browse the repository at this point in the history
* Execute LCSS job when have new molecule

* Execute LCSS job when have new molecule

create a job to get single lcss data

refactor to use active record callback

Co-authored-by: Danny Truong <[email protected]>
  • Loading branch information
Allenskywalker92 and Danny Truong authored Jul 4, 2022
1 parent 87cf147 commit 6566850
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,4 @@ vendor/cache/*
.devcontainer
.docker-sync/

public/sprite.png
11 changes: 11 additions & 0 deletions app/jobs/pubchem_single_lcss_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

# Job to update molecule info for molecules with no LCSS
# associated LCSS (molecule tag) is updated if cid found in PC db
class PubchemSingleLcssJob < ApplicationJob
queue_as :single_pubchem_lcss

def perform(molecule)
molecule.pubchem_lcss
end
end
12 changes: 12 additions & 0 deletions app/models/molecule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class Molecule < ApplicationRecord

before_save :sanitize_molfile
after_create :create_molecule_names
after_create :get_lcss
skip_callback :save, before: :sanitize_molfile, if: :skip_sanitize_molfile

validates_uniqueness_of :inchikey, scope: :is_partial
Expand Down Expand Up @@ -94,6 +95,7 @@ def self.find_or_create_by_molfile(molfile, **babel_info)
molecule.molfile = is_partial && partial_molfile || molfile
molecule.assign_molecule_data(babel_info, pubchem_info)
end

molecule.ob_log = babel_info[:ob_log]
molecule
end
Expand Down Expand Up @@ -224,6 +226,16 @@ def create_molecule_names
molecule_names.create(name: sum_formular, description: 'sum_formular')
end

def get_lcss
delayed_jobs = Delayed::Job.where(queue: 'single_pubchem_lcss')
if delayed_jobs.empty?
PubchemSingleLcssJob.perform_later self
else
last_job = delayed_jobs.last
PubchemSingleLcssJob.set(run_at: last_job.created_at + 1.seconds).perform_later self
end
end

def create_molecule_name_by_user(new_name, user_id)
return unless unique_molecule_name(new_name)
molecule_names
Expand Down

0 comments on commit 6566850

Please sign in to comment.