Repository navigation
Add background job for ai hint request #1135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8c72520
add AiHints table and do migration
cocomarine 02fa34e
add ai_hint model
cocomarine cb2f2cc
add generate ai hint job
cocomarine f7291fc
Give the GoodJob worker a larger DB pool via DB_POOL
cocomarine ad8fac3
Add AiHintsController to create and show AI hints
cocomarine 22c62c0
Merge branch 'main' into 1932-add-background-job-for-AiHint-request
cocomarine 0e8fcf9
handle unexpected job errors
cocomarine 017b018
Make worker pool and thread count configurable for different environm…
cocomarine f181bf4
Merge branch 'main' into 1932-add-background-job-for-AiHint-request
cocomarine 6356048
clarify timings using constants
cocomarine 84b6c6c
Add weekly cleanup job for old AI hints
cocomarine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| web: bundle exec puma -C config/puma.rb | ||
| release: bundle exec rails db:migrate && bundle exec rake integration_tests:dispatch | ||
| worker: bundle exec good_job start --max-threads=8 | ||
| worker: DB_POOL=${WORKER_DB_POOL:-25} bundle exec good_job start --max-threads=8 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Api | ||
| class AiHintsController < ApiController | ||
| MAX_REQUEST_SIZE_KB = 100 | ||
|
|
||
| before_action :authorize_user | ||
| before_action :check_feature_flag | ||
| before_action :check_request_size, only: :create | ||
| skip_authorization_check | ||
|
|
||
| def show | ||
| ai_hint = AiHint.find_by!(id: params.expect(:id), user_id: current_user.id) | ||
|
|
||
| render json: { status: ai_hint.status, hint: ai_hint.hint }.compact, status: :ok | ||
| end | ||
|
|
||
| def create | ||
| params.require(%i[message trace runtime code]) | ||
|
|
||
| ai_hint = AiHint.create!( | ||
| user_id: current_user.id, | ||
| input: params.permit(:message, :trace, :runtime, :code).to_h | ||
| ) | ||
| GenerateAiHintJob.perform_later(ai_hint_id: ai_hint.id) | ||
|
|
||
| render json: { id: ai_hint.id }, status: :accepted | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def check_feature_flag | ||
| return if FeatureFlagHelpers.enabled_for_user?(:ai_friendly_errors, current_user, current_user&.schools&.first) | ||
|
|
||
| render json: {}, status: :unauthorized | ||
| end | ||
|
|
||
| def check_request_size | ||
| return if request.body.size <= MAX_REQUEST_SIZE_KB.kilobytes | ||
|
|
||
| render json: { error: "The request was over the max size of #{MAX_REQUEST_SIZE_KB} KB" }, status: :content_too_large | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class GenerateAiHintJob < ApplicationJob | ||
| # How long editor-ui polls for a hint before giving up. | ||
| UI_POLL_TIMEOUT = 30.seconds | ||
|
|
||
| # A hint must leave enough time for inference to finish before editor-ui stops polling. | ||
| MAX_QUEUE_WAIT = UI_POLL_TIMEOUT - FriendlyErrorMessagesInferenceClient::TIMEOUT | ||
|
|
||
| queue_as :ai_hints | ||
|
|
||
| def perform(ai_hint_id:) | ||
| ai_hint = AiHint.find(ai_hint_id) | ||
| return unless ai_hint.pending? | ||
| return ai_hint.fail! if ai_hint.created_at < MAX_QUEUE_WAIT.ago | ||
|
|
||
| input = ai_hint.input | ||
| hint = FriendlyErrorMessagesInferenceClient.new.get_message( | ||
| input['message'], | ||
| input['trace'], | ||
| input['runtime'], | ||
| input['code'] | ||
| ) | ||
|
|
||
| ai_hint.complete!(hint) | ||
| rescue FriendlyErrorMessagesInferenceClient::Error => e | ||
| Sentry.capture_exception(e) | ||
| ai_hint.fail! | ||
|
github-code-quality[bot] marked this conversation as resolved.
Fixed
cocomarine marked this conversation as resolved.
Dismissed
|
||
| rescue StandardError | ||
| # Don't leave the hint stuck in pending holding the input; re-raise so | ||
| # GoodJob records the failure and sentry-rails reports it. | ||
| ai_hint&.fail! | ||
| raise | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class RemoveOldAiHintsJob < ApplicationJob | ||
| MAX_AGE = 1.day | ||
|
|
||
| queue_as :default | ||
|
|
||
| def perform | ||
| AiHint.where(created_at: ...MAX_AGE.ago).in_batches.delete_all | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class AiHint < ApplicationRecord | ||
| enum :status, { pending: 'pending', completed: 'completed', failed: 'failed' }, default: :pending, validate: true | ||
|
|
||
| validates :user_id, presence: true | ||
|
|
||
| def complete!(hint) | ||
| update!(status: :completed, hint:, input: nil) | ||
| end | ||
|
|
||
| def fail! | ||
| update!(status: :failed, input: nil) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class CreateAiHints < ActiveRecord::Migration[8.1] | ||
| def change | ||
| create_table :ai_hints, id: :uuid do |t| | ||
| t.uuid :user_id, null: false | ||
| t.string :status, null: false, default: 'pending' | ||
| t.jsonb :input | ||
| t.text :hint | ||
| t.timestamps | ||
| end | ||
|
|
||
| add_index :ai_hints, :user_id | ||
| end | ||
| end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| FactoryBot.define do | ||
| factory :ai_hint do | ||
| user_id { SecureRandom.uuid } | ||
| input { { 'message' => 'NameError', 'trace' => 'line 1', 'runtime' => 'python', 'code' => 'print(x)' } } | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe GenerateAiHintJob do | ||
| let(:ai_hint) { create(:ai_hint) } | ||
| let(:client) { instance_double(FriendlyErrorMessagesInferenceClient) } | ||
|
|
||
| before do | ||
| allow(FriendlyErrorMessagesInferenceClient).to receive(:new).and_return(client) | ||
| allow(client).to receive(:get_message).and_return('Check the variable name') | ||
| allow(Sentry).to receive(:capture_exception) | ||
| end | ||
|
|
||
| it 'uses the ai_hints queue' do | ||
| expect(described_class.new.queue_name).to eq('ai_hints') | ||
| end | ||
|
|
||
| it 'passes the input to the inference client' do | ||
| described_class.perform_now(ai_hint_id: ai_hint.id) | ||
| expect(client).to have_received(:get_message).with('NameError', 'line 1', 'python', 'print(x)') | ||
| end | ||
|
|
||
| it 'saves the hint and marks it completed' do | ||
| described_class.perform_now(ai_hint_id: ai_hint.id) | ||
| expect(ai_hint.reload).to have_attributes(status: 'completed', hint: 'Check the variable name', input: nil) | ||
| end | ||
|
|
||
| context 'when the hint waited too long in the queue' do | ||
| let(:ai_hint) { create(:ai_hint, created_at: (described_class::MAX_QUEUE_WAIT + 1.second).ago) } | ||
|
|
||
| it 'marks it failed' do | ||
| described_class.perform_now(ai_hint_id: ai_hint.id) | ||
| expect(ai_hint.reload).to have_attributes(status: 'failed', input: nil) | ||
| end | ||
|
|
||
| it 'does not call the inference client' do | ||
| described_class.perform_now(ai_hint_id: ai_hint.id) | ||
| expect(client).not_to have_received(:get_message) | ||
| end | ||
| end | ||
|
|
||
| context 'when the hint is no longer pending' do | ||
| let(:ai_hint) { create(:ai_hint, status: :completed, hint: 'Existing hint') } | ||
|
|
||
| it 'does not call the inference client' do | ||
| described_class.perform_now(ai_hint_id: ai_hint.id) | ||
| expect(client).not_to have_received(:get_message) | ||
| end | ||
| end | ||
|
|
||
| context 'when the inference client fails' do | ||
| let(:error) { FriendlyErrorMessagesInferenceClient::Error.new('boom') } | ||
|
|
||
| before { allow(client).to receive(:get_message).and_raise(error) } | ||
|
|
||
| it 'marks it failed' do | ||
| described_class.perform_now(ai_hint_id: ai_hint.id) | ||
| expect(ai_hint.reload).to have_attributes(status: 'failed', hint: nil, input: nil) | ||
| end | ||
|
|
||
| it 'reports the error to Sentry' do | ||
| described_class.perform_now(ai_hint_id: ai_hint.id) | ||
| expect(Sentry).to have_received(:capture_exception).with(error) | ||
| end | ||
| end | ||
|
|
||
| context 'when an unexpected error is raised' do | ||
| before { allow(client).to receive(:get_message).and_raise(StandardError, 'boom') } | ||
|
|
||
| it 'marks it failed and re-raises the error' do | ||
| expect { described_class.perform_now(ai_hint_id: ai_hint.id) }.to raise_error(StandardError, 'boom') | ||
| expect(ai_hint.reload).to have_attributes(status: 'failed', hint: nil, input: nil) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe RemoveOldAiHintsJob do | ||
| it 'removes hints older than a day and keeps newer ones' do | ||
| old_hint = create(:ai_hint, created_at: 2.days.ago) | ||
| new_hint = create(:ai_hint, created_at: 1.hour.ago) | ||
|
|
||
| described_class.perform_now | ||
|
|
||
| expect(AiHint.pluck(:id)).to contain_exactly(new_hint.id) | ||
| expect(AiHint.exists?(old_hint.id)).to be(false) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe AiHint do | ||
| subject(:ai_hint) { build(:ai_hint) } | ||
|
|
||
| describe 'validations' do | ||
| it 'has a valid factory' do | ||
| expect(ai_hint).to be_valid | ||
| end | ||
|
|
||
| it 'requires a user_id' do | ||
| ai_hint.user_id = nil | ||
| expect(ai_hint).not_to be_valid | ||
| end | ||
|
|
||
| it 'rejects an unknown status' do | ||
| ai_hint.status = 'random' | ||
| expect(ai_hint).not_to be_valid | ||
| end | ||
| end | ||
|
|
||
| it 'defaults to pending' do | ||
| expect(described_class.new).to be_pending | ||
| end | ||
|
|
||
| describe '#complete!' do | ||
| before { ai_hint.save! } | ||
|
|
||
| it 'saves the hint and marks it completed' do | ||
| ai_hint.complete!('Check the variable name') | ||
| expect(ai_hint.reload).to have_attributes(status: 'completed', hint: 'Check the variable name') | ||
| end | ||
|
|
||
| it 'clears the input' do | ||
| ai_hint.complete!('Check the variable name') | ||
| expect(ai_hint.reload.input).to be_nil | ||
| end | ||
| end | ||
|
|
||
| describe '#fail!' do | ||
| before { ai_hint.save! } | ||
|
|
||
| it 'marks it failed without a hint' do | ||
| ai_hint.fail! | ||
| expect(ai_hint.reload).to have_attributes(status: 'failed', hint: nil) | ||
| end | ||
|
|
||
| it 'clears the input' do | ||
| ai_hint.fail! | ||
| expect(ai_hint.reload.input).to be_nil | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.