// Add ...
func (r *JobsInMemoryWithMutex) Add(job entity.Job) (entity.Job, error) {
	job.ID = uuid.New()
	job.Status = entity.JobStatusPending

	r.Lock()
	defer r.Unlock()

	r.jobIndexByID = append(r.jobIndexByID, job.ID)
	r.jobsByID[job.ID] = &job

	jobTime, err := job.Schedule.NextTimestamp()
	if err != nil {
		return entity.Job{}, err
	}
	if r.jobsBySchedule[jobTime] == nil {
		r.jobsBySchedule[jobTime] = make([]*entity.Job, 0)
	}
	r.jobsBySchedule[jobTime] = append(r.jobsBySchedule[jobTime], &job)

	return job, nil
}
Exemple #2
0
func (r *JobsInMemoryWithChannels) add(job *entity.Job, res chan response) {
	if job.ID != "" {
		res <- response{job: job, err: fmt.Errorf("cannot add an existing job")}
		return
	}

	timestamp, err := job.Schedule.NextTimestamp()
	if err != nil {
		res <- response{job: job, err: fmt.Errorf("invalid job schedule: %v", err)}
		return
	}

	job.ID = uuid.New()
	job.Status = entity.JobStatusPending
	r.JobsByID[job.ID] = job

	if r.JobsBySchedule[timestamp] == nil {
		r.JobsBySchedule[timestamp] = make([]*entity.Job, 0)
	}
	r.JobsBySchedule[timestamp] = append(r.JobsBySchedule[timestamp], job)
	res <- response{job: job, err: nil}
}