Class: Api::JobsController

Inherits:
ApiController show all
Defined in:
app/controllers/api/jobs_controller.rb

Instance Method Summary collapse

Methods inherited from ApiController

#probe

Instance Method Details

#createObject

Creates a job

Creates a job using the specified parameters, which are all required. If the request was valid, the created job is returned. If the request could not be completed, a list of errors will be returned.

If the job is successfully created, two extra headers will be sent in the response. The header X-State-Changes-Location contains the location of the state changes endpoint for this job. The header X-Notifications-Location contains the location of the notifications endpoint for this job.

Parameters

input, output and preset are required parameters, notify is optional.

input

Input file to process

output

Output file to write to

preset

Preset name to use

priority

Priority as integer (default nil, maximum 999)

notify

A list of email addresses and urls separated by commas.

If a job enters the completed or failed state, a notification will be sent to the emails and urls specified in the notify parameter. Urls will receive a POST request with the JSON representation of the job as body.

Response codes

success

201 created

failed

406 Unprocessable Entity

Example

$ curl -d 'input=/tmp/foo.flv&output=/tmp/bar.mp4&preset=h264' http://localhost:3000/api/jobs

{
  "job": {
    "completed_at":null,
    "created_at":"2011-05-10T08:25:00Z",
    "destination_file":"/tmp/bar.mp4",
    "duration":null,
    "filesize":null,
    "host_id":1,
    "id":26,
    "message":null,
    "preset_id":1,
    "priority":null,
    "progress":null,
    "remote_job_id":"fa832776a64b6844fb9f1a244757734a9d83c00f",
    "source_file":"/tmp/foo.flv",
    "state":"scheduled",
    "transcoding_started_at":"2011-05-10T08:25:03Z",
    "updated_at":"2011-05-10T08:25:03Z"
  }
}

104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/controllers/api/jobs_controller.rb', line 104

def create
  job = Job.from_api(params)
  if job.valid?
    response.headers['X-State-Changes-Location'] = api_state_changes_url(job)
    response.headers['X-Notifications-Location'] = api_notifications_url(job)
    respond_with job, location: api_job_url(job) do |format|
      format.html { redirect_to jobs_path }
    end
  else
    respond_with job do |format|
      format.html do
        @job = job
        render '/jobs/new'
      end
    end
  end
end

#destroyObject

Deletes a job

Removes a job from the database, regardless of state

Parameters

id

The id of the job to destroy


194
195
196
197
198
199
200
# File 'app/controllers/api/jobs_controller.rb', line 194

def destroy
  job = Job.find(params[:id])
  job.destroy
  respond_with job do |format|
    format.html { redirect_to jobs_path }
  end
end

#failedObject

Returns a list of failed jobs

Jobs become failed if the Transcoder reports an error. This method uses pagination.


53
54
55
# File 'app/controllers/api/jobs_controller.rb', line 53

def failed
  jobs_index(Job.failed)
end

#indexObject

Returns a list of jobs regardless of state.

This method uses pagination.


22
23
24
# File 'app/controllers/api/jobs_controller.rb', line 22

def index
  jobs_index(Job.all)
end

#on_holdObject

Returns a list of jobs that are on hold

Jobs will be put on hold when a Transcoder instance is unavailable. This method uses pagination.


40
41
42
# File 'app/controllers/api/jobs_controller.rb', line 40

def on_hold
  jobs_index(Job.on_hold)
end

#processingObject

Returns a list of jobs being processed.

Jobs being processed are being transcoded by a Transcoder. This method uses pagination.


34
35
36
# File 'app/controllers/api/jobs_controller.rb', line 34

def processing
  jobs_index(Job.processing)
end

#purgeObject

Purges failed jobs

This method will permanently delete all failed jobs from the database.

$ curl -XDELETE http://localhost:3000/api/jobs/purge
{} # HTTP Status: 200 OK

169
170
171
172
# File 'app/controllers/api/jobs_controller.rb', line 169

def purge
  Job.failed.destroy_all
  render nothing: true
end

#retryObject

Retries a job

This method will force a job into the Scheduled state, regardless of which state it's currently in.

Parameters

id

The id of the job to retry


180
181
182
183
184
185
186
# File 'app/controllers/api/jobs_controller.rb', line 180

def retry
  job = Job.find(params[:id])
  job.enter(Job::Scheduled)
  respond_with job do |format|
    format.html { redirect_to jobs_path }
  end
end

#scheduledObject

Returns a list of scheduled jobs

Scheduled jobs are created, but not yet accepted by the transcoders. This method uses pagination.


28
29
30
# File 'app/controllers/api/jobs_controller.rb', line 28

def scheduled
  jobs_index(Job.scheduled)
end

#showObject

Shows a job

The displayed job will have its status updated to provide an up-to-date view of attributes.

Parameters

id

The id of the job to show

Example

{
  "job": {
    "completed_at":null,
    "created_at":"2011-05-10T08:25:00Z",
    "destination_file":"/tmp/bar.mp4",
    "duration":null,
    "filesize":null,
    "host_id":1,
    "id":26,
    "message":null,
    "preset_id":1,
    "progress":null,
    "remote_job_id":"fa832776a64b6844fb9f1a244757734a9d83c00f",
    "source_file":"/tmp/foo.flv",
    "state":"scheduled",
    "transcoding_started_at":"2011-05-10T08:25:03Z",
    "updated_at":"2011-05-10T08:25:03Z"
  }
}

158
159
160
161
# File 'app/controllers/api/jobs_controller.rb', line 158

def show
  job = Job.find(params[:id])
  respond_with job
end

#successObject Also known as: completed

Returns a list of successfully completed jobs

These jobs have been successfully transcoded. This method uses pagination.


46
47
48
# File 'app/controllers/api/jobs_controller.rb', line 46

def success
  jobs_index(Job.success)
end

#updateObject

Updates a job with attributes from the transcoder

This endpoint is specifically for updating a job from the transcoder and should not be called manually.


125
126
127
128
129
# File 'app/controllers/api/jobs_controller.rb', line 125

def update #:nodoc:
  job = Job.find(params[:id])
  job.enter(params[:status], params, request.headers)
  respond_with job, location: api_job_url(job)
end