laravel - To prevent timeout - what are the advantages of queueing a job vs chunking results? -
my laravel 5 site allows users send emails contacts. problem operation times out after loops through around 50 contacts.
an overview of process:
- get list of contacts
- construct email
- loop through list of contacts , send them each email using mailer service.
- return email details controller saved in db.
chunking pretty easy understand, i'm not real comfortable mechanics of placing job on queue.
to prevent timeout, better off chunking contacts limit size of loop or putting entire job on queue? if entire job should placed on queue - can still chunk contacts? can queued job still use mail::queue or redundant?
loop code:
public function sendindividualemail($members, $subject, $body, $file) { $view = 'emails.membermessage'; //construct email details $mdata = $this->emailconstructor($subject, $body); //check see if there attachment, if upload it. if (!is_null($file)){ $uploadedfile = $this->attachment->saveattachment($file); $mdata['path'] = 'https://'.$_server["http_host"].'/uploads/attachments/'.$uploadedfile['filename']; $mdata['display'] = $uploadedfile['display']; } foreach($members $member){ if (!empty($member->contact_email)){ //add member details here $mdata['email'] = $member->contact_email; $mdata['name'] = $member->contact_firstname; //email member $this->emailto($view, $mdata); $mdata['emailcount']++; } } return $mdata; } mailer service:
public function emailto($view, $mdata) { mail::queue($view, $mdata, function($message) use ($mdata) { $message->from($mdata['from'], $mdata['fromname']); $message->to($mdata['email'], $mdata['name'])->subject($mdata['subject']); $message->getheaders()->addtextheader('x-mc-metadata', $mdata['meta']); if(array_key_exists('path', $mdata)){ $message->attach($mdata['path'], array('as' => $mdata['display'])); } }); }
a few suggestions first. chunking them use less computational power, not offer flexibility queue system presents.
chunking scenario --- models, count(x) new chunk store chunk. foreach chunk items foreach item in items item->dosomething
-- pagination might adapt pagination library support instead. i.e collection , paginate it, store page number, , page worth of records @ time?
-- event observer queue? technique use event listener system. can number of ways, think when email should sent.
your model have either column sent_mymail_email, or use mutator getsentmymailemailattribute() return boolean value of true if email sent (job complete).
you set new event or latch on existing event.
see: how can see laravel events being fired
queue system -- queue system receive events , data 1 server via http request . items added big list of jobs need completed. when event comes (it's turn), send http request somewhere on system. system interprets task , accomplishes task, when complete, response sent queue system(typically) notifying system of completed task. queue move on next task, send request ... , on.
comparing chunk method, can see application needs send email in both scenarios, not need send task, receive task or interpret task in chunking method.
you have less moving parts in chunking method, if wanted ability scale , put part of applications workload onto secondary system or cluster handle jobs, want go queue system.
the queue system depends on 3rd party services have own built in flaws. example cant define order these items.
this blog post explains: http://cloudstacking.com/posts/amazon-sqs-gotchas.html
here decent post using queue systems: http://www.isixsigma.com/industries/retail/queuing-theory-and-practice-source-competitive-advantage/
the advantage in nutshell being have ability scale large amount of these requests being processed 0 impact on front-end(application).
you can run these jobs on separate system or cluster of systems push work needed server not impact user experience.
Comments
Post a Comment