Skip to content
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

Add "async" API's for Odyssey to test out. #966

Closed
wants to merge 8 commits into from
Closed

Conversation

zaneenders
Copy link
Collaborator

@zaneenders zaneenders commented Aug 16, 2024

This PR duplicates the API's Currently used by Odyessey to have a different async calling pattern. Where you start the job and then can use check-status to see if the job has been completed then use a new results API to retrieve the information.

While I'm not sure if the async version of the API is a value add this didn't take long to add and allows for flexibility to experiment with how Odyessy manages jobs sent to Herbie as this has been talked about as one possible advantage of the new server design. However, I do think the results API could be useful for re-retrieving information from a job you know has been completed.

An example of how to call these new API's can be found in the testAsyncAPI function in the updated testApi.mjs file.

Very open to different API names as I just copied the convention that improve and improve-start used.

Let me know your thoughts and if I should make a PR in Odyessy using these new APIs.

@elmisback
Copy link
Collaborator

Easy starting point for an Odyssey PR would be to create an async calling function in herbiejs.ts that uses the same strategy as testAsyncAPI and try changing one of the endpoints to use that async function as its backend. (You don't have to totally replace the old functions yet, we can just deprecate and then remove them after we've tested this to be working.) Note Parth did some refactoring on this code recently so it might be a good idea to DM Parth about that.

Copy link
Contributor

@pavpanchekha pavpanchekha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm willing to merge this but we really need to do some cleanup here—lots of ugly duplicated code.

// --------------------------------------

// Sample
const sampleStartData = await testAsyncAPI("sample-start", JSON.stringify({ formula: FPCoreFormula2, seed: 5 }))
Copy link
Contributor

@pavpanchekha pavpanchekha Aug 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this body not stored in a variable above like all the others?


async function testAsyncAPI(endpointName, fetchBodyString) {
const serverURL = `http://127.0.0.1:8000`
const sampleStartURL = new URL(`${serverURL}${`/api/${endpointName}`}`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const sampleStartURL = new URL(`${serverURL}${`/api/${endpointName}`}`)
const sampleStartURL = new URL(`${serverURL}/api/${endpointName}`)

const rspJSON = await rsp.json()
assertIdAndPath(rspJSON)

const checkStatusURL = new URL(`${serverURL}${"/check-status/"}${rspJSON.job}`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const checkStatusURL = new URL(`${serverURL}${"/check-status/"}${rspJSON.job}`)
const checkStatusURL = new URL(`${serverURL}/check-status/${rspJSON.job}`)

let counter = 0
let cap = 100
let checkStatus = await fetch(checkStatusURL, { method: 'GET' })
// Loop to wait for for job to finnish
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Loop to wait for for job to finnish
// Loop to wait for for job to finish

Big fan of the Finns but not like this.

await new Promise(r => setTimeout(r, 100)); // ms
}
assert.equal(checkStatus.statusText, 'Job complete')
const resultsURL = new URL(`${serverURL}${"/api/results/"}${rspJSON.job}`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const resultsURL = new URL(`${serverURL}${"/api/results/"}${rspJSON.job}`)
const resultsURL = new URL(`${serverURL}/api/results/${rspJSON.job}`)

[("api" "sample") #:method "post" sample-endpoint]
[("api" "sample-start") #:method "post" sample-start-endpoint]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, these are all fine, it's not the end of the world or anything, but I think it would be a lot nicer to do /api/start/foo instead of /api/foo-start. We already have a separator character!

Comment on lines +525 to +491
(define analyze-start-endpoint
(post-with-json-response (lambda (post-data)
(define formula-str (hash-ref post-data 'formula))
(define formula (read-syntax 'web (open-input-string formula-str)))
(define sample (hash-ref post-data 'sample))
(define seed (hash-ref post-data 'seed #f))
(define test (parse-test formula))
(define pcontext (json->pcontext sample (test-context test)))
(define command
(create-job 'errors
test
#:seed seed
#:pcontext pcontext
#:profile? #f
#:timeline-disabled? #t))
(define job-id (start-job command))
(hasheq 'job job-id 'path (make-path job-id)))))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could be convinced to merge this but surely this is extremely duplicative with the existing non-async endpoints? Could we have a single function and then generic wrappers around it to make it sync or async?

@zaneenders
Copy link
Collaborator Author

Replaced by PR #983.

@zaneenders zaneenders closed this Sep 1, 2024
@zaneenders zaneenders deleted the zane-async-apis branch September 1, 2024 22:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants