Skip to content

Commit 173d888

Browse files
author
Vidas P
committed
Cache worker status for multiple browsers(tabs)
1 parent 7feab24 commit 173d888

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

app/controllers/worker_status_controller.rb

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,27 @@ def show
44

55
since_id = params[:since_id].presence || 0
66

7-
messages = current_user.messages.where('id > ?', since_id)
7+
cache_key = "worker_status/since_id-#{since_id}-user_id-#{current_user.id}"
88

9-
result = messages.select('COUNT(id) AS count', 'MAX(id) AS max_id')
10-
.reorder(Arel.sql('min(created_at)')).first
11-
count, max_id = result.count, result.max_id
9+
worker_status = Rails.cache.fetch(cache_key, expires_in: 2.seconds) do
10+
messages = current_user.messages.where('id > ?', since_id)
1211

13-
# Max aggregate only works if there are any new messages.
14-
max_id = since_id if count == 0
12+
result = messages.select('COUNT(id) AS count', 'MAX(id) AS max_id')
13+
.reorder(Arel.sql('min(created_at)')).first
14+
count, max_id = result.count, result.max_id
1515

16-
render json: {
17-
pending: Delayed::Job.pending.where('run_at <= ?', start).count,
18-
awaiting_retry: Delayed::Job.awaiting_retry.count,
19-
recent_failures: Delayed::Job.failed_jobs.where('failed_at > ?', 5.days.ago).count,
20-
message_count: count,
21-
max_id: max_id || 0,
22-
compute_time: Time.now - start
23-
}
16+
# Max aggregate only works if there are any new messages.
17+
max_id = since_id if count == 0
18+
19+
{
20+
pending: Delayed::Job.pending.where('run_at <= ?', start).count,
21+
awaiting_retry: Delayed::Job.awaiting_retry.count,
22+
recent_failures: Delayed::Job.failed_jobs.where('failed_at > ?', 5.days.ago).count,
23+
message_count: count,
24+
max_id: max_id || 0
25+
}
26+
end
27+
28+
render json: worker_status
2429
end
2530
end

0 commit comments

Comments
 (0)