diff --git a/scrapers/cron.go b/scrapers/cron.go index 7023fbbbb..bdc139bc2 100644 --- a/scrapers/cron.go +++ b/scrapers/cron.go @@ -136,9 +136,9 @@ func SyncScrapeJob(sc api.ScrapeContext) error { return nil } -func scheduleScraperJob(sc api.ScrapeContext) error { +func newScraperJob(sc api.ScrapeContext) *job.Job { schedule, _ := lo.Coalesce(sc.ScrapeConfig().Spec.Schedule, DefaultSchedule) - j := &job.Job{ + return &job.Job{ Name: "Scraper", Context: sc.DutyContext().WithObject(sc.ScrapeConfig().ObjectMeta).WithAnyValue("scraper", sc.ScrapeConfig()), Schedule: schedule, @@ -159,6 +159,10 @@ func scheduleScraperJob(sc api.ScrapeContext) error { return nil }, } +} + +func scheduleScraperJob(sc api.ScrapeContext) error { + j := newScraperJob(sc) scrapeJobs.Store(sc.ScrapeConfig().GetPersistedID().String(), j) if err := j.AddToScheduler(scrapeJobScheduler); err != nil { diff --git a/scrapers/run_now.go b/scrapers/run_now.go index 38ba03ba3..11c7ca94a 100644 --- a/scrapers/run_now.go +++ b/scrapers/run_now.go @@ -28,16 +28,14 @@ func RunNowHandler(c echo.Context) error { } ctx := api.DefaultContext.WithScrapeConfig(&configScraper) - results, err := RunScraper(ctx) - if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "failed to run scraper", err) - } - + j := newScraperJob(ctx) + j.Run() res := v1.RunNowResponse{ - Total: len(results), - Errors: results.Errors(), + Total: j.LastJob.SuccessCount + j.LastJob.ErrorCount, + Errors: j.LastJob.Errors, + Failed: len(j.LastJob.Errors), + Success: j.LastJob.SuccessCount, } - res.Failed = len(res.Errors) - res.Success = res.Total - res.Failed + return c.JSON(http.StatusOK, res) }