Registering application commands | laravel-schedule-monitor-rust | Spatie       

 SPATIE  

laravel-schedule-monitor-rust
=============================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-schedule-monitor-rust](https://spatie.be/docs/laravel-schedule-monitor-rust/v1)  Registering application commands

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/laravel-schedule-monitor-rust/v1) 

  Registering application commands    
- [ Introduction ](https://spatie.be/docs/laravel-schedule-monitor-rust/v1/introduction)
- [ Support us ](https://spatie.be/docs/laravel-schedule-monitor-rust/v1/support-us)
- [ Installation ](https://spatie.be/docs/laravel-schedule-monitor-rust/v1/installation-and-setup)
- [ Usage ](https://spatie.be/docs/laravel-schedule-monitor-rust/v1/usage)
- [ Monitoring and Oh Dear ](https://spatie.be/docs/laravel-schedule-monitor-rust/v1/monitoring)
- [ Testing ](https://spatie.be/docs/laravel-schedule-monitor-rust/v1/testing)
- [ Changelog ](https://spatie.be/docs/laravel-schedule-monitor-rust/v1/changelog)
- [ Customization ](https://spatie.be/docs/laravel-schedule-monitor-rust/v1/customization)
- [ Registering application commands ](https://spatie.be/docs/laravel-schedule-monitor-rust/v1/scheduled-commands)

 Registering application commands
================================

- Register the command with the application's console before scheduling it.
- Give each schedule a unique monitor name, even when several schedules run the same command.
- Attach the validated definition inside `with_schedule`, then add native hooks and locks.

This example executes a console command through the scheduler and checks its history:

```
use laravel::{Application, config::{Env, database::ConnectionConfig}, console::clap};
use laravel::testing::MakesConsoleRequests;
use spatie_schedule_monitor::{MonitoredTask, ScheduleMonitor};

#[derive(clap::Parser)]
#[command(name = "app:cleanup")]
struct Cleanup;

#[laravel::console::command]
impl Cleanup {
    async fn handle(self) -> u8 { 0 }
}

let cleanup = MonitoredTask::new("cleanup", "0 2 * * *")?
    .timezone("Europe/Brussels")?
    .grace_minutes(10);
let monitor = ScheduleMonitor::new([cleanup.clone()])?;
let app = monitor.clone().install(
    Application::configure()
        .replace_env(Env::default())
        .with_config(|config, _| {
            let mut connection = ConnectionConfig::new("sqlite::memory:");
            connection.max_connections = 1;
            config.database.add_connection("monitor", connection);
            config.database.default = "monitor".into();
        })
        .with_commands(|commands, _| { commands.add::<Cleanup>(); })
        .with_schedule(move |schedule, _| {
            cleanup.register(schedule.command("app:cleanup"))
                .without_overlapping();
        })
).create();
app.travel_to("2026-11-30T01:00:00Z".parse::<laravel::time::Timestamp>()?);
app.scope(async || monitor.migrate().await).await?;
app.command("schedule-monitor:sync").await.assert_successful();
app.command("schedule:run").await.assert_successful();
app.scope(async || {
    let logs = monitor.logs("cleanup").await?;
    assert_eq!(logs.len(), 2);
    assert_eq!(logs[1].kind, "finished");
    assert_eq!(logs[1].exit_code, Some(0));
    Ok::<_, laravel::Error>(())
}).await?;
```

- Keep optional definitions under the same condition as their native registration.
- Use `schedule.job(job)` when monitoring queue dispatch, not worker completion.
- Use `schedule-monitor:prune --days 30` to prune monitor logs. Application model pruning is separate.
- Commands run concurrently unless configured otherwise. The scheduler waits for completion; there is no need to detach a long-running command.
- Set grace for expected runtime, and check the host's process timeout independently.
- See [timezones](usage#timezones) for local schedules, DST gaps and repeated minutes.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
