2014-02-05 5 views
0

파일을 출력하는 delayed_job 작업이 있습니다. 나중에 쉽게 참조 할 수 있도록 파일을 만든 작업의 ​​ID로 접두사를 붙일 수 있어야합니다.작업 작업 내에서 delayed_job 작업 ID를 참조하십시오.

# Create a file that has the job id in the name... 
job_id = thing.delay.create_file 
# Now we'll use the job_id to search for the file... 

이게 가능합니까?

+0

create_file 태스크에 코드를 포함하십시오. – eabraham

+0

@ eabraham- 왜? – Yarin

+0

DJ가 작업을 완료하면 DJ가 항목을 삭제하므로 작업 ID는 어떤 용도로 사용됩니까? –

답변

1

의 여기에서 보자 ...

class WriteFileJob 
    attr_accessor :dj_id, :file_name 

    def initialize(file_name) 
    @file_name = file_name 
    end 

    def perform 
    # do something with @dj_id; don't worry, we'll set it below 
    end 
end 

당신이 작업을 대기열에 때, 당신은 일을 다시 얻어야한다 : 그 표는 당신이하는 쿼리 수있는 기본 id 컬럼을해야합니다 :

j = Delayed::Job.enqueue(WriteFileJob.new("foo.txt")) 

다음, 당신은 당신이 큐에 객체를로드하고 ID로 업데이트 그냥 돌아왔다 :

object_to_update = YAML.load(j.handler) 
object_to_update.dj_id = j.id 
# update the handler 
j.handler = object_to_update.to_yaml 
j.save 
+0

@ Abdo- 똑똑! 감사! – Yarin

+0

@Yarin :-) @dj_id가 할당되기 전에 너무 일찍 실행되는 작업에 대해 걱정할 경우 @dj_id.nil? 예외를 throw하여 나중에 작업을 다시 실행할 수 있습니다 (또는 처음에 run_at을 지정할 수 있음) – Abdo

1

delayed_job은 작업을 대기열로 보내기 위해 테이블을 사용하며 해당 테이블은 Gem's migration file에서 찾을 수 있습니다. 이제

ActiveRecord::Base.connection.raw_connection.prepare("Select id FROM delayed_job where handler=?","YAML Encoded string representing the object doing work") 
+0

감사합니다. DB에 다시 쿼리하는 작업이 필요없는 작업을 원하지만 고맙겠습니다. – Yarin

관련 문제