選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

345 行
12 KiB

  1. // Copyright 2019 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Code generated by gapic-generator. DO NOT EDIT.
  15. package scheduler
  16. import (
  17. "context"
  18. "math"
  19. "time"
  20. "github.com/golang/protobuf/proto"
  21. gax "github.com/googleapis/gax-go/v2"
  22. "google.golang.org/api/iterator"
  23. "google.golang.org/api/option"
  24. "google.golang.org/api/transport"
  25. schedulerpb "google.golang.org/genproto/googleapis/cloud/scheduler/v1"
  26. "google.golang.org/grpc"
  27. "google.golang.org/grpc/codes"
  28. "google.golang.org/grpc/metadata"
  29. )
  30. // CloudSchedulerCallOptions contains the retry settings for each method of CloudSchedulerClient.
  31. type CloudSchedulerCallOptions struct {
  32. ListJobs []gax.CallOption
  33. GetJob []gax.CallOption
  34. CreateJob []gax.CallOption
  35. UpdateJob []gax.CallOption
  36. DeleteJob []gax.CallOption
  37. PauseJob []gax.CallOption
  38. ResumeJob []gax.CallOption
  39. RunJob []gax.CallOption
  40. }
  41. func defaultCloudSchedulerClientOptions() []option.ClientOption {
  42. return []option.ClientOption{
  43. option.WithEndpoint("cloudscheduler.googleapis.com:443"),
  44. option.WithScopes(DefaultAuthScopes()...),
  45. }
  46. }
  47. func defaultCloudSchedulerCallOptions() *CloudSchedulerCallOptions {
  48. retry := map[[2]string][]gax.CallOption{
  49. {"default", "idempotent"}: {
  50. gax.WithRetry(func() gax.Retryer {
  51. return gax.OnCodes([]codes.Code{
  52. codes.DeadlineExceeded,
  53. codes.Unavailable,
  54. }, gax.Backoff{
  55. Initial: 100 * time.Millisecond,
  56. Max: 60000 * time.Millisecond,
  57. Multiplier: 1.3,
  58. })
  59. }),
  60. },
  61. }
  62. return &CloudSchedulerCallOptions{
  63. ListJobs: retry[[2]string{"default", "idempotent"}],
  64. GetJob: retry[[2]string{"default", "idempotent"}],
  65. CreateJob: retry[[2]string{"default", "non_idempotent"}],
  66. UpdateJob: retry[[2]string{"default", "non_idempotent"}],
  67. DeleteJob: retry[[2]string{"default", "idempotent"}],
  68. PauseJob: retry[[2]string{"default", "non_idempotent"}],
  69. ResumeJob: retry[[2]string{"default", "non_idempotent"}],
  70. RunJob: retry[[2]string{"default", "non_idempotent"}],
  71. }
  72. }
  73. // CloudSchedulerClient is a client for interacting with Cloud Scheduler API.
  74. //
  75. // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
  76. type CloudSchedulerClient struct {
  77. // The connection to the service.
  78. conn *grpc.ClientConn
  79. // The gRPC API client.
  80. cloudSchedulerClient schedulerpb.CloudSchedulerClient
  81. // The call options for this service.
  82. CallOptions *CloudSchedulerCallOptions
  83. // The x-goog-* metadata to be sent with each request.
  84. xGoogMetadata metadata.MD
  85. }
  86. // NewCloudSchedulerClient creates a new cloud scheduler client.
  87. //
  88. // The Cloud Scheduler API allows external entities to reliably
  89. // schedule asynchronous jobs.
  90. func NewCloudSchedulerClient(ctx context.Context, opts ...option.ClientOption) (*CloudSchedulerClient, error) {
  91. conn, err := transport.DialGRPC(ctx, append(defaultCloudSchedulerClientOptions(), opts...)...)
  92. if err != nil {
  93. return nil, err
  94. }
  95. c := &CloudSchedulerClient{
  96. conn: conn,
  97. CallOptions: defaultCloudSchedulerCallOptions(),
  98. cloudSchedulerClient: schedulerpb.NewCloudSchedulerClient(conn),
  99. }
  100. c.setGoogleClientInfo()
  101. return c, nil
  102. }
  103. // Connection returns the client's connection to the API service.
  104. func (c *CloudSchedulerClient) Connection() *grpc.ClientConn {
  105. return c.conn
  106. }
  107. // Close closes the connection to the API service. The user should invoke this when
  108. // the client is no longer required.
  109. func (c *CloudSchedulerClient) Close() error {
  110. return c.conn.Close()
  111. }
  112. // setGoogleClientInfo sets the name and version of the application in
  113. // the `x-goog-api-client` header passed on each request. Intended for
  114. // use by Google-written clients.
  115. func (c *CloudSchedulerClient) setGoogleClientInfo(keyval ...string) {
  116. kv := append([]string{"gl-go", versionGo()}, keyval...)
  117. kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
  118. c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
  119. }
  120. // ListJobs lists jobs.
  121. func (c *CloudSchedulerClient) ListJobs(ctx context.Context, req *schedulerpb.ListJobsRequest, opts ...gax.CallOption) *JobIterator {
  122. ctx = insertMetadata(ctx, c.xGoogMetadata)
  123. opts = append(c.CallOptions.ListJobs[0:len(c.CallOptions.ListJobs):len(c.CallOptions.ListJobs)], opts...)
  124. it := &JobIterator{}
  125. req = proto.Clone(req).(*schedulerpb.ListJobsRequest)
  126. it.InternalFetch = func(pageSize int, pageToken string) ([]*schedulerpb.Job, string, error) {
  127. var resp *schedulerpb.ListJobsResponse
  128. req.PageToken = pageToken
  129. if pageSize > math.MaxInt32 {
  130. req.PageSize = math.MaxInt32
  131. } else {
  132. req.PageSize = int32(pageSize)
  133. }
  134. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  135. var err error
  136. resp, err = c.cloudSchedulerClient.ListJobs(ctx, req, settings.GRPC...)
  137. return err
  138. }, opts...)
  139. if err != nil {
  140. return nil, "", err
  141. }
  142. return resp.Jobs, resp.NextPageToken, nil
  143. }
  144. fetch := func(pageSize int, pageToken string) (string, error) {
  145. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  146. if err != nil {
  147. return "", err
  148. }
  149. it.items = append(it.items, items...)
  150. return nextPageToken, nil
  151. }
  152. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  153. it.pageInfo.MaxSize = int(req.PageSize)
  154. return it
  155. }
  156. // GetJob gets a job.
  157. func (c *CloudSchedulerClient) GetJob(ctx context.Context, req *schedulerpb.GetJobRequest, opts ...gax.CallOption) (*schedulerpb.Job, error) {
  158. ctx = insertMetadata(ctx, c.xGoogMetadata)
  159. opts = append(c.CallOptions.GetJob[0:len(c.CallOptions.GetJob):len(c.CallOptions.GetJob)], opts...)
  160. var resp *schedulerpb.Job
  161. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  162. var err error
  163. resp, err = c.cloudSchedulerClient.GetJob(ctx, req, settings.GRPC...)
  164. return err
  165. }, opts...)
  166. if err != nil {
  167. return nil, err
  168. }
  169. return resp, nil
  170. }
  171. // CreateJob creates a job.
  172. func (c *CloudSchedulerClient) CreateJob(ctx context.Context, req *schedulerpb.CreateJobRequest, opts ...gax.CallOption) (*schedulerpb.Job, error) {
  173. ctx = insertMetadata(ctx, c.xGoogMetadata)
  174. opts = append(c.CallOptions.CreateJob[0:len(c.CallOptions.CreateJob):len(c.CallOptions.CreateJob)], opts...)
  175. var resp *schedulerpb.Job
  176. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  177. var err error
  178. resp, err = c.cloudSchedulerClient.CreateJob(ctx, req, settings.GRPC...)
  179. return err
  180. }, opts...)
  181. if err != nil {
  182. return nil, err
  183. }
  184. return resp, nil
  185. }
  186. // UpdateJob updates a job.
  187. //
  188. // If successful, the updated [Job][google.cloud.scheduler.v1.Job] is returned. If the job does
  189. // not exist, NOT_FOUND is returned.
  190. //
  191. // If UpdateJob does not successfully return, it is possible for the
  192. // job to be in an [Job.State.UPDATE_FAILED][google.cloud.scheduler.v1.Job.State.UPDATE_FAILED] state. A job in this state may
  193. // not be executed. If this happens, retry the UpdateJob request
  194. // until a successful response is received.
  195. func (c *CloudSchedulerClient) UpdateJob(ctx context.Context, req *schedulerpb.UpdateJobRequest, opts ...gax.CallOption) (*schedulerpb.Job, error) {
  196. ctx = insertMetadata(ctx, c.xGoogMetadata)
  197. opts = append(c.CallOptions.UpdateJob[0:len(c.CallOptions.UpdateJob):len(c.CallOptions.UpdateJob)], opts...)
  198. var resp *schedulerpb.Job
  199. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  200. var err error
  201. resp, err = c.cloudSchedulerClient.UpdateJob(ctx, req, settings.GRPC...)
  202. return err
  203. }, opts...)
  204. if err != nil {
  205. return nil, err
  206. }
  207. return resp, nil
  208. }
  209. // DeleteJob deletes a job.
  210. func (c *CloudSchedulerClient) DeleteJob(ctx context.Context, req *schedulerpb.DeleteJobRequest, opts ...gax.CallOption) error {
  211. ctx = insertMetadata(ctx, c.xGoogMetadata)
  212. opts = append(c.CallOptions.DeleteJob[0:len(c.CallOptions.DeleteJob):len(c.CallOptions.DeleteJob)], opts...)
  213. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  214. var err error
  215. _, err = c.cloudSchedulerClient.DeleteJob(ctx, req, settings.GRPC...)
  216. return err
  217. }, opts...)
  218. return err
  219. }
  220. // PauseJob pauses a job.
  221. //
  222. // If a job is paused then the system will stop executing the job
  223. // until it is re-enabled via [ResumeJob][google.cloud.scheduler.v1.CloudScheduler.ResumeJob]. The
  224. // state of the job is stored in [state][google.cloud.scheduler.v1.Job.state]; if paused it
  225. // will be set to [Job.State.PAUSED][google.cloud.scheduler.v1.Job.State.PAUSED]. A job must be in [Job.State.ENABLED][google.cloud.scheduler.v1.Job.State.ENABLED]
  226. // to be paused.
  227. func (c *CloudSchedulerClient) PauseJob(ctx context.Context, req *schedulerpb.PauseJobRequest, opts ...gax.CallOption) (*schedulerpb.Job, error) {
  228. ctx = insertMetadata(ctx, c.xGoogMetadata)
  229. opts = append(c.CallOptions.PauseJob[0:len(c.CallOptions.PauseJob):len(c.CallOptions.PauseJob)], opts...)
  230. var resp *schedulerpb.Job
  231. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  232. var err error
  233. resp, err = c.cloudSchedulerClient.PauseJob(ctx, req, settings.GRPC...)
  234. return err
  235. }, opts...)
  236. if err != nil {
  237. return nil, err
  238. }
  239. return resp, nil
  240. }
  241. // ResumeJob resume a job.
  242. //
  243. // This method reenables a job after it has been [Job.State.PAUSED][google.cloud.scheduler.v1.Job.State.PAUSED]. The
  244. // state of a job is stored in [Job.state][google.cloud.scheduler.v1.Job.state]; after calling this method it
  245. // will be set to [Job.State.ENABLED][google.cloud.scheduler.v1.Job.State.ENABLED]. A job must be in
  246. // [Job.State.PAUSED][google.cloud.scheduler.v1.Job.State.PAUSED] to be resumed.
  247. func (c *CloudSchedulerClient) ResumeJob(ctx context.Context, req *schedulerpb.ResumeJobRequest, opts ...gax.CallOption) (*schedulerpb.Job, error) {
  248. ctx = insertMetadata(ctx, c.xGoogMetadata)
  249. opts = append(c.CallOptions.ResumeJob[0:len(c.CallOptions.ResumeJob):len(c.CallOptions.ResumeJob)], opts...)
  250. var resp *schedulerpb.Job
  251. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  252. var err error
  253. resp, err = c.cloudSchedulerClient.ResumeJob(ctx, req, settings.GRPC...)
  254. return err
  255. }, opts...)
  256. if err != nil {
  257. return nil, err
  258. }
  259. return resp, nil
  260. }
  261. // RunJob forces a job to run now.
  262. //
  263. // When this method is called, Cloud Scheduler will dispatch the job, even
  264. // if the job is already running.
  265. func (c *CloudSchedulerClient) RunJob(ctx context.Context, req *schedulerpb.RunJobRequest, opts ...gax.CallOption) (*schedulerpb.Job, error) {
  266. ctx = insertMetadata(ctx, c.xGoogMetadata)
  267. opts = append(c.CallOptions.RunJob[0:len(c.CallOptions.RunJob):len(c.CallOptions.RunJob)], opts...)
  268. var resp *schedulerpb.Job
  269. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  270. var err error
  271. resp, err = c.cloudSchedulerClient.RunJob(ctx, req, settings.GRPC...)
  272. return err
  273. }, opts...)
  274. if err != nil {
  275. return nil, err
  276. }
  277. return resp, nil
  278. }
  279. // JobIterator manages a stream of *schedulerpb.Job.
  280. type JobIterator struct {
  281. items []*schedulerpb.Job
  282. pageInfo *iterator.PageInfo
  283. nextFunc func() error
  284. // InternalFetch is for use by the Google Cloud Libraries only.
  285. // It is not part of the stable interface of this package.
  286. //
  287. // InternalFetch returns results from a single call to the underlying RPC.
  288. // The number of results is no greater than pageSize.
  289. // If there are no more results, nextPageToken is empty and err is nil.
  290. InternalFetch func(pageSize int, pageToken string) (results []*schedulerpb.Job, nextPageToken string, err error)
  291. }
  292. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  293. func (it *JobIterator) PageInfo() *iterator.PageInfo {
  294. return it.pageInfo
  295. }
  296. // Next returns the next result. Its second return value is iterator.Done if there are no more
  297. // results. Once Next returns Done, all subsequent calls will return Done.
  298. func (it *JobIterator) Next() (*schedulerpb.Job, error) {
  299. var item *schedulerpb.Job
  300. if err := it.nextFunc(); err != nil {
  301. return item, err
  302. }
  303. item = it.items[0]
  304. it.items = it.items[1:]
  305. return item, nil
  306. }
  307. func (it *JobIterator) bufLen() int {
  308. return len(it.items)
  309. }
  310. func (it *JobIterator) takeBuf() interface{} {
  311. b := it.items
  312. it.items = nil
  313. return b
  314. }