Skip to content

Conversation

@Kitt3120
Copy link
Contributor

@Kitt3120 Kitt3120 commented Oct 4, 2024

The main goal of this branch was to introduce an event system, use said event system to track service status events, and let the bot react to them. The bot should shut down on errors or when receiving SIGINT.

This took way longer than expected because of IRL stuff, like work and studying. Feels surreal to finally be able to merge this now after 9 months! 🥳

AI Summary

This pull request introduces significant enhancements to the Lum Discord Bot, including the addition of an event system, improvements to the bot's service management, and updates to dependencies. Here's a summary of the most important changes:

Event System Enhancements:

  • New Event Modules: Added several new modules (arc_observable, event, event_repeater, observable, subscriber, subscription) to support the event-driven architecture. (src/event.rs, src/event/arc_observable.rs, src/event/event.rs, src/event/event_repeater.rs, src/event/observable.rs, src/event/subscriber.rs, src/event/subscription.rs) [1] [2] [3] [4] [5] [6] [7]

Bot Service Management:

  • Service Management Refactor: Refactored the BotBuilder and Bot structs to use tokio::sync::Mutex instead of RwLock for service management and added an ExitReason enum to handle different exit scenarios. (src/bot.rs) [1] [2] [3]

Dependency Updates:

  • Updated Dependencies: Updated the version of the Lum package to 0.2.1 and added the uuid crate with specific features. (Cargo.toml) [1] [2]

Documentation:

  • README Correction: Fixed a typo in the README file. (README.md)

These changes collectively enhance the functionality, reliability, and maintainability of the Lum Discord Bot.

* Setup (#1)

 - Add tokio, serde and sqlx crates
 - Setup .gitignore
 - Setup Cargo.toml
 - Setup pipelines

* Enable Dependabot (#3)

Add dependabot.yml with daily cargo updates configured

* Fix staging pipelines (#4)

Fix staging pipelines not triggering on pull request

* Implement Cargo caching (#5)

Add Swatinem's Rust-Cache@v2 action to build, test and deploy pipelines

* Improve README.md (#6)

 - Add deployment badges
 - Add collaborating info (board + issues)
 - Made Status clonable by not using BoxedErrors anymore but Strings for holding the error information
 - Add get_status() to Service
 - Made status property of Service private
 - Implement Event<T>
 - Add status_changed Event to ServiceInfo
 - Add name attribute to Event<T>
 - Unify Channel and Closure subscribers by using an Enum
 - Propagate errors when dispatching events
 - Add error log when errors occur while dispatching events
 - Subscribers are now removed from an event when they run into an error while dispatching
Make the removal of Event subscribers on error optional
 - Refactors in service_manager.rs
 - Refactors in watchdog.rs
I made these changes many months ago. Reviewed them for like half an hour, looks good. I know what I was working on. Will continue now :)
@Kitt3120 Kitt3120 added the feature Adds or requests new feature label Oct 4, 2024
@Kitt3120 Kitt3120 self-assigned this Oct 4, 2024
@Kitt3120 Kitt3120 linked an issue Oct 4, 2024 that may be closed by this pull request
@Kitt3120 Kitt3120 enabled auto-merge (squash) October 4, 2024 14:52
@Kitt3120 Kitt3120 changed the title Feature/service status events Merge feature/service-status-events into staging Oct 4, 2024
@Kitt3120 Kitt3120 disabled auto-merge October 4, 2024 14:59
@Kitt3120 Kitt3120 merged commit 55956b9 into staging Oct 4, 2024
3 checks passed
@Kitt3120 Kitt3120 deleted the feature/service-status-events branch October 4, 2024 14:59
Kitt3120 added a commit that referenced this pull request Oct 4, 2024
* Bump main to staging (#7)

* Setup (#1)

 - Add tokio, serde and sqlx crates
 - Setup .gitignore
 - Setup Cargo.toml
 - Setup pipelines

* Enable Dependabot (#3)

Add dependabot.yml with daily cargo updates configured

* Fix staging pipelines (#4)

Fix staging pipelines not triggering on pull request

* Implement Cargo caching (#5)

Add Swatinem's Rust-Cache@v2 action to build, test and deploy pipelines

* Improve README.md (#6)

 - Add deployment badges
 - Add collaborating info (board + issues)

* Clonable Status

 - Made Status clonable by not using BoxedErrors anymore but Strings for holding the error information
 - Add get_status() to Service
 - Made status property of Service private

* Adapt Service Manager to new Status enum

* Events

 - Implement Event<T>
 - Add status_changed Event to ServiceInfo

* Event improvements

 - Add name attribute to Event<T>
 - Unify Channel and Closure subscribers by using an Enum
 - Propagate errors when dispatching events
 - Add error log when errors occur while dispatching events
 - Subscribers are now removed from an event when they run into an error while dispatching

* Event improvements

Make the removal of Event subscribers on error optional

* Slight refactors

 - Refactors in service_manager.rs
 - Refactors in watchdog.rs

* WIP: Idk lol

I made these changes many months ago. Reviewed them for like half an hour, looks good. I know what I was working on. Will continue now :)

* add: allow clippy::multiple_bound_locations for service module

* add: observables

* refactor: use Mutex instead of RwLock everywhere

* refactor: make remove_on_error work on per-subscriber basis

* refactor: make subscribers identifiable

* refactor: move dispatch logic to Subscriber

* add: AsyncClosure Callback type

* WIP: EventRepeater

* add: EventRepeater

* refactor: event subscribe method names

* refactor: move subscription into own module

* add: AsRef<Event<T>>

* add: UUID, PartialEq/Eq, unsubscribe()

* add: event_repeater detach(), close()

* add: attach/deattach EventRepeater on start/stop of service

* add: service runtime failure handling

* fix: bump version to 0.2.1
Kitt3120 added a commit that referenced this pull request Oct 4, 2024
* Setup (#1)

- Add tokio, serde and sqlx crates
 - Setup .gitignore
 - Setup Cargo.toml
 - Setup pipelines

* Add config file (#14)

* Implement config

 - Add dirs crate
 - Implement ConfigHandler
 - Implement custom error types
 - Implement config struct
 - Load config on startup

* Use thiserror

 - Add thiserror crate
 - Refactor existing error types to use thiserror

* Reference Milestones in README.md (#15)

Update README.md

Add reference to Milestones page

* Add README.md portrait (#16)

* Add portrait image

* Add portrait image to README.md

* Add Service Framework (#21)

* Implement service framework

 - Make main async
 - Implement Status enum
 - Implement Priority enum
 - Implement ServiceInfo struct
 - Implement ServiceInternals trait
 - Implement Service trait

* Bot library

 - Add fern crate
 - Add humantime crate
 - Add log crate
 - Implement Bot
 - Implement BotBuilder
 - Refactor config Display trait implementation
 - Implement library is_debug() function
 - Implement library run(Bot) function
 - Implement log module (log::setup(), log::is_set_up() and log::get_min_log_level())
 - Adapt main to new changes

* WIP: Finish services framework

Just a lot of refactoring and fixing. No time to describe all this now. Happy new year! :)

* Finish services framework

Too much to describe. It's done, that's it. This was one hell of a ride.

* Refactor codebase (#23)

- Refined some derived traits
 - Refactor Service/ServiceInfo: Move some trait implementations from Service to ServiceInfo and refer to the implementations from Service
 - Compare ID instead of name in ServiceManagerBuilder

* Feature/discord service (#24)

* Add serenity

Add serenity as a dependency and use some extra features and the native TLS backend

* Implement Discord Service

 - Implement Discord Service
 - Add discordTimeout config variable
 - Add additional logger setup to mute unimportant messages introduced by serenity

* Refactor service framework (#25)

* Service framework improvements

 - Way better handling of mutable/immutable attributes
 - Less Mutexes
 - Better handling of passing references through the service framework's API
 - Reimplement get_service accepting a TypeId as a generic parameter for easier usage
 - Reimplement status_map and status_tree as a result of the above adaptations, resulting in way simpler versions

* More service framework improvements

 - Replace all Mutexes with RwLock
 - Remove status_map method of ServiceManager
 - Services vector in ServiceManager now wraps the Service trait objects in a RwLock to potentially make them available mutably through the public API of ServiceManager

* Implement get_service<T> method

 - Add downcast-rs crate
 - Implement get_service<T> method of ServiceManager

Had to use unsafe Rust for this. Tried it with safe Rust for 3 days and couldn't do it. With unsafe Rust, it's very easy. It's also still kinda safe, as the crash case is checked and prevented before going into the unsafe block.

* Finish refactor of service framework

 - ServiceManager now holds an Arc to itself
 - Self-Arc is now passed to services when initializing them, so they can access other services and copy Arcs to those for themselves
 - Implement SetLock<T> struct which is a wrapper around Option<T> for lazy-initialization
 - ServiceManagerBuilder handles the creation and injection of the Self-Arc of ServiceManager. That's why the build() method is now async and the From trait had to be removed. The From trait cannot be implemented async.
 - To keep everything consistent, the From trait has also been removed from the BotBuilder and the build() method becase async.

* Adapt Discord service

 - Adapt Discord service to new service framework and SetLock type

* Fix service framework (#31)

 - Fix deadlock when accessing the ServiceManager parameter in start() method of a Service
 - Known bug: Deadlock still happens when a service accesses itself through the ServiceManager on start

* Refactor/service-framework: start_wrapped and stop_wrapped (#32)

Refactor service

start_wrapped and stop_wrapped have been removed from the Service trait and the logic has been implemented in the Service Manager.

* Implement service startup/shutdown timeout (#33)

- Add hardcoded timeout of 10 seconds on service startup
 - Add hardcoded timeout of 10 seconds on service shutdown
 - Remove timeout implementation of Discord service, as it is now handled by the Service Manager.

* Service framework background tasks and watchdogs (#39)

* Service framework background task

 - Implement an optional background task for services that starts automatically on service start.
 - Implement a watchdog that updates the service's status when its task fails at runtime

* WIP: Partially implemented

 - Background task startup + watchdog implemented for service startup
 - Implementation for service shutdown missing
 - Handling of watchdog triggers of essential tasks missing

* Refactor of service framework

Split service framework into multiple submodules

* Implement Watchdog module

* Optimize Watchdog module

Make usage of Watchdog more flexible

* Refactor start_service

Refactor the giant block of cod e into smaller helper methods where possible

* Edit StartupError string representations

* Finish refactor of start_service method

* Refactor stop_service

 - Apply same refactoring to stop_service
 - Increase name placeholder space to 30 in logger

* Add Devcontainer (#40)

* added devcontainer

added postgresql and some nice to have extensions

* removed clang to fix pipelines

* Update .devcontainer/devcontainer.json

Co-authored-by: Torben Schweren <torben@schweren.dev>

* Update .devcontainer/docker-compose.yml

Co-authored-by: Torben Schweren <torben@schweren.dev>

---------

Co-authored-by: Torben Schweren <torben@schweren.dev>

* Update sqlx requirement from 0.7.3 to 0.8.0 (#49)

* Bump main to staging (#7)

* Setup (#1)

 - Add tokio, serde and sqlx crates
 - Setup .gitignore
 - Setup Cargo.toml
 - Setup pipelines

* Enable Dependabot (#3)

Add dependabot.yml with daily cargo updates configured

* Fix staging pipelines (#4)

Fix staging pipelines not triggering on pull request

* Implement Cargo caching (#5)

Add Swatinem's Rust-Cache@v2 action to build, test and deploy pipelines

* Improve README.md (#6)

 - Add deployment badges
 - Add collaborating info (board + issues)

* Update sqlx requirement from 0.7.3 to 0.8.0

Updates the requirements on [sqlx](https://github.com/launchbadge/sqlx) to permit the latest version.
- [Changelog](https://github.com/launchbadge/sqlx/blob/main/CHANGELOG.md)
- [Commits](launchbadge/sqlx@v0.7.3...v0.8.0)

---
updated-dependencies:
- dependency-name: sqlx
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>

* remove: empty line

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: Torben Schweren <torben@schweren.dev>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Merge feature/service-status-events into staging (#55)

* Bump main to staging (#7)

* Setup (#1)

 - Add tokio, serde and sqlx crates
 - Setup .gitignore
 - Setup Cargo.toml
 - Setup pipelines

* Enable Dependabot (#3)

Add dependabot.yml with daily cargo updates configured

* Fix staging pipelines (#4)

Fix staging pipelines not triggering on pull request

* Implement Cargo caching (#5)

Add Swatinem's Rust-Cache@v2 action to build, test and deploy pipelines

* Improve README.md (#6)

 - Add deployment badges
 - Add collaborating info (board + issues)

* Clonable Status

 - Made Status clonable by not using BoxedErrors anymore but Strings for holding the error information
 - Add get_status() to Service
 - Made status property of Service private

* Adapt Service Manager to new Status enum

* Events

 - Implement Event<T>
 - Add status_changed Event to ServiceInfo

* Event improvements

 - Add name attribute to Event<T>
 - Unify Channel and Closure subscribers by using an Enum
 - Propagate errors when dispatching events
 - Add error log when errors occur while dispatching events
 - Subscribers are now removed from an event when they run into an error while dispatching

* Event improvements

Make the removal of Event subscribers on error optional

* Slight refactors

 - Refactors in service_manager.rs
 - Refactors in watchdog.rs

* WIP: Idk lol

I made these changes many months ago. Reviewed them for like half an hour, looks good. I know what I was working on. Will continue now :)

* add: allow clippy::multiple_bound_locations for service module

* add: observables

* refactor: use Mutex instead of RwLock everywhere

* refactor: make remove_on_error work on per-subscriber basis

* refactor: make subscribers identifiable

* refactor: move dispatch logic to Subscriber

* add: AsyncClosure Callback type

* WIP: EventRepeater

* add: EventRepeater

* refactor: event subscribe method names

* refactor: move subscription into own module

* add: AsRef<Event<T>>

* add: UUID, PartialEq/Eq, unsubscribe()

* add: event_repeater detach(), close()

* add: attach/deattach EventRepeater on start/stop of service

* add: service runtime failure handling

* fix: bump version to 0.2.1

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: Marco Murawski <info@murasko.de>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature Adds or requests new feature

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Service Manager / Service events for status change

2 participants