Class: Amigo
- Inherits:
-
Object
- Object
- Amigo
- Defined in:
- lib/amigo.rb,
lib/amigo/job.rb,
lib/amigo/router.rb,
lib/amigo/version.rb,
lib/amigo/audit_logger.rb,
lib/amigo/spec_helpers.rb,
lib/amigo/scheduled_job.rb,
lib/amigo/deprecated_jobs.rb
Overview
Put jobs here to die. If you just remove a job in Sidekiq, it may be queued up (like if it’s scheduled or retrying), and will fail if the class does not exist.
So, make the class exist, but noop so it won’t be scheduled and won’t be retried. Then it can be deleted later.
Defined Under Namespace
Modules: DeprecatedJobs, Job, ScheduledJob, SpecHelpers Classes: AuditLogger, Event, Router
Constant Summary collapse
- VERSION =
"1.0.0"
Class Attribute Summary collapse
-
.log_callback ⇒ Object
Proc called with [job, level, message, params].
-
.on_publish_error ⇒ Object
A single callback to be run when an event publication errors.
-
.registered_jobs ⇒ Object
Every subclass of Amigo::Job and Amigo::ScheduledJob goes here.
-
.structured_logging ⇒ Object
Returns the value of attribute structured_logging.
-
.subscribers ⇒ Object
An Array of callbacks to be run when an event is published.
-
.synchronous_mode ⇒ Object
If true, perform event work synchronously rather than asynchronously.
Class Method Summary collapse
- ._subscriber(event) ⇒ Object
-
.install_amigo_jobs ⇒ Object
Install Amigo so that every publish will be sent to the AuditLogger job and will invoke the relevant jobs in registered_jobs via the Router job.
- .log(job, level, message, params) ⇒ Object
-
.publish(eventname, *payload) ⇒ Object
Publish an event with the specified
eventname
andpayload
to any configured publishers. -
.register_subscriber(&block) ⇒ Object
Register a hook to be called when an event is sent.
-
.registered_event_jobs ⇒ Object
Return an array of all Job subclasses that respond to event publishing (have patterns).
-
.registered_scheduled_jobs ⇒ Object
Return an array of all Job subclasses that are scheduled (have intervals).
- .reset_logging ⇒ Object
- .unregister_subscriber(block_ref) ⇒ Object
Class Attribute Details
.log_callback ⇒ Object
Proc called with [job, level, message, params]. By default, logs to the job’s logger (or Sidekiq’s if job is nil). If structured_logging is true, the message will be an ‘event’ without any dynamic info, if false, the params will be rendered into the message so are suitable for unstructured logging.
113 114 115 |
# File 'lib/amigo.rb', line 113 def log_callback @log_callback end |
.on_publish_error ⇒ Object
A single callback to be run when an event publication errors.
140 141 142 |
# File 'lib/amigo.rb', line 140 def on_publish_error @on_publish_error end |
.registered_jobs ⇒ Object
Every subclass of Amigo::Job and Amigo::ScheduledJob goes here. It is used for routing and testing isolated jobs.
134 135 136 |
# File 'lib/amigo.rb', line 134 def registered_jobs @registered_jobs end |
.structured_logging ⇒ Object
Returns the value of attribute structured_logging.
107 108 109 |
# File 'lib/amigo.rb', line 107 def structured_logging @structured_logging end |
.subscribers ⇒ Object
An Array of callbacks to be run when an event is published.
137 138 139 |
# File 'lib/amigo.rb', line 137 def subscribers @subscribers end |
.synchronous_mode ⇒ Object
If true, perform event work synchronously rather than asynchronously. Only useful for testing.
130 131 132 |
# File 'lib/amigo.rb', line 130 def synchronous_mode @synchronous_mode end |
Class Method Details
._subscriber(event) ⇒ Object
188 189 190 191 192 |
# File 'lib/amigo.rb', line 188 def _subscriber(event) event_json = event.as_json Amigo::AuditLogger.perform_async(event_json) Amigo::Router.perform_async(event_json) end |
.install_amigo_jobs ⇒ Object
Install Amigo so that every publish will be sent to the AuditLogger job and will invoke the relevant jobs in registered_jobs via the Router job.
182 183 184 185 186 |
# File 'lib/amigo.rb', line 182 def install_amigo_jobs return self.register_subscriber do |ev| self._subscriber(ev) end end |
.log(job, level, message, params) ⇒ Object
120 121 122 123 124 125 126 |
# File 'lib/amigo.rb', line 120 def log(job, level, , params) if self.structured_logging paramstr = params.map { |k, v| "#{k}=#{v}" }.join(" ") = "#{} #{paramstr}" end self.log_callback[job, level, , params] end |
.publish(eventname, *payload) ⇒ Object
Publish an event with the specified eventname
and payload
to any configured publishers.
144 145 146 147 148 149 150 151 152 153 |
# File 'lib/amigo.rb', line 144 def publish(eventname, *payload) ev = Event.new(SecureRandom.uuid, eventname, payload) self.subscribers.to_a.each do |hook| hook.call(ev) rescue StandardError => e self.log(nil, :error, "amigo_subscriber_hook_error", error: e, hook: hook, event: ev) self.on_publish_error.call(e) end end |
.register_subscriber(&block) ⇒ Object
Register a hook to be called when an event is sent.
156 157 158 159 160 161 |
# File 'lib/amigo.rb', line 156 def register_subscriber(&block) raise LocalJumpError, "no block given" unless block self.log nil, :info, "amigo_installed_subscriber", block: block self.subscribers << block return block end |
.registered_event_jobs ⇒ Object
Return an array of all Job subclasses that respond to event publishing (have patterns).
168 169 170 |
# File 'lib/amigo.rb', line 168 def registered_event_jobs return self.registered_jobs.select(&:event_job?) end |
.registered_scheduled_jobs ⇒ Object
Return an array of all Job subclasses that are scheduled (have intervals).
173 174 175 |
# File 'lib/amigo.rb', line 173 def registered_scheduled_jobs return self.registered_jobs.select(&:scheduled_job?) end |
.reset_logging ⇒ Object
115 116 117 118 |
# File 'lib/amigo.rb', line 115 def reset_logging self.log_callback = ->(job, level, msg, _params) { (job || Sidekiq).logger.send(level, msg) } self.structured_logging = false end |
.unregister_subscriber(block_ref) ⇒ Object
163 164 165 |
# File 'lib/amigo.rb', line 163 def unregister_subscriber(block_ref) self.subscribers.delete(block_ref) end |