states.rb revision 29df54c6935a6dc6ebd194518adf1b1a2c19b876
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen# states:
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen# * pending
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen# * downloading
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen# * processing
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen# * failed
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen# * done
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenmodule OntologyVersion::States
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen extend ActiveSupport::Concern
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen include StateUpdater
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen TERMINAL_STATES = %w(failed done)
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen included do
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen after_save :after_update_state, if: :change_applicable?
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen end
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen def change_applicable?
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen state_changed? &&
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen parent.nil?
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen end
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen def state_message
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen msg = [state]
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen if last_error
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen lines = last_error.split("\n")
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen if (ind=lines.index("*** Error:")) and (out = lines[ind+1]).present?
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen i = ind+2
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen while lines[i] and !lines[i].include?("hets: user error") do
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen out += " "+lines[i]
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i+=1
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen end
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen msg << out.sub(URI.regexp,"...").sub(/ \/[A-Za-z0-9\/.]*/," ...")
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen elsif last_error.include?("exited with status")
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen msg << last_error[0,50]+" ... "+last_error.match("exited with status.*")[0]
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen else
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen msg << lines.first
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen end
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen end
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen msg.join(": ")
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen end
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen protected
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen def after_update_state
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen ontology.state = state.to_s
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen ontology.save!
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen if ontology.distributed?
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen ontology.children.update_all state: ontology.state
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen end
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen end
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainenend
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen