job_runs.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. )
  35. // JobRunFromProto converts a job run proto to a JobRun
  36. func JobRunFromProto(ctx context.Context, jobRun *porterv1.JobRun) (JobRun, error) {
  37. ctx, span := telemetry.NewSpan(ctx, "porter-app-job-runs-from-proto")
  38. defer span.End()
  39. var run JobRun
  40. if jobRun == nil {
  41. return run, telemetry.Error(ctx, span, nil, "job run is nil")
  42. }
  43. status, err := jobStatusFromProto(ctx, jobRun.Status)
  44. if err != nil {
  45. return run, telemetry.Error(ctx, span, err, "job status from proto")
  46. }
  47. run = JobRun{
  48. ID: jobRun.Id,
  49. Name: jobRun.JobName,
  50. ServiceName: jobRun.ServiceName,
  51. Status: status,
  52. CreatedAt: jobRun.CreatedAt.AsTime(),
  53. FinishedAt: jobRun.FinishedAt.AsTime(),
  54. AppRevisionID: jobRun.AppRevisionId,
  55. }
  56. return run, nil
  57. }
  58. func jobStatusFromProto(ctx context.Context, status porterv1.EnumJobRunStatus) (JobRunStatus, error) {
  59. ctx, span := telemetry.NewSpan(ctx, "porter-app-job-status-from-proto")
  60. defer span.End()
  61. switch status {
  62. case porterv1.EnumJobRunStatus_ENUM_JOB_RUN_STATUS_RUNNING:
  63. return JobRunStatus_Running, nil
  64. case porterv1.EnumJobRunStatus_ENUM_JOB_RUN_STATUS_SUCCESSFUL:
  65. return JobRunStatus_Successful, nil
  66. case porterv1.EnumJobRunStatus_ENUM_JOB_RUN_STATUS_FAILED:
  67. return JobRunStatus_Failed, nil
  68. default:
  69. return "", telemetry.Error(ctx, span, nil, "invalid job status")
  70. }
  71. }