https://discord.cloudflare.com logo
Join Discord
Powered by
# durable-objects
  • m

    molenzwiebel

    04/21/2021, 7:58 PM
    you can only use scheduled stuff/io when handling a request iirc
  • m

    molenzwiebel

    04/21/2021, 7:58 PM
    theres some error it throws
  • m

    molenzwiebel

    04/21/2021, 8:00 PM
    wrangler tail might be able to show it?
  • v

    vans163

    04/21/2021, 8:01 PM
    is there anyway to make a DO tick then as long as 1 websocket is connected?
  • v

    vans163

    04/21/2021, 8:02 PM
    i thought via setInterval or setTimeout and waitUntil it was possible
  • k

    kenton

    04/21/2021, 8:09 PM
    waitUntil
    inside a DO is meaningless. Anything you do inside a DO will keep running until the object itself shuts down, which is typically after 30 seconds of being idle (no new requests).
  • k

    kenton

    04/21/2021, 8:11 PM
    ^
  • v

    vans163

    04/21/2021, 8:12 PM
    hum well i tried setTimeout(this.my_func, 1000) and setInterval(this.my_func, 1000) both dont call anything, DO keeps running
  • v

    vans163

    04/21/2021, 8:13 PM
    tried making my_func async and nonasync
  • m

    molenzwiebel

    04/21/2021, 8:18 PM
    I presume it stays active while a websocket is connected to it? Or does it also require messages through that websocket every n seconds?
  • k

    kenton

    04/21/2021, 8:20 PM
    This might be a JavaScript issue.
    setTimeout(this.my_func, 1000)
    won't work --
    my_func
    will be called without a
    this
    , and probably throws an exception. You need to do
    setTimeout(this.my_func.bind(this), 1000)
    or
    setTimeout(() => this.my_func(), 1000)
    .
  • k

    kenton

    04/21/2021, 8:22 PM
    Yes. "idle" really means "no request currently running", so shutdown is 30+ seconds after the last request finishes. An open WebSocket is an active request.
  • m

    molenzwiebel

    04/21/2021, 8:24 PM
    I assume the billing for DOs with a WebSocket active counts every second the socket is open, and not just every second it spends handling the messages?
  • m

    molenzwiebel

    04/21/2021, 8:26 PM
    Would be awesome if it was the second one, but obviously keeping the DO in memory costs money
  • m

    matt

    04/21/2021, 8:34 PM
    https://discord.com/channels/595317990191398933/773219443911819284/831990845523623977 We’re still thinking about it
  • v

    vans163

    04/21/2021, 9:05 PM
    🌩️ perfect it works!
  • v

    vans163

    04/21/2021, 9:12 PM
    is there a recommended workflow to catch errors goin on in the DO?
  • v

    vans163

    04/21/2021, 9:12 PM
    something like datadog logger
  • v

    vans163

    04/21/2021, 9:12 PM
    guessing cloudflare can keep a log of these?
  • k

    kenton

    04/21/2021, 9:57 PM
    wrangler tail
    should report uncaught exceptions but I'm not sure if it works yet for DO...
  • v

    vans163

    04/22/2021, 12:27 AM
    how do you properly close a websocket on side of DO? i called websocket.close() on it, but it seems still open on the clientside
  • v

    vans163

    04/22/2021, 12:37 AM
    ah I see, I think inside the DO, if you call close with no exit reason, it does not close the conn. Just confirmed this
    Copy code
    // nothing happens, client is connected
    session.websocket.close();
    
    // client gets disconnected
    session.websocket.close(1000, "no pong");
  • k

    kenton

    04/22/2021, 12:47 AM
    .close()
    will throw an exception if you don't pass in a valid code. I guess the exception isn't getting caught. Sorry that errors are hard to see right now. FWIW I highly recommend wrapping all code in a try/catch of some sort and reporting the error somewhere.
  • v

    vans163

    04/22/2021, 1:12 AM
    yea i have a global wrap but in this case, the close got called from a setTimeout
  • v

    vans163

    04/22/2021, 1:12 AM
    the docs also say both args to .close() are optional
  • v

    vans163

    04/22/2021, 1:12 AM
    it should perhaps be edited to include the first arg to .close is manditory
  • v

    vans163

    04/22/2021, 1:14 AM
  • v

    vans163

    04/22/2021, 1:14 AM
    (these are the docs for the websocket pair on the cloudflare side, not the clientside javascript)
  • v

    vans163

    04/22/2021, 1:47 AM
    Copy code
    export class User {
      constructor(controller, env) {
        this.account = this.constructor.name;
    this does not seem to return the name of the DO for me, it just returns "S"
  • v

    vans163

    04/22/2021, 1:51 AM
    this.storage.put
    does not seem to work if called from a setTimeout, could that be?
1...676869...567Latest