Class: CommandTower::ServiceBase

Inherits:
Object
  • Object
show all
Includes:
ArgumentValidation, ServiceLogging, Interactor
Defined in:
app/services/command_tower/service_base.rb

Defined Under Namespace

Classes: ArgumentValidationError, CompositionValidationError, ConfigurationError, DefaultValueError, KeyValidationError, NameConflictError, NestedOneOfError, OneOfError, ServiceBaseError, ValidationError

Constant Summary collapse

ON_ARGUMENT_VALIDATION =
[
  DEFAULT_VALIDATION = :raise,
  :fail_early,
  :log,
]

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ArgumentValidation

included

Methods included from ServiceLogging

#aletered_message, #class_name, #log, #log_error, #log_info, #log_prefix, #log_warn, #logger, #service_id

Class Method Details

.inherited(subclass) ⇒ Object



29
30
31
32
33
34
35
# File 'app/services/command_tower/service_base.rb', line 29

def self.inherited(subclass)
  # Add the base logging to the subclass.
  # Since this is done at inheritance time it should always be the first and last hook to run.
  subclass.around(:service_base_logging)
  subclass.around(:internal_validate)
  subclass.after(:sanitize_params)
end

Instance Method Details

#internal_validate(interactor) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/services/command_tower/service_base.rb', line 41

def internal_validate(interactor)
  # call validate that is overridden from child
  begin
    validate! # custom validations defined on the child class
    run_validations! # ArgumentValidation's based on defined settings on child
  rescue StandardError => e
    log_error("Error during validation. #{e.message}")
    raise
  end

  # call interactor
  interactor.call
end

#service_base_logging(interactor) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/services/command_tower/service_base.rb', line 55

def service_base_logging(interactor)
  beginning_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)

  # Pre processing stats
  log_info("Start")

  # Run the job!
  interactor.call

  # Set status for use in ensure block
  status = :complete

# Capture Interactor::Failure for logging purposes, then reraise
rescue ::Interactor::Failure
  # set status for use in ensure block
  status = :failure

  # Re-raise to let the core Interactor handle this
  raise
# Capture exception explicitly for logging purposes, then reraise
rescue ::Exception => e
  # set status for use in ensure block
  status = :error

  # Log error
  log_error("Error #{e.class.name}")

  raise
ensure
  # Always log how long it took along with a status
  finished_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  elapsed = ((finished_time - beginning_time) * 1000).round(2)
  log_info("Finished with [#{status}]...elapsed #{elapsed}ms")
end

#validate!Object



37
38
39
# File 'app/services/command_tower/service_base.rb', line 37

def validate!
  # overload from child
end