2021-05-24 04:24:18 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-03-17 00:33:59 +00:00
|
|
|
"github.com/stashapp/stash/internal/manager"
|
2021-05-24 04:24:18 +00:00
|
|
|
"github.com/stashapp/stash/pkg/job"
|
|
|
|
)
|
|
|
|
|
2022-04-25 05:55:05 +00:00
|
|
|
func makeJobStatusUpdate(t JobStatusUpdateType, j job.Job) *JobStatusUpdate {
|
|
|
|
return &JobStatusUpdate{
|
2021-05-24 04:24:18 +00:00
|
|
|
Type: t,
|
|
|
|
Job: jobToJobModel(j),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 05:55:05 +00:00
|
|
|
func (r *subscriptionResolver) JobsSubscribe(ctx context.Context) (<-chan *JobStatusUpdate, error) {
|
|
|
|
msg := make(chan *JobStatusUpdate, 100)
|
2021-05-24 04:24:18 +00:00
|
|
|
|
|
|
|
subscription := manager.GetInstance().JobManager.Subscribe(ctx)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case j := <-subscription.NewJob:
|
2022-04-25 05:55:05 +00:00
|
|
|
msg <- makeJobStatusUpdate(JobStatusUpdateTypeAdd, j)
|
2021-05-24 04:24:18 +00:00
|
|
|
case j := <-subscription.RemovedJob:
|
2022-04-25 05:55:05 +00:00
|
|
|
msg <- makeJobStatusUpdate(JobStatusUpdateTypeRemove, j)
|
2021-05-24 04:24:18 +00:00
|
|
|
case j := <-subscription.UpdatedJob:
|
2022-04-25 05:55:05 +00:00
|
|
|
msg <- makeJobStatusUpdate(JobStatusUpdateTypeUpdate, j)
|
2021-05-24 04:24:18 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
close(msg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *subscriptionResolver) ScanCompleteSubscribe(ctx context.Context) (<-chan bool, error) {
|
|
|
|
return manager.GetInstance().ScanSubscribe(ctx), nil
|
|
|
|
}
|