Usage | laravel-schedule-monitor-go | Spatie       

 SPATIE  

laravel-schedule-monitor-go
===========================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-schedule-monitor-go](https://spatie.be/docs/laravel-schedule-monitor-go/v1)  Usage

 Version   v1      

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

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

 Usage
=====

###  On this page 

1. [ Naming tasks ](#content-naming-tasks)
2. [ Setting a grace time ](#content-setting-a-grace-time)
3. [ Ignoring scheduled tasks ](#content-ignoring-scheduled-tasks)
4. [ Storing output in the database ](#content-storing-output-in-the-database)
5. [ Multitenancy ](#content-multitenancy)
6. [ Getting notified when a scheduled task doesn't finish in time ](#content-getting-notified-when-a-scheduled-task-doesnt-finish-in-time)
7. [ Disabling Oh Dear for individual tasks ](#content-disabling-oh-dear-for-individual-tasks)
8. [ Customizing the Oh Dear ping job ](#content-customizing-the-oh-dear-ping-job)

To monitor your schedule, first run `schedule-monitor:sync`. This command reads the native application schedule and creates an entry for each named task in `monitored_scheduled_tasks`.

```
go run . schedule-monitor:sync
go run . schedule-monitor:list
```

The list shows the last start, successful finish, failure and skip. It warns about duplicate names and tasks that need syncing. Duplicate names share one monitor, with the first definition used at sync. Give distinct names to tasks with different schedules.

Each event creates a log item with metrics in its `meta` JSON column. Read history through the configured store:

```
items, err := schedulemonitor.From(ctx).Logs(ctx, "daily-report")
statuses, err := schedulemonitor.From(ctx).Statuses(ctx)
```

Pass the handler request, job, command or listener context so database connections, time travel and fakes apply. History is newest first. A task absent from the last sync has no history until it is synced. Tasks excluded by `Environments` are not synced outside their environment.

Naming tasks
--------------------------------------------------------------------------------------------

Schedule monitor uses the native task's final name. Commands use their command line by default, jobs use their type name, and `Call` takes a name. Use `Name` to set a custom name:

```
func Schedule(ctx context.Context, s *schedule.Schedule) {
    s.Command("reports:generate").Name("daily-report").Daily()
    s.Call("addition", func(context.Context) error { return nil }).Hourly()
}
```

When you change a name, the next destructive sync removes the old monitor and its logs and creates a new monitor. `--keep-old` retains the old monitor.

Setting a grace time
--------------------------------------------------------------------------------------------------------------------

The list marks a task late when its first expected successful completion plus grace is in the past. Grace is the time a task normally needs to finish. By default each task gets five minutes.

```
schedulemonitor.New(func(cfg *schedulemonitor.Config) {
    grace := 10 * time.Minute
    cfg.Tasks["daily-report"] = schedulemonitor.TaskOptions{Grace: &grace}
})
```

A pointer to zero grants no grace. A nil pointer uses `cfg.Grace`. Sync after changing grace. See [completion deadlines](monitoring).

Ignoring scheduled tasks
--------------------------------------------------------------------------------------------------------------------------------

```
cfg.Tasks["local-maintenance"] = schedulemonitor.TaskOptions{Ignore: true}
```

Ignored tasks are not synced or logged. A destructive sync removes an old monitor if the task is now ignored.

Storing output in the database
--------------------------------------------------------------------------------------------------------------------------------------------------

```
cfg.Tasks["daily-report"] = schedulemonitor.TaskOptions{StoreOutput: true}
```

Also configure the native task with `SendOutputTo("storage/logs/report.log")` or `AppendOutputTo`. The directory must exist. Non-empty file contents become `meta.output` on terminal logs. Read errors return a listener error without losing the terminal event log. Functions and jobs have no output file. Output is never sent to Oh Dear.

Multitenancy
--------------------------------------------------------------------------------------------

PHP's `not_tenant_aware_jobs` configuration does not apply to Laravel Go. If your application adds tenant-aware queue middleware, make the ping job and configured store use the intended central database connection. A custom store must apply its scope consistently to sync, event writes and worker reads.

Getting notified when a scheduled task doesn't finish in time
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

This package can sync your schedule with [Oh Dear](https://ohdear.app). Set `OH_DEAR_API_TOKEN` and `OH_DEAR_MONITOR_ID`, then verify them:

```
go run . schedule-monitor:verify
go run . schedule-monitor:sync
```

After that, `list` shows which tasks are registered with Oh Dear. Its notifications work outside your scheduler, so it can alert you even when the scheduler stops running.

Pings run through native queued jobs. Use a dedicated queue to avoid false positives caused by queue delays. Set `OH_DEAR_QUEUE=monitoring`, configure a non-sync connection and run:

```
go run . queue:work --queue=monitoring
```

The database queue requires the application's `queue.CreateTables` migration. Sync queues send pings inline. Successful pings update `LastPingedAt`. Skipped tasks never send pings. Starting pings are optional. Finished pings contain runtime, exit code and memory, excluding runtime when starting pings are enabled. Failed pings contain the failure message.

Disabling Oh Dear for individual tasks
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

```
cfg.Tasks["local-maintenance"] = schedulemonitor.TaskOptions{LocalOnly: true}
```

This keeps local monitoring and clears the old ping URL and pending pings on sync. It excludes the task from remote registration. A `--keep-old` sync does not delete its already existing remote check. Remove that check manually or perform a destructive sync.

Customizing the Oh Dear ping job
--------------------------------------------------------------------------------------------------------------------------------------------------------

Use `Config.PingJob` to return an application-owned native job. See [customization](customization) for the factory, worker and model contracts.

 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)
