-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add destroy handler and custom event for Durable Objects
- Loading branch information
Showing
9 changed files
with
219 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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,63 @@ | ||
// Copyright (c) 2017-2022 Cloudflare, Inc. | ||
// Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
// https://opensource.org/licenses/Apache-2.0 | ||
|
||
#include "actor-destroy.h" | ||
#include <workerd/api/global-scope.h> | ||
#include <workerd/jsg/ser.h> | ||
|
||
namespace workerd::api { | ||
|
||
ActorDestroyEvent::ActorDestroyEvent() : ExtendableEvent("actorDestroy"){}; | ||
|
||
kj::Promise<WorkerInterface::CustomEvent::Result> | ||
ActorDestroyCustomEventImpl::run(kj::Own<IoContext::IncomingRequest> incomingRequest, | ||
kj::Maybe<kj::StringPtr> entrypointName) { | ||
|
||
auto& context = incomingRequest->getContext(); | ||
|
||
// Mark the request as delivered because we're about to run some JS. | ||
incomingRequest->delivered(); | ||
|
||
EventOutcome outcome = EventOutcome::OK; | ||
|
||
try { | ||
auto result = co_await context.run( | ||
[entrypointName = entrypointName, &context](Worker::Lock& lock) mutable { | ||
auto results = lock.getGlobalScope().actorDestroy( | ||
lock, lock.getExportedHandler(entrypointName, context.getActor())); | ||
return results; | ||
}); | ||
// Now that we've completed the handler execution we can do all the necessary cleanup and cancel | ||
// prevent anything else to be sent to the destroyed actor. As such, we abort the current | ||
// IoContext and shutdown the actor. | ||
context.getActorOrThrow().shutdown( | ||
2, KJ_EXCEPTION(DISCONNECTED, "broken.dropped; Actor was destroyed.")); | ||
context.abort(KJ_EXCEPTION(DISCONNECTED, "broken.dropped; Actor was destroyed.")); | ||
co_return result; | ||
} catch (kj::Exception e) { | ||
if (auto desc = e.getDescription(); | ||
!jsg::isTunneledException(desc) && !jsg::isDoNotLogException(desc)) { | ||
LOG_EXCEPTION("ActorDestroyCustomEventImpl"_kj, e); | ||
} | ||
outcome = EventOutcome::EXCEPTION; | ||
} | ||
|
||
co_return Result{ | ||
.outcome = outcome, | ||
}; | ||
} | ||
|
||
kj::Promise<WorkerInterface::CustomEvent::Result> ActorDestroyCustomEventImpl::sendRpc( | ||
capnp::HttpOverCapnpFactory& httpOverCapnpFactory, capnp::ByteStreamFactory& byteStreamFactory, | ||
kj::TaskSet& waitUntilTasks, rpc::EventDispatcher::Client dispatcher) { | ||
auto req = dispatcher.castAs<rpc::EventDispatcher>().actorDestroyRequest(); | ||
|
||
return req.send().then([](auto resp) { | ||
return WorkerInterface::CustomEvent::Result{ | ||
.outcome = resp.getOutcome(), | ||
}; | ||
}); | ||
} | ||
|
||
} // namespace workerd::api |
This file contains 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,52 @@ | ||
// Copyright (c) 2017-2022 Cloudflare, Inc. | ||
// Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
// https://opensource.org/licenses/Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#include <kj/debug.h> | ||
|
||
#include <workerd/io/worker-interface.capnp.h> | ||
#include <workerd/io/worker-interface.h> | ||
#include <workerd/api/basics.h> | ||
|
||
namespace workerd::api { | ||
|
||
class ActorDestroyEvent final: public ExtendableEvent { | ||
public: | ||
explicit ActorDestroyEvent(); | ||
|
||
static jsg::Ref<ActorDestroyEvent> constructor(kj::String type) = delete; | ||
|
||
JSG_RESOURCE_TYPE(ActorDestroyEvent) { | ||
JSG_INHERIT(ExtendableEvent); | ||
} | ||
}; | ||
|
||
class ActorDestroyCustomEventImpl final: public WorkerInterface::CustomEvent, | ||
public kj::Refcounted { | ||
public: | ||
ActorDestroyCustomEventImpl(uint16_t typeId): typeId(typeId) {} | ||
|
||
kj::Promise<Result> run( | ||
kj::Own<IoContext_IncomingRequest> incomingRequest, | ||
kj::Maybe<kj::StringPtr> entrypointName) override; | ||
|
||
kj::Promise<Result> sendRpc( | ||
capnp::HttpOverCapnpFactory& httpOverCapnpFactory, | ||
capnp::ByteStreamFactory& byteStreamFactory, | ||
kj::TaskSet& waitUntilTasks, | ||
rpc::EventDispatcher::Client dispatcher) override; | ||
|
||
uint16_t getType() override { | ||
return typeId; | ||
} | ||
|
||
private: | ||
uint16_t typeId; | ||
}; | ||
|
||
#define EW_ACTOR_DESTROY_ISOLATE_TYPES \ | ||
api::ActorDestroyEvent, \ | ||
api::ActorDestroyExportedHandler | ||
} // namespace workerd::api |
This file contains 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 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 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 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 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 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 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