job_runs.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package porter_app
  2. import (
  3. "context"
  4. "time"
  5. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  6. "github.com/porter-dev/porter/internal/telemetry"
  7. )
  8. // JobRunStatus represents the status of a job run
  9. type JobRunStatus string
  10. // JobRun is a representation of a job run on the cluster
  11. type JobRun struct {
  12. // ID is the UID of the job
  13. ID string `json:"id"`
  14. // Name is the name of the job object
  15. Name string `json:"name"`
  16. // Status is the status of the job run
  17. Status JobRunStatus `json:"status"`
  18. // CreatedAt is the time the job was created
  19. CreatedAt time.Time `json:"created_at"`
  20. // FinishedAt is the time the job finished, if applicable
  21. FinishedAt time.Time `json:"finished_at"`
  22. // AppRevisionID is the ID of the app revision associated with this run
  23. AppRevisionID string `json:"app_revision_id"`
  24. // ServiceName is the name of the job service on the app
  25. ServiceName string `json:"service_name"`
  26. }
  27. const (
  28. // JobRunStatus_Running represents a job run that is currently running
  29. JobRunStatus_Running JobRunStatus = "RUNNING"
  30. // JobRunStatus_Successful represents a job run that has completed successfully
  31. JobRunStatus_Successful JobRunStatus = "SUCCESSFUL"
  32. // JobRunStatus_Failed represents a job run that has failed
  33. JobRunStatus_Failed JobRunStatus = "FAILED"
  34. // JobRunStatus_Canceled represents a job run that has been cancelled
  35. JobRunStatus_Canceled JobRunStatus = "CANCELED"
  36. )
  37. // JobRunFromProto converts a job run proto to a JobRun
  38. func JobRunFromProto(ctx context.Context, jobRun *porterv1.JobRun) (JobRun, error) {
  39. ctx, span := telemetry.NewSpan(ctx, "porter-app-job-runs-from-proto")
  40. defer span.End()
  41. var run JobRun
  42. if jobRun == nil {
  43. return run, telemetry.Error(ctx, span, nil, "job run is nil")
  44. }
  45. status, err := jobStatusFromProto(ctx, jobRun.Status)
  46. if err != nil {
  47. return run, telemetry.Error(ctx, span, err, "job status from proto")
  48. }
  49. run = JobRun{
  50. ID: jobRun.Id,
  51. Name: jobRun.JobName,
  52. ServiceName: jobRun.ServiceName,
  53. Status: status,
  54. CreatedAt: jobRun.CreatedAt.AsTime(),
  55. FinishedAt: jobRun.FinishedAt.AsTime(),
  56. AppRevisionID: jobRun.AppRevisionId,
  57. }
  58. return run, nil
  59. }
  60. func jobStatusFromProto(ctx context.Context, status porterv1.EnumJobRunStatus) (JobRunStatus, error) {
  61. ctx, span := telemetry.NewSpan(ctx, "porter-app-job-status-from-proto")
  62. defer span.End()
  63. switch status {
  64. case porterv1.EnumJobRunStatus_ENUM_JOB_RUN_STATUS_RUNNING:
  65. return JobRunStatus_Running, nil
  66. case porterv1.EnumJobRunStatus_ENUM_JOB_RUN_STATUS_SUCCESSFUL:
  67. return JobRunStatus_Successful, nil
  68. case porterv1.EnumJobRunStatus_ENUM_JOB_RUN_STATUS_FAILED:
  69. return JobRunStatus_Failed, nil
  70. case porterv1.EnumJobRunStatus_ENUM_JOB_RUN_STATUS_CANCELED:
  71. return JobRunStatus_Canceled, nil
  72. default:
  73. return "", telemetry.Error(ctx, span, nil, "invalid job status")
  74. }
  75. }