Add Job Status Updated Subscription

What does this MR do and why?

The purpose of this MR is to implement a subscription for front end views to be able to access real time updates on a job status. This will initially be used for real time updates on the job status icon in the job show page.

  • ciJobStatusUpdated requires a global Ci::Build ID to initiate the subscription.
  • the subscription is triggered when any transition event occurs on the job's status through an after commit hook on status updated
  • the subscription payload is a Ci::JobType

References

Screenshots or screen recordings

Screenshot_2025-05-09_at_10.35.43_AM Screenshot_2025-05-09_at_10.35.32_AM

How to set up and validate locally

Example Subscription query:

subscription jobStatusUpdated($jobId: CiBuildID!) {
  ciJobStatusUpdated(jobId: $jobId) {
    id
    status
  }
}

NOTE: You will need to set a jobId variable or you can pass it directly(see screenshots). The value required is a Ci:Build gid( ie. "gid://gitlab/Ci::Build/299"). You can also get the pipeline's formatted gid in the rails console by calling to_gid on an existing job/build.

  1. Ensure you are logged into your local instance and navigate to the graphql explorer page (eg. http://gdk.test:3000/-/graphql-explorer or relace gdk.test:3000 to whatever your localhost is set to)
  2. In your terminal start a rails console (bundle exec rails c)
  3. In the console enable the feature flag Feature.enable(:ci_job_status_realtime)
  4. In the console find a job/build you would like to use for testing and fetch the global id:
 job = Ci::Build.last 
 job.to_gid.uri.to_str #this is the string you will use for the $jobId variable in the graphql UI
  1. Return to the graphql explorer and copy the query noted above ⬆️ before the steps
  2. Set the jobId variable to the global id string you generated in the console in Step#3 (you can do this either directly in the subscription query or in the section below the query box that allows you to input variables:
{
  "jobId": "gid://gitlab/Ci::Build/???" #replace with the string you generated in the console
}
  1. Press the play ▶️ in the graphql-explorer UI. The initial response should be nil because you are initiating/opening up the subscription. You should see this response:
{
  "data": {
    "ciJobStatusUpdated": null
  }
}
  1. There are 2 ways to trigger the subscription from the rails console to test it out. Using the same job set earlier (job = Ci::Build.last) :

i. Triggering the subscription directly: GraphqlTriggers.ci_job_status_updated(job)

ii. Mocking a job state machine event with any valid action(depending on the current state of your job the actual status may update or not): job.cancel!

  1. With either of the actions performed in Step#7 the subscription response should update with the values you set in the subscription and the data response should look something like:
{
  "data": {
    "ciJobStatusUpdated": {
      "id": 1234
      "status": "CANCELED",
    }
  }
}

Gotchas: If you are getting and unauthorized error make sure the user you are logged in as is authorized to read the build/project, probably best to be logged in as the root user.

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #541146 (closed)

Edited by Vlad Wolanyk

Merge request reports

Loading