Skip to content

add the ability to de-register a task #121

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 5 commits into from
Jun 13, 2022

Conversation

tomerd
Copy link
Contributor

@tomerd tomerd commented Jun 10, 2022

motivation: sometimes a task needs to be de-registered since the task was manually shutdown outside the lifecycle scope

changes:

  • registration APIs now return a registration key which can be used as a cancellation token
  • add API to de-register a task
  • refactor state to use a registery instead of array of tasks
  • add tests

@tomerd
Copy link
Contributor Author

tomerd commented Jun 10, 2022

resolves #96

@tomerd tomerd force-pushed the feature/cancelation-tickets branch 4 times, most recently from 4e05ee6 to 821c959 Compare June 10, 2022 20:35
motivation: sometimes a task needs to be de-registered since the task was manually shutdown outside the lifecycle scope

changes:
* registration APIs now return a registration key which can be used as a cancellation token
* add API to de-register a task
* refactor state to use a registery instead of array of tasks
* add tests
@tomerd tomerd force-pushed the feature/cancelation-tickets branch from 821c959 to 6b88ef1 Compare June 10, 2022 20:40
@tomerd tomerd requested review from weissi, yim-lee and ktoso June 10, 2022 21:19

func add(_ tasks: [LifecycleTask]) -> [RegistrationKey] {
// FIXME: better id generation scheme (cant use UUID)
let random = UInt64.random(in: UInt64.min ..< UInt64.max).addingReportingOverflow(DispatchTime.now().uptimeNanoseconds).partialValue
Copy link
Contributor Author

Choose a reason for hiding this comment

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

better ideas welcome. we cant use UUID here

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe worth checking for existing keys to make sure there are no conflicts?


func add(_ tasks: [LifecycleTask]) -> [RegistrationKey] {
// FIXME: better id generation scheme (cant use UUID)
let random = UInt64.random(in: UInt64.min ..< UInt64.max).addingReportingOverflow(DispatchTime.now().uptimeNanoseconds).partialValue
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe worth checking for existing keys to make sure there are no conflicts?

tomerd and others added 4 commits June 10, 2022 18:02
/// - tasks: one or more `LifecycleTask`.
@discardableResult
public func register(_ tasks: LifecycleTask ...) -> [RegistrationKey] {
return self.register(tasks)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
return self.register(tasks)
self.register(tasks)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

still supporting swift 5.0 :D

self.register(tasks)
@discardableResult
public func register(_ tasks: LifecycleTask) -> RegistrationKey {
return self.register(tasks).first! // force the optional on the first in this case is safe
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
return self.register(tasks).first! // force the optional on the first in this case is safe
self.register(tasks).first! // force the optional on the first in this case is safe

self.register(_LifecycleTask(label: label, shutdownIfNotStarted: shutdownIfNotStarted, start: start, shutdown: shutdown))
@discardableResult
public func register(label: String, start: LifecycleHandler, shutdown: LifecycleHandler, shutdownIfNotStarted: Bool? = nil) -> RegistrationKey {
return self.register(_LifecycleTask(label: label, shutdownIfNotStarted: shutdownIfNotStarted, start: start, shutdown: shutdown))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
return self.register(_LifecycleTask(label: label, shutdownIfNotStarted: shutdownIfNotStarted, start: start, shutdown: shutdown))
self.register(_LifecycleTask(label: label, shutdownIfNotStarted: shutdownIfNotStarted, start: start, shutdown: shutdown))

self.register(label: label, start: .none, shutdown: handler)
@discardableResult
public func registerShutdown(label: String, _ handler: LifecycleHandler) -> RegistrationKey {
return self.register(label: label, start: .none, shutdown: handler)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
return self.register(label: label, start: .none, shutdown: handler)
self.register(label: label, start: .none, shutdown: handler)

self.register(StatefulLifecycleTask(label: label, start: start, shutdown: shutdown))
@discardableResult
public func registerStateful<State>(label: String, start: LifecycleStartHandler<State>, shutdown: LifecycleShutdownHandler<State>) -> RegistrationKey {
return self.register(StatefulLifecycleTask(label: label, start: start, shutdown: shutdown))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
return self.register(StatefulLifecycleTask(label: label, start: start, shutdown: shutdown))
self.register(StatefulLifecycleTask(label: label, start: start, shutdown: shutdown))

Copy link
Collaborator

@weissi weissi left a comment

Choose a reason for hiding this comment

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

Awesome, thank you!

@tomerd tomerd merged commit 22363fe into swift-server:main Jun 13, 2022
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