Replies: 5 comments 7 replies
-
Usually a layer should work well. |
Beta Was this translation helpful? Give feedback.
-
I can get the storage context if I stuff it into a layer but how would I get the JobRequest from inside the build_fn function? Looking at the update_by_id code in each db implementation, I don't see it updating the "job" column. Ideally the results would be a separate column. |
Beta Was this translation helpful? Give feedback.
-
Here is how to do it with pub struct SaveResultLayer;
impl<S> Layer<S> for SaveResultLayer {
type Service = SaveResultService<S>;
fn layer(&self, service: S) -> Self::Service {
SaveResultService {
service,
}
}
}
pub struct SaveResultService<S> {
service: S,
}
impl<S, J> Service<JobRequest<J>> for SaveResultService<S>
where
S: Service<JobRequest<J>>,
Request: std::fmt::Debug,
J: Job
{
type Response = S::Response;
type Error = S::Error;
type Future = S::Future; // Possibly box dyn here
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.service.poll_ready(cx)
}
fn call(&mut self, request: JobRequest<J>) -> Self::Future {
let id = request.id();
let storage: PostgresStorage<J> = request.data();
let fut = self.service.call(request);
Box::pin(async move {
let res = fut.await;
let job = storage.get_by_id(&id).await;
job.set_result(&res.to_string());
storage.update_by_id(&id, job).await;
res
})
}
} I have not tested it but, it should give you the idea. |
Beta Was this translation helpful? Give feedback.
-
I got it to work with "new_with_data", however, it's not very ergonomic way of doing it. Also, I'm not able to update the job payload as the update methods don't update the "job" column, only status, attempts etc: "UPDATE jobs SET status = ?, attempts = ?, done_at = ?, lock_by = ?, lock_at = ?, last_error = ? WHERE id = ?" It is really critical to be able to update the job to store results. |
Beta Was this translation helpful? Give feedback.
-
In sqlite this is how it works: apalis/packages/apalis-sql/src/sqlite.rs Line 481 in 95618ff The only thing I need is to update the name of the column name from |
Beta Was this translation helpful? Give feedback.
-
How would I go about storing the result of a job in the DB along with the request?
Thanks.
Beta Was this translation helpful? Give feedback.
All reactions