Skip to content

Commit

Permalink
fix(gRPC): scheduler API standardized naming
Browse files Browse the repository at this point in the history
  • Loading branch information
kwkwc committed Jun 2, 2024
1 parent 2bf131f commit c6b97c2
Show file tree
Hide file tree
Showing 19 changed files with 231 additions and 225 deletions.
12 changes: 6 additions & 6 deletions examples/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ func runExampleGRPC(c pb.SchedulerClient) {
job3 = agscheduler.PbJobPtrToJob(pbJob3)
slog.Info(fmt.Sprintf("%s.\n\n", job3))

pbJobs, _ := c.GetAllJobs(ctx, &emptypb.Empty{})
jobs := agscheduler.PbJobsPtrToJobs(pbJobs)
jsResp, _ := c.GetAllJobs(ctx, &emptypb.Empty{})
jobs := agscheduler.PbJobsPtrToJobs(jsResp.Jobs)
slog.Info(fmt.Sprintf("Scheduler get all jobs %s.\n\n", jobs))

slog.Info("Sleep 5s......\n\n")
time.Sleep(5 * time.Second)

pbJob1, _ = c.GetJob(ctx, &pb.JobId{Id: job1.Id})
pbJob1, _ = c.GetJob(ctx, &pb.JobReq{Id: job1.Id})
job1 = agscheduler.PbJobPtrToJob(pbJob1)
slog.Info(fmt.Sprintf("Scheduler get job `%s` %s.\n\n", job1.FullName(), job1))

Expand All @@ -85,16 +85,16 @@ func runExampleGRPC(c pb.SchedulerClient) {
slog.Info("Sleep 4s......")
time.Sleep(4 * time.Second)

pbJob1, _ = c.PauseJob(ctx, &pb.JobId{Id: job1.Id})
pbJob1, _ = c.PauseJob(ctx, &pb.JobReq{Id: job1.Id})
job1 = agscheduler.PbJobPtrToJob(pbJob1)

slog.Info("Sleep 3s......\n\n")
time.Sleep(3 * time.Second)

pbJob1, _ = c.ResumeJob(ctx, &pb.JobId{Id: job1.Id})
pbJob1, _ = c.ResumeJob(ctx, &pb.JobReq{Id: job1.Id})
job1 = agscheduler.PbJobPtrToJob(pbJob1)

c.DeleteJob(ctx, &pb.JobId{Id: job2.Id})
c.DeleteJob(ctx, &pb.JobReq{Id: job2.Id})

slog.Info("Sleep 4s......\n\n")
time.Sleep(4 * time.Second)
Expand Down
4 changes: 2 additions & 2 deletions examples/grpc/python/proto/recorder_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions examples/grpc/python/proto/recorder_pb2_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __init__(self, channel):
)
self.DeleteRecords = channel.unary_unary(
'/services.Recorder/DeleteRecords',
request_serializer=scheduler__pb2.JobId.SerializeToString,
request_serializer=scheduler__pb2.JobReq.SerializeToString,
response_deserializer=google_dot_protobuf_dot_empty__pb2.Empty.FromString,
)
self.DeleteAllRecords = channel.unary_unary(
Expand Down Expand Up @@ -80,7 +80,7 @@ def add_RecorderServicer_to_server(servicer, server):
),
'DeleteRecords': grpc.unary_unary_rpc_method_handler(
servicer.DeleteRecords,
request_deserializer=scheduler__pb2.JobId.FromString,
request_deserializer=scheduler__pb2.JobReq.FromString,
response_serializer=google_dot_protobuf_dot_empty__pb2.Empty.SerializeToString,
),
'DeleteAllRecords': grpc.unary_unary_rpc_method_handler(
Expand Down Expand Up @@ -144,7 +144,7 @@ def DeleteRecords(request,
timeout=None,
metadata=None):
return grpc.experimental.unary_unary(request, target, '/services.Recorder/DeleteRecords',
scheduler__pb2.JobId.SerializeToString,
scheduler__pb2.JobReq.SerializeToString,
google_dot_protobuf_dot_empty__pb2.Empty.FromString,
options, channel_credentials,
insecure, call_credentials, compression, wait_for_ready, timeout, metadata)
Expand Down
18 changes: 9 additions & 9 deletions examples/grpc/python/proto/scheduler_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/grpc/python/proto/scheduler_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ from typing import ClassVar as _ClassVar, Iterable as _Iterable, Mapping as _Map

DESCRIPTOR: _descriptor.FileDescriptor

class JobId(_message.Message):
class JobReq(_message.Message):
__slots__ = ("id",)
ID_FIELD_NUMBER: _ClassVar[int]
id: str
Expand Down Expand Up @@ -48,7 +48,7 @@ class Job(_message.Message):
status: str
def __init__(self, id: _Optional[str] = ..., name: _Optional[str] = ..., type: _Optional[str] = ..., start_at: _Optional[str] = ..., end_at: _Optional[str] = ..., interval: _Optional[str] = ..., cron_expr: _Optional[str] = ..., timezone: _Optional[str] = ..., func_name: _Optional[str] = ..., args: _Optional[_Union[_struct_pb2.Struct, _Mapping]] = ..., timeout: _Optional[str] = ..., queues: _Optional[_Iterable[str]] = ..., last_run_time: _Optional[_Union[_timestamp_pb2.Timestamp, _Mapping]] = ..., next_run_time: _Optional[_Union[_timestamp_pb2.Timestamp, _Mapping]] = ..., status: _Optional[str] = ...) -> None: ...

class Jobs(_message.Message):
class JobsResp(_message.Message):
__slots__ = ("jobs",)
JOBS_FIELD_NUMBER: _ClassVar[int]
jobs: _containers.RepeatedCompositeFieldContainer[Job]
Expand Down
30 changes: 15 additions & 15 deletions examples/grpc/python/proto/scheduler_pb2_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ def __init__(self, channel):
)
self.GetJob = channel.unary_unary(
'/services.Scheduler/GetJob',
request_serializer=scheduler__pb2.JobId.SerializeToString,
request_serializer=scheduler__pb2.JobReq.SerializeToString,
response_deserializer=scheduler__pb2.Job.FromString,
)
self.GetAllJobs = channel.unary_unary(
'/services.Scheduler/GetAllJobs',
request_serializer=google_dot_protobuf_dot_empty__pb2.Empty.SerializeToString,
response_deserializer=scheduler__pb2.Jobs.FromString,
response_deserializer=scheduler__pb2.JobsResp.FromString,
)
self.UpdateJob = channel.unary_unary(
'/services.Scheduler/UpdateJob',
Expand All @@ -37,7 +37,7 @@ def __init__(self, channel):
)
self.DeleteJob = channel.unary_unary(
'/services.Scheduler/DeleteJob',
request_serializer=scheduler__pb2.JobId.SerializeToString,
request_serializer=scheduler__pb2.JobReq.SerializeToString,
response_deserializer=google_dot_protobuf_dot_empty__pb2.Empty.FromString,
)
self.DeleteAllJobs = channel.unary_unary(
Expand All @@ -47,12 +47,12 @@ def __init__(self, channel):
)
self.PauseJob = channel.unary_unary(
'/services.Scheduler/PauseJob',
request_serializer=scheduler__pb2.JobId.SerializeToString,
request_serializer=scheduler__pb2.JobReq.SerializeToString,
response_deserializer=scheduler__pb2.Job.FromString,
)
self.ResumeJob = channel.unary_unary(
'/services.Scheduler/ResumeJob',
request_serializer=scheduler__pb2.JobId.SerializeToString,
request_serializer=scheduler__pb2.JobReq.SerializeToString,
response_deserializer=scheduler__pb2.Job.FromString,
)
self.RunJob = channel.unary_unary(
Expand Down Expand Up @@ -162,13 +162,13 @@ def add_SchedulerServicer_to_server(servicer, server):
),
'GetJob': grpc.unary_unary_rpc_method_handler(
servicer.GetJob,
request_deserializer=scheduler__pb2.JobId.FromString,
request_deserializer=scheduler__pb2.JobReq.FromString,
response_serializer=scheduler__pb2.Job.SerializeToString,
),
'GetAllJobs': grpc.unary_unary_rpc_method_handler(
servicer.GetAllJobs,
request_deserializer=google_dot_protobuf_dot_empty__pb2.Empty.FromString,
response_serializer=scheduler__pb2.Jobs.SerializeToString,
response_serializer=scheduler__pb2.JobsResp.SerializeToString,
),
'UpdateJob': grpc.unary_unary_rpc_method_handler(
servicer.UpdateJob,
Expand All @@ -177,7 +177,7 @@ def add_SchedulerServicer_to_server(servicer, server):
),
'DeleteJob': grpc.unary_unary_rpc_method_handler(
servicer.DeleteJob,
request_deserializer=scheduler__pb2.JobId.FromString,
request_deserializer=scheduler__pb2.JobReq.FromString,
response_serializer=google_dot_protobuf_dot_empty__pb2.Empty.SerializeToString,
),
'DeleteAllJobs': grpc.unary_unary_rpc_method_handler(
Expand All @@ -187,12 +187,12 @@ def add_SchedulerServicer_to_server(servicer, server):
),
'PauseJob': grpc.unary_unary_rpc_method_handler(
servicer.PauseJob,
request_deserializer=scheduler__pb2.JobId.FromString,
request_deserializer=scheduler__pb2.JobReq.FromString,
response_serializer=scheduler__pb2.Job.SerializeToString,
),
'ResumeJob': grpc.unary_unary_rpc_method_handler(
servicer.ResumeJob,
request_deserializer=scheduler__pb2.JobId.FromString,
request_deserializer=scheduler__pb2.JobReq.FromString,
response_serializer=scheduler__pb2.Job.SerializeToString,
),
'RunJob': grpc.unary_unary_rpc_method_handler(
Expand Down Expand Up @@ -254,7 +254,7 @@ def GetJob(request,
timeout=None,
metadata=None):
return grpc.experimental.unary_unary(request, target, '/services.Scheduler/GetJob',
scheduler__pb2.JobId.SerializeToString,
scheduler__pb2.JobReq.SerializeToString,
scheduler__pb2.Job.FromString,
options, channel_credentials,
insecure, call_credentials, compression, wait_for_ready, timeout, metadata)
Expand All @@ -272,7 +272,7 @@ def GetAllJobs(request,
metadata=None):
return grpc.experimental.unary_unary(request, target, '/services.Scheduler/GetAllJobs',
google_dot_protobuf_dot_empty__pb2.Empty.SerializeToString,
scheduler__pb2.Jobs.FromString,
scheduler__pb2.JobsResp.FromString,
options, channel_credentials,
insecure, call_credentials, compression, wait_for_ready, timeout, metadata)

Expand Down Expand Up @@ -305,7 +305,7 @@ def DeleteJob(request,
timeout=None,
metadata=None):
return grpc.experimental.unary_unary(request, target, '/services.Scheduler/DeleteJob',
scheduler__pb2.JobId.SerializeToString,
scheduler__pb2.JobReq.SerializeToString,
google_dot_protobuf_dot_empty__pb2.Empty.FromString,
options, channel_credentials,
insecure, call_credentials, compression, wait_for_ready, timeout, metadata)
Expand Down Expand Up @@ -339,7 +339,7 @@ def PauseJob(request,
timeout=None,
metadata=None):
return grpc.experimental.unary_unary(request, target, '/services.Scheduler/PauseJob',
scheduler__pb2.JobId.SerializeToString,
scheduler__pb2.JobReq.SerializeToString,
scheduler__pb2.Job.FromString,
options, channel_credentials,
insecure, call_credentials, compression, wait_for_ready, timeout, metadata)
Expand All @@ -356,7 +356,7 @@ def ResumeJob(request,
timeout=None,
metadata=None):
return grpc.experimental.unary_unary(request, target, '/services.Scheduler/ResumeJob',
scheduler__pb2.JobId.SerializeToString,
scheduler__pb2.JobReq.SerializeToString,
scheduler__pb2.Job.FromString,
options, channel_credentials,
insecure, call_credentials, compression, wait_for_ready, timeout, metadata)
Expand Down
14 changes: 7 additions & 7 deletions job.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,26 +258,26 @@ func PbJobPtrToJob(pbJob *pb.Job) Job {
}

// Used to gRPC Protobuf
func JobsToPbJobsPtr(js []Job) (*pb.Jobs, error) {
pbJs := &pb.Jobs{}
func JobsToPbJobsPtr(js []Job) ([]*pb.Job, error) {
pbJs := []*pb.Job{}

for _, j := range js {
pbJ, err := JobToPbJobPtr(j)
if err != nil {
return &pb.Jobs{}, err
return []*pb.Job{}, err

Check warning on line 267 in job.go

View check run for this annotation

Codecov / codecov/patch

job.go#L267

Added line #L267 was not covered by tests
}

pbJs.Jobs = append(pbJs.Jobs, pbJ)
pbJs = append(pbJs, pbJ)
}

return pbJs, nil
}

// Used to gRPC Protobuf
func PbJobsPtrToJobs(pbJs *pb.Jobs) []Job {
js := make([]Job, 0)
func PbJobsPtrToJobs(pbJs []*pb.Job) []Job {
js := []Job{}

for _, pbJ := range pbJs.Jobs {
for _, pbJ := range pbJs {
js = append(js, PbJobPtrToJob(pbJ))
}

Expand Down
4 changes: 2 additions & 2 deletions job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ func TestJobsToPbJobsPtr(t *testing.T) {
pbJs, err := JobsToPbJobsPtr(js)
assert.NoError(t, err)

assert.IsType(t, &pb.Jobs{}, pbJs)
assert.Len(t, pbJs.Jobs, 2)
assert.IsType(t, []*pb.Job{}, pbJs)
assert.Len(t, pbJs, 2)
}

func TestPbJobsPtrToJobs(t *testing.T) {
Expand Down
Loading

0 comments on commit c6b97c2

Please sign in to comment.