github.com/cozy/cozy-stack@v0.0.0-20240327093429-939e4a21320e/tests/system/lib/job.rb (about)

     1  class Job
     2    include Model
     3  
     4    attr_reader :attributes
     5  
     6    def self.doctype
     7      "io.cozy.jobs"
     8    end
     9  
    10    def self.create(inst, worker, args)
    11      opts = {
    12        accept: 'application/vnd.api+json',
    13        content_type: 'application/vnd.api+json',
    14        authorization: "Bearer #{inst.token_for doctype}"
    15      }
    16      data = JSON.generate({ data: { attributes: { arguments: args } } })
    17      inst.client["/jobs/queue/#{worker}"].post data, opts
    18    end
    19  
    20    def initialize(opts = {})
    21      @couch_id = opts["id"]
    22      @attributes = opts["attributes"]
    23    end
    24  
    25    def done?(inst)
    26      status(inst) == "done"
    27    end
    28  
    29    def status(inst)
    30      opts = {
    31        accept: 'application/vnd.api+json',
    32        authorization: "Bearer #{inst.token_for doctype}"
    33      }
    34      res = inst.client["/jobs/#{@couch_id}"].get opts
    35      j = JSON.parse(res.body)
    36      j.dig("data", "attributes", "state")
    37    end
    38  end