Skip to content
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

Refactor main.rs: Move loop and functions to app module #44

Merged
merged 3 commits into from
Apr 17, 2023

Conversation

cipherchabon
Copy link
Contributor

This pull request significantly improves the organization and readability of the codebase by refactoring the main.rs file. Previously, main.rs contained a large number of lines of code, making it difficult to navigate and maintain.

The following changes have been made:

  1. The main.rs file has been streamlined to only include the initial configuration setup, making it easier to understand the purpose of the file.

  2. The main loop responsible for mapping actions to their corresponding functions has been moved to a separate file called app.rs.

  3. Each function associated with a specific action has been moved to its own separate file within the app module. This modular approach makes it easier to manage, maintain, and extend the codebase in the future.

Overall, these changes lead to a more maintainable and easier-to-understand code structure, allowing for more efficient development and collaboration.

@arkanoider
Copy link
Collaborator

Great work buddy! You did a great refactoring of main routine imo. Let's wait for @grunch review! I think you will be merged soon!

@grunch
Copy link
Member

grunch commented Apr 17, 2023

Hey @cypherchabon Great contribution!!!! we need to start clean things up 😃

I'm trying to review it but I'm having this panick runing it:

RUST_LOG=info cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.26s
     Running `target/debug/mostro`
 INFO  sqlx::query > PRAGMA foreign_keys = ON; …; rows affected: 0, rows returned: 0, elapsed: 65.302µs

PRAGMA foreign_keys = ON;

 INFO  nostr_sdk::relay > Connected to ws://localhost:7000/
2023-04-17T12:56:24.187413Z  INFO mostro::scheduler: Creating scheduler
2023-04-17T12:56:24.188490Z  INFO tokio_cron_scheduler::job_scheduler: Uninited
2023-04-17T12:56:24.188752Z  INFO tokio_cron_scheduler::job_scheduler: Job creator created
thread 'main' panicked at 'Cannot start a runtime from within a runtime. This happens because a function (like `block_on`) attempted to block the current thread while the thread is being used to drive asynchronous tasks.', /home/grunch/.cargo/registry/src/git.luolix.top-1ecc6299db9ec823/tokio-1.27.0/src/runtime/scheduler/multi_thread/mod.rs:65:25

And here is a verbose backtrace

RUST_BACKTRACE=1 RUST_LOG=info cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.26s
     Running `target/debug/mostro`
 INFO  sqlx::query > PRAGMA foreign_keys = ON; …; rows affected: 0, rows returned: 0, elapsed: 50.077µs

PRAGMA foreign_keys = ON;

 INFO  nostr_sdk::relay > Connected to ws://localhost:7000/
2023-04-17T12:56:48.459441Z  INFO mostro::scheduler: Creating scheduler
2023-04-17T12:56:48.460436Z  INFO tokio_cron_scheduler::job_scheduler: Uninited
2023-04-17T12:56:48.460775Z  INFO tokio_cron_scheduler::job_scheduler: Job creator created
thread 'main' panicked at 'Cannot start a runtime from within a runtime. This happens because a function (like `block_on`) attempted to block the current thread while the thread is being used to drive asynchronous tasks.', /home/grunch/.cargo/registry/src/git.luolix.top-1ecc6299db9ec823/tokio-1.27.0/src/runtime/scheduler/multi_thread/mod.rs:65:25
stack backtrace:
   0: rust_begin_unwind
             at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/std/src/panicking.rs:575:5
   1: core::panicking::panic_fmt
             at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/core/src/panicking.rs:65:14
   2: tokio::runtime::context::enter_runtime
             at /home/grunch/.cargo/registry/src/git.luolix.top-1ecc6299db9ec823/tokio-1.27.0/src/runtime/context.rs:183:9
   3: tokio::runtime::scheduler::multi_thread::MultiThread::block_on
             at /home/grunch/.cargo/registry/src/git.luolix.top-1ecc6299db9ec823/tokio-1.27.0/src/runtime/scheduler/multi_thread/mod.rs:65:25
   4: tokio::runtime::runtime::Runtime::block_on
             at /home/grunch/.cargo/registry/src/git.luolix.top-1ecc6299db9ec823/tokio-1.27.0/src/runtime/runtime.rs:304:45
   5: mostro::app::run
             at ./src/app.rs:29:5
   6: mostro::main::{{closure}}
             at ./src/main.rs:39:5
   7: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
             at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/core/src/future/mod.rs:91:19
   8: tokio::runtime::park::CachedParkThread::block_on::{{closure}}
             at /home/grunch/.cargo/registry/src/git.luolix.top-1ecc6299db9ec823/tokio-1.27.0/src/runtime/park.rs:283:63
   9: tokio::runtime::coop::with_budget
             at /home/grunch/.cargo/registry/src/git.luolix.top-1ecc6299db9ec823/tokio-1.27.0/src/runtime/coop.rs:107:5
  10: tokio::runtime::coop::budget
             at /home/grunch/.cargo/registry/src/git.luolix.top-1ecc6299db9ec823/tokio-1.27.0/src/runtime/coop.rs:73:5
  11: tokio::runtime::park::CachedParkThread::block_on
             at /home/grunch/.cargo/registry/src/git.luolix.top-1ecc6299db9ec823/tokio-1.27.0/src/runtime/park.rs:283:31
  12: tokio::runtime::context::BlockingRegionGuard::block_on
             at /home/grunch/.cargo/registry/src/git.luolix.top-1ecc6299db9ec823/tokio-1.27.0/src/runtime/context.rs:315:13
  13: tokio::runtime::scheduler::multi_thread::MultiThread::block_on
             at /home/grunch/.cargo/registry/src/git.luolix.top-1ecc6299db9ec823/tokio-1.27.0/src/runtime/scheduler/multi_thread/mod.rs:66:9
  14: tokio::runtime::runtime::Runtime::block_on
             at /home/grunch/.cargo/registry/src/git.luolix.top-1ecc6299db9ec823/tokio-1.27.0/src/runtime/runtime.rs:304:45
  15: mostro::main
             at ./src/main.rs:39:5
  16: core::ops::function::FnOnce::call_once
             at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/core/src/ops/function.rs:251:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Fix nested tokio runtime issue by making app::run async and awaiting it in main
Copy link
Member

@grunch grunch left a comment

Choose a reason for hiding this comment

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

thanks now is working, the only thing left is we are getting a lot of warnings, can you run a cargo clippy and fix them please?

Copy link
Member

@grunch grunch left a comment

Choose a reason for hiding this comment

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

LGTM!

@grunch grunch merged commit dfcce2a into MostroP2P:main Apr 17, 2023
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