Class: Jobs::StreamTopicSummary

Inherits:
Base
  • Object
show all
Defined in:
app/jobs/regular/stream_topic_summary.rb

Instance Method Summary collapse

Methods inherited from Base

acquire_cluster_concurrency_lock!, clear_cluster_concurrency_lock!, cluster_concurrency, cluster_concurrency_redis_key, delayed_perform, #error_context, get_cluster_concurrency, #last_db_duration, #log, #perform, #perform_immediately

Instance Method Details

#execute(args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/jobs/regular/stream_topic_summary.rb', line 7

def execute(args)
  return unless topic = Topic.find_by(id: args[:topic_id])
  return unless user = User.find_by(id: args[:user_id])

  strategy = Summarization::Base.selected_strategy
  return if strategy.nil? || !Summarization::Base.can_see_summary?(topic, user)

  guardian = Guardian.new(user)
  return unless guardian.can_see?(topic)

  opts = args[:opts] || {}

  streamed_summary = +""
  start = Time.now

  summary =
    TopicSummarization
      .new(strategy)
      .summarize(topic, user, opts) do |partial_summary|
        streamed_summary << partial_summary

        # Throttle updates.
        if (Time.now - start > 0.5) || Rails.env.test?
          payload = { done: false, topic_summary: { summarized_text: streamed_summary } }
          publish_update(topic, user, payload)
          start = Time.now
        end
      end

  publish_update(
    topic,
    user,
    TopicSummarySerializer.new(summary, { scope: guardian }).as_json.merge(done: true),
  )
end