yuluo-yx commented on code in PR #11:
URL:
https://github.com/apache/hertzbeat-collector-go/pull/11#discussion_r2330431024
##########
internal/collector/common/job/job_server.go:
##########
@@ -17,51 +17,191 @@
* under the License.
*/
-package transport
+package job
import (
"context"
+ "fmt"
+ "sync"
+
"hertzbeat.apache.org/hertzbeat-collector-go/internal/collector/common/collect"
+
"hertzbeat.apache.org/hertzbeat-collector-go/internal/collector/common/collect/dispatch"
+
"hertzbeat.apache.org/hertzbeat-collector-go/internal/collector/common/dispatcher"
clrServer
"hertzbeat.apache.org/hertzbeat-collector-go/internal/collector/common/server"
"hertzbeat.apache.org/hertzbeat-collector-go/internal/collector/common/types/collector"
+ jobtypes
"hertzbeat.apache.org/hertzbeat-collector-go/internal/collector/common/types/job"
)
+// TimeDispatcher interface defines time-based job scheduling
+type TimeDispatcher interface {
+ AddJob(job *jobtypes.Job) error
+ RemoveJob(jobID int64) error
+ Start(ctx context.Context) error
+ Stop() error
+}
+
+// Config represents job service configuration
type Config struct {
clrServer.Server
}
+// Runner implements the service runner interface
type Runner struct {
Config
+ timeDispatch TimeDispatcher
+ mu sync.RWMutex
+ runningJobs map[int64]*jobtypes.Job
+ ctx context.Context
+ cancel context.CancelFunc
+}
+
+// AddAsyncCollectJob adds a job to async collection scheduling
+func (r *Runner) AddAsyncCollectJob(job *jobtypes.Job) error {
+ if job == nil {
+ r.Logger.Error(nil, "job cannot be nil")
+ return fmt.Errorf("job cannot be nil")
+ }
+
+ r.Logger.Info("adding async collect job",
+ "jobID", job.ID,
+ "monitorID", job.MonitorID,
+ "app", job.App,
+ "isCyclic", job.IsCyclic)
+
+ r.mu.Lock()
+ defer r.mu.Unlock()
+
+ // Store job in running jobs map
+ r.runningJobs[job.ID] = job
+
+ // Add job to time dispatcher for scheduling
+ if err := r.timeDispatch.AddJob(job); err != nil {
+ delete(r.runningJobs, job.ID)
+ r.Logger.Error(err, "failed to add job to time dispatcher",
"jobID", job.ID)
+ return fmt.Errorf("failed to add job to time dispatcher: %w",
err)
Review Comment:
使用 errors.New() 以返回一个 error
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]