On 04/18/2015 12:34 PM, George Neuner wrote:
Hi all,

I have an web server application in which I need to page results of a database query, but I also have to guarantee *exactly-once* statistical processing of each sent result row regardless of how many times it may be sent.

Theresults are from a complex join, howeverthe join is separable into one that gets a list of ids and then another that usesthe ids to get the actual return data.Since the ids are separable (and small), my first thought was to gather the list of ids and then to page through the list separately retrieving theassociated data and doing statistical processing on the rows as necessary.

However, to do this, I have to keep the initial id listacross multiple requestsand thatis where I've run into trouble. Thus far, I haven't had to use web server continuations. It seems like send/suspend does not allow the browser to provideadditional arguments (like paging direction).
The trick with using the continuations is you don't pass any arguments through the url. All information is stored in local variables that are captured automatically by the continuation. The url will be generated for you, it just points to a continuation stored on the server. Does that make sense? It's hard to wrap your brain around at first.
send/suspend/dispatch looks like it will do what I want, however Ineed to communicate the URLs to the browser in JSONand it (superficially) appears as if embed/url is meant to work in generated HTML?
Youneed to use a different response function. See theattached codefor an example.Look at the docs for 'response/xexpr' to see what it does.

The final problem is how to ensure that I eventually get to send/finishand clean up my thread. This particular function is expected to be executed quite often. Does the browser app have to invoke a URL that deliberately ends the thread or will the thread end if/whenits continuations timeout?
What do you need to clean up? To be clear, there isn't a thread that keeps running - the continuation is just some data, and like Jay said, it will eventually be cleaned up. If you want to remove the continuations manually, call send/forward or send/finish. See the attached code. In particular, try running it, and then copying the url to a second browser window. You'll see how two users have different continuations.

Apologies if this is rambling - I'm trying to sort out a bunch of things all at once.
Thanks,
George
--
You are asking the right questions.  Hope this helps, and keep asking!

Thanks,
Dave

--
You received this message because you are subscribed to the Google Groups "Racket 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
#lang web-server/insta

(require json)

(define (response/json jsexpr)
  (response/full 200 #"Okay" (current-seconds) #"application/json"
    (list)
    (list #"" (jsexpr->bytes jsexpr))))

(define (start request)
  ; get ids from database
  (define ids '(0 1 2 3 4 5 6 7 8 9))
  
  ; idx is the index of the id we are showing
  (define idx 0)
  
  (let loop ()
    
    ; produce stats to show to user, memoize?
    (define stats (* 2 (list-ref ids idx)))
      
    (send/suspend/dispatch
     (lambda (embed/url)
       ; example of json response
       #;(response/json
        (hasheq 'id (list-ref ids idx)
                'url (embed/url
                      (lambda (req)
                        (set! idx (sub1 idx))
                        (loop)))))
       (response/xexpr
        `(html
          (body
           (p
            "showing id " ,(~a (list-ref ids idx)))
           (a ((href
                ,(embed/url
                  (lambda (req)
                    (set! idx (sub1 idx))
                    (loop)))))
              "show previous id")
           (br)
           (a ((href
                ,(embed/url
                  (lambda (req)
                    (set! idx (add1 idx))
                    (loop)))))
              "show next id")
           (br)
           (a ((href
                ,(embed/url
                  (lambda (req)
                    (send/finish (logout-page))))))
              "done"))))))))

(define (logout-page)
  (response/xexpr
   `(html
     (body
      "continuations cleared, you're done"))))

Reply via email to