summaryrefslogtreecommitdiff
path: root/plugins/jobs/rpc.go
blob: c6bd1645bbfd9a310a3608bd5e85b0414ddab576 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package jobs

import (
	"sync"

	"github.com/spiral/errors"
	"github.com/spiral/roadrunner/v2/plugins/jobs/structs"
	"github.com/spiral/roadrunner/v2/plugins/logger"
	jobsv1beta "github.com/spiral/roadrunner/v2/proto/jobs/v1beta"
)

type rpc struct {
	log logger.Logger
	p   *Plugin
}

var jobsPool = &sync.Pool{
	New: func() interface{} {
		return &structs.Job{
			Options: &structs.Options{},
		}
	},
}

func pubJob(j *structs.Job) {
	// clear
	j.Job = ""
	j.Payload = ""
	j.Options = &structs.Options{}
	jobsPool.Put(j)
}

func getJob() *structs.Job {
	return jobsPool.Get().(*structs.Job)
}

/*
List of the RPC methods:
1. Push - single job push
2. PushBatch - push job batch

3. Reset - managed by the Resetter plugin

4. Stop - stop pipeline processing
5. StopAll - stop all pipelines processing
6. Resume - resume pipeline processing
7. ResumeAll - resume stopped pipelines

8. Workers - managed by the Informer plugin.
9. Stat - jobs statistic
*/

func (r *rpc) Push(j *jobsv1beta.Request, resp *jobsv1beta.Response) error {
	const op = errors.Op("jobs_rpc_push")

	// convert transport entity into domain
	// how we can do this quickly
	jb := getJob()
	defer pubJob(jb)

	jb = &structs.Job{
		Job:     j.GetJob().Job,
		Payload: j.GetJob().Payload,
		Options: &structs.Options{
			Priority:   &j.GetJob().Options.Priority,
			ID:         &j.GetJob().Options.Id,
			Pipeline:   j.GetJob().Options.Pipeline,
			Delay:      j.GetJob().Options.Delay,
			Attempts:   j.GetJob().Options.Attempts,
			RetryDelay: j.GetJob().Options.RetryDelay,
			Timeout:    j.GetJob().Options.Timeout,
		},
	}
	id, err := r.p.Push(jb)
	if err != nil {
		return errors.E(op, err)
	}

	resp.Id = *id

	return nil
}

func (r *rpc) PushBatch(j *structs.Job, idRet *string) error {
	const op = errors.Op("jobs_rpc_push")
	id, err := r.p.Push(j)
	if err != nil {
		return errors.E(op, err)
	}

	*idRet = *id
	return nil
}

func (r *rpc) Stop(pipeline string, w *string) error {
	return nil
}

func (r *rpc) StopAll(_ bool, w *string) error {
	return nil
}

func (r *rpc) Resume(pipeline string, w *string) error {
	return nil
}

func (r *rpc) ResumeAll(_ bool, w *string) error {
	return nil
}