Skip to content

Commit

Permalink
Wrap ScreenshotTaker in a timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
miharekar committed Jan 3, 2024
1 parent 7dac113 commit 89d60ba
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions app/jobs/screenshot_taker_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,27 @@ class ScreenshotTakerJob < ApplicationJob
def perform(shot)
return if shot.screenshot? || Rails.env.development? || Rails.env.test?

chart = ShotChart.new(shot)
options = Oj.load(File.read("lib/assets/chart_options.json"))
options["xAxis"]["plotLines"] = chart.stages
options["series"] = chart.shot_chart
Timeout.timeout(10) do
chart = ShotChart.new(shot)
options = Oj.load(File.read("lib/assets/chart_options.json"))
options["xAxis"]["plotLines"] = chart.stages
options["series"] = chart.shot_chart

uri = URI.parse("https://export.highcharts.com/")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri, {"Content-Type": "application/json"})
request.body = {options:, type: "image/png"}.to_json
response = http.request(request)
File.write("tmp/screenshot-#{shot.id}.png", response.body, mode: "wb")
uri = URI.parse("https://export.highcharts.com/")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri, {"Content-Type": "application/json"})
request.body = {options:, type: "image/png"}.to_json
response = http.request(request)
File.write("tmp/screenshot-#{shot.id}.png", response.body, mode: "wb")

client = Aws::S3::Client.new
s3_response = client.put_object(
acl: "public-read",
body: File.read("tmp/screenshot-#{shot.id}.png"),
bucket: "visualizer-coffee-shots",
key: "screenshots/#{shot.id}.png"
)
shot.update(s3_etag: s3_response.etag) if s3_response&.etag
client = Aws::S3::Client.new
s3_response = client.put_object(
acl: "public-read",
body: File.read("tmp/screenshot-#{shot.id}.png"),
bucket: "visualizer-coffee-shots",
key: "screenshots/#{shot.id}.png"
)
shot.update(s3_etag: s3_response.etag) if s3_response&.etag
end
end
end

0 comments on commit 89d60ba

Please sign in to comment.