Napi api: Batch API

The spam filter installed on this site is currently unavailable. Per site policy, we are unable to accept new submissions until that problem is resolved. Please try resubmitting the form in a couple of minutes.
Category: 
Leírás
Napi api: Batch API

Well, talán harmadjára futok össze a batch apival, és még mindig nem volt zökkenőmentes a barátság, szóval amíg az a betyár sok elem leszalad, amin dolgoztam, levésem gyorsan a tudnivalókat, hogy legközelebb már ne kelljen megint mindenre rájönni.

No akkor batch api:

A batch kötegelt feldolgozást jelent. Drupalon belül úgy is mondhatjuk, hogyha egyszerre annyi adattal kell dolgozni, ami túlnő egy oldalletöltés korlátjain, akkor kell a batch apihoz nyúlni. A képet megnézve valószínű egyébként, hogy egyből leesik, a Drupal melyik részéről is van szó.

Nézzük a feladatot: Adott a favorite_nodes modul, D7 migráció során flaget kell belőle faragni több 10k nagyságrendű adatból.

A batch api maga két fő részből áll. Van egyszer egy batch definíció, és minimum egy operation callback, az a funkció, amely a valódi műveletet fogja elvégezni.

Így néz ki nagyjából egy batch definíció:

[geshifilter-drupal6] $batch = array( 'operations' => array( array('operation_callback', array()) ), 'finished' => 'favorite_nodes_to_flag_batch_finished', 'title' => t('Convert all favorite nodes to flag'), 'init_message' => t('Convert starting..'), 'progress_message' => t('Processed @current out of @total.'), 'error_message' => t('Hmm.. Something could be wrong, sorry, post an issue to module..'), );[/geshifilter-drupal6]

Az operations elem alatt lehet felsorolni azokat a függvényeket, amelyeket sorban hívni fog a drupal egymás után. Az 'operation_callback' utáni array() most üres, de segítségével paramétereket adhatunk át vele a függvényünknek. Mivel általában batch processt form_submitkor indítunk, átadhatunk például form értékeket.

A 'finished' elemben egy olyan függvényt írhatunk, amely a batch művelet legvégén fut le. Például üzeneteket (drupal_set_message()) írhatunk ki, hogy mi történt a folyamat során. Figyi, ez nem array, ebből csak egy van!

Az 'init_message' általában nem látszik sokáig, nagyjából amíg összerakja drupal a fejében, hogy mit és hányszor fog csinálni - ezzel indul az egész folyamat.

Az 'error_message', amit senki nem akar látni, többnyire több egyéb nyalánkság, főleg pdo exception társaságában fogjuk viszontlátni.

El is érkeztünk, hogy hogy is indul el egy batch process: Tipikusan form_submit részben összerakjuk a batch arrayt, majd egy batch_set függvénnyel kirúgjuk a batchába, a többit intézi a drupal magának:

[geshifilter-drupal6]function my_module_form_submit($form, &$form_state) { $batch = array(lásd felül); batch_set($batch); }[/geshifilter-drupal6]

Ezzel elkezdődik az operation része a batchnek. Ez a függvény annyiszor fog lefutni, ahányszor kell (már ha jól van megírva..)
Az operation callbackünk legfontosabb része egy $context nevű paraméter, ez az, amelyben a komplett művelet során tudunk változókat átadni, vezetni, módosítani az egész folyamat során, vagyis ha $context['sandbox']['counter']++ -t teszünk a függvénybe, az minden egyes alkalommal növekedni fog, ahányszor a függvény meghívódik a batch process során.

Ennek a $context paraméternek a következő részeit állítgathatjuk:

$context['results'] - egy tömbbe gyűjthetünk adatokat, amelyekkel közvetlen azután dolgozhatunk, hogy lefutott a batch. Ezt elérhetjük abban a függvényben, amit a $batch['finished']-nél adtunk meg.

$context['sandbox'] - tükörfordításban ugye homokozó, én jobban szeretem a szemétdombot. Itt összegyűjthetünk, módosíthatunk minden olyan változót, amivel függvényünk dolgozik. Például ahogy fentebb is volt már, hogy hányszor futott a process.

$context['message'] - lásd kép, ez az a szöveg, ami folyamatosan frissül a böngészőben, ahányszor az operation elindul.

$context['finished'] - Hogy hány %-nál jár a művelet. Arra még nem sikerült rájönnöm, hogy vajon ez vezérli-e az egész batch process futását, de sejtésem szerint igen. Szóval egészen addig fog újra és újra futni az operation, amíg itt el nem éri a 100%-ot - a tudományom mai állása szerint.

Nézzünk egy ilyen függvényt, és magyarázzunk:

[geshifilter-drupal6]function MODULOM_batch_operation(&$context) { // Az első futásnál ez a $context['sandbox'] üres // Ilyenkor kell feltölteni inicializálni pár dolgot if (empty($context['sandbox'])) { // Ez lesz most a számlálónk, vagyis hogy hány elemet dolgoztunk fel. $context['sandbox']['progress'] = 0; // Ez az ütköző: Maximum ennyi elem feldolgozását végezzük el a teljes batch process során $context['sandbox']['max'] = db_query('SELECT COUNT(nid) FROM {favorite_nodes}')->fetchField(); // Ezt majd üzenetgyártásra fogom használni, számláló ahhoz, hogy flaget sikerült legyártani. $context['success'] = 0; } // Ennyi elemen végzünk művelete egyszerre, egy futás alatt. $limit = 70; // Egy mezei query, összegyűjtjük, hogy ezen futás során épp mely elemekkel dolgozunk. // Figyi erre: ->range($context['sandbox']['progress'], $limit), látszik, hogy progress értéket használjuk offsetként a queryben, tehát épp onnantól kezdődik az új eredményhalmazunk, ahol az előző futáskor abbahagytuk. $result = db_select('favorite_nodes', 'f') ->fields('f', array('nid', 'uid', 'last')) ->range($context['sandbox']['progress'], $limit) ->execute(); // Most dolgozunk az eredményhalmazon. foreach ($result as $row) { // Először növeljük a progresst. $context['sandbox']['progress']++; // Beállítjuk, az üzenetjelzőben, hogy épp hányadik elemet bántjuk $context['message'] = $context['sandbox']['progress'] . ' item processed.'; // Ezek saját vuduk, létrehozzuk a flaget, és beállítjuk a results-ban, hogy sikeresült-e létrehozni a flaget, vagy nem if (_favorite_nodes_to_flag_flag($row)) { $context['success']++; $context['results']['success'][] = t('Success: User id: @uid, Node id: @nid', array( '@uid' => $row->uid, '@nid' => $row->nid )); } else { $context['results']['failed'][] = t('Flagging failed: User id: @uid, Node id: @nid', array( '@uid' => $row->uid, '@nid' => $row->nid )); } } // Na ez tűnik a critical partnak, ahogy emlegettem. Ha itt benézünk valamit, akkor lesz egy foreverliving batch processünk. Szóval figyeljünk, hogy hogyan állítjuk be az ütközőt. if ($context['sandbox']['progress'] != $context['sandbox']['max']) { $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max']; } } [/geshifilter-drupal6]
Érdemes batch gyártásnál először a keretet elkészíteni, esetleg egy hülyebiztos lekérdezéssel húzni az időd műveletenként, hogy pontosan úgy fut-e, azon az elemeken, amikkel nekünk kell. Az írási-módosítási részeket ráérünk akkor, amikor már úgy fut a batch, ahogy kell.

Ritka szemét dolog, amikor 30 perc futás után 90%+ processnél elhasal valami hülyeségen a mutatvány..

Comments

Folks with operate from

Folks with operate from property jobs can stay awzy from the pitfalls earn money by doing surveys setting a
schedule for themselves.

Yianne is a tool psychic

Yianne is a tool psychic fortuneteller she brings idea right into your psychological as well as economic troubles.

my webpage - uniselrealty.com

Actually when someone doesn't

Actually when someone doesn't know then its up
to other viewers that they will assist, so here it occurs.

To get Unlimited Clash of

To get Unlimited Clash of Clans Gems, Elixir or Cash you can use our easy software.

You can now say yes to

You can now say yes to limitless token, money and fans once you get the hack software.

Also visit my web-site Top eleven Hack

Although being aware of

Although being aware of keyword percentages is a good idea, it
is more important that content be relevant and useful
to the visitor. The trends in this industry change and new developments take place only when Google comes up
with some novel feature that ends up breaking every site's SEO
and compels them to consider a change in their current content presentation in order to do well.
Forgetting to write for an audience is one of the biggest mistakes that bloggers make.

Eventbrite is an online party-planning tool with KISS
(Keep It Simple Stupid) design so it is usually a top
rated choice among event planners (organizers).

Nice response in return of

Nice response in return of this matter with firm arguments and describing
the whole thing concerning that.

Good day! This is my first

Good day! This is my first visit to your blog!
We are a collection of volunteers and starting a new initiative in a community
in the same niche. Your blog provided us useful information to
work on. You have done a wonderful job!

Here is my blog Fertility diet (Estela)

They'll take a look at

They'll take a look at employment information and
your revenue instead of your credit rating to ascertain your endorsement.

My weblog ... how to buy a car with bad credit history - Lona,

They'll take a look at

They'll take a look at employment information and
your revenue instead of your credit rating to ascertain your endorsement.

My weblog ... how to buy a car with bad credit history - Lona,

Magnificent items from you,

Magnificent items from you, man. I've bear in mind your stuff previous to and you're simply extremely fantastic.
I actually like what you have got right here, certainly like what you are saying and the best way during which you
say it. You make it enjoyable and you continue to care for
to stay it sensible. I cant wait to learn much more from you.
This is really a wonderful site.

Sometimes its a pain in the

Sometimes its a pain in the ass to read what blog
owners wrote yet this site is actually amazing and I actually treasure your good article, Excellent post.

My web page ... grounding mat for bed

'Are people really searching

'Are people really searching online for my product or services'.
When Page - Rank was patented the patent was assigned to Stanford University.
But it is extremely important that anyone looking to hire
an SEO professional be aware of the differences.

You have to take price quotes from different SEO companies locally and internationally.

Wow that was unusual. I just

Wow that was unusual. I just wrote an incredibly long comment
but after I clicked submit my comment didn't appear. Grrrr...
well I'm not writing all that over again. Anyway,
just wanted to say great blog!

The best way you present your

The best way you present your own home to a prospective buyer can determine a number
of issues.

Here is my blog; sell my house fast online quote

Appreciating the time and

Appreciating the time and energy you put into
your site and detailed information you provide.
It's good to come across a blog every once in a while that isn't the
same outdated rehashed material. Great read! I've bookmarked your site and I'm adding your RSS feeds to my Google account.

Very nice post. I just

Very nice post. I just stumbled upon your weblog and wanted to say that I'vetruly enjoyed surfing around your blog posts.
In any case I'll be subscribing to your rss feed and I hope you write again soon!

Hey there, You've done a

Hey there, You've done a fantastic job. I will certainly digg it
and personally recommend to my friends. I am confident they'll be benefited from this site.

I'm not sure why but this

I'm not sure why but this website is loading incredibly slow for me.
Is anyone else having this issue or is it a problem on my end?
I'll check back later and see if the problem still exists.

Hi mates, how is everything,

Hi mates, how is everything, and what you wish for to say
regarding this piece of writing, in my view its actually amazing
in support of me.

my web blog: coque iphone 5

Cooling the plastic is so

Cooling the plastic is so important mainly because
if something goes wrong, the entire process has been in vain. As there are many pipeline liners service providers or manufacturers who still do not
use modern technicalities in their service plant and in turn provide an imperfect liner service to their clients which are neither resilient nor durable as such.
Established nations have the plastic accessories for play kitchen set extruder vastly managed by contemporary electronics and
computer system engineering, and so they use laptop or computer
to regulate the method parameters of extrusion course of action.

Many small companies prefer

Many small companies prefer to hire SEO specialists as consultants rather than full time employees,
unless they have a certain amount of websites that need to be continuously maintained and optimized.
When Page - Rank was patented the patent was assigned to Stanford University.
Forgetting to write for an audience is one of the biggest mistakes that bloggers make.
Tests with a model like Page - Rank have shown that the system is not infallible.

'Are people really searching

'Are people really searching online for my product or services'.
The trends in this industry change and new developments take place only when Google comes up with some
novel feature that ends up breaking every site's SEO and compels them
to consider a change in their current content presentation in order to do well.

Unlike TV, radio and other traditional marketing
channels that need big budgets to be effective, SEO can be cost effective.
There are two techniques that combine to form a complete SEO.

I simply couldn’t depart your

I simply couldn’t depart your site as I very enjoyed the standard information a person provide for your visitors?
Is going to be back frequently so as to inspect new posts

Also visit my web page ... lorex security reviews

Gone are the days of one way

Gone are the days of one way marketing or merely just posting comments about your site or other related topics.
Thirdly, the search engines need legit companies to do site optimization. Unlike TV, radio and other traditional marketing channels
that need big budgets to be effective, SEO can be
cost effective. You have to take price quotes from different SEO companies locally and internationally.

At the finish of the day, you

At the finish of the day, you do not know until you
test.

my web page create a website free

This paragraph will help the

This paragraph will help the internet visitors for building up new web site or even a weblog
from start to end.

We are a group of volunteers

We are a group of volunteers and starting a new
scheme in our community. Your site offered us with valuable info to work on. You've done
an impressive job and our entire community will be grateful to you.

We're a gaggle of volunteers

We're a gaggle of volunteers and starting a new scheme in our community.
Your web site provided us with useful info
to work on. You've done a formidable activity and our entire group might be grateful to you.

Your method of explaining the

Your method of explaining the whole thing in this article is genuinely fastidious, all can effortlessly know it, Thanks a lot.

Hi, all is going perfectly

Hi, all is going perfectly here and ofcourse every one is sharing information,
that's in fact excellent, keep up writing.

Travel in search of the

Travel in search of the Northern Lights when the landscape is covered in pure white snow.

My web blog :: Jonna

bookmarked!!, I like your

bookmarked!!, I like your website!

Unfortunately cold calling

Unfortunately cold calling and prospecting no matter
how good you are and what training you go through is a drain and really requires you to
be a sales rockstar to prosper with which limits how big and fast your team is.
Forums which are centered around panic attacks are also great places to
talk about experiences with other people suffering with anxiety.
I've seen a couple of videos from some Organo Gold events or functions,
and in them I've seen a lot of fired up distributors.

KE

KE

CI

CI

When some one searches for

When some one searches for his required thing, thus he/she wishes to be available that
in detail, thus that thing is maintained over here.

Hey! Someone in my Myspace

Hey! Someone in my Myspace group shared this site with us so I came to check it out.
I'm definitely enjoying the information. I'm book-marking
and will be tweeting this to my followers! Terrific blog and amazing design and style.

Norway is not an EU member,

Norway is not an EU member, but has aligned itself with the bloc's sanctions
against Russia.

My page - Davis

Having read this I believed

Having read this I believed it was really informative.
I appreciate you taking the time and effort to put this
information together. I once again find myself spending a lot
of time both reading and posting comments. But so what, it was still worthwhile!

When I initially commented I

When I initially commented I clicked the "Notify me when new comments are added" checkbox
and now each time a comment is added I get several e-mails with the same comment.
Is there any way you can remove me from that service?
Thank you!

I loved as much as you will

I loved as much as you will receive carried out right
here. The sketch is tasteful, your authored material stylish.
nonetheless, you command get bought an nervousness over that you wish
be delivering the following. unwell unquestionably come more formerly again since exactly the same nearly a lot often inside case you shield this increase.

Cooker "black" list

Cooker "black" list Electric Manufacturing Co.
They are gentle pounds, compact, adaptable and weather
evidence. Basically, convection is heat transfer via fluid flow.

I love it when individuals

I love it when individuals come together and share views. Great website, continue the
good work!

If you are going for most

If you are going for most excellent contents like I do, just go
to see this web page every day because it gives quality contents,
thanks

Wow, amazing blog layout! How

Wow, amazing blog layout! How long have you been blogging for?
you make blogging look easy. The overall look of your web site is wonderful,
let alone the content!

If you are going for most

If you are going for most excellent contents like myself,
only pay a quick visit this web page every day since it gives quality contents, thanks

Here is my blog - website design cost Philippines

I wanna admit that this is

I wanna admit that this is extremely helpful, Thanks for
taking your time to publish this.

my web-site ... locksmith in greenville sc

Hi there, of сourse tҺis

Hi there, of сourse tҺis article is genuіnely pleasɑnt and I have
learned lot of things from it on the toƿic of blogging. thankѕ.

my page :: cara daftar sbobet online

Howdy! I just wish to give an

Howdy! I just wish to give an enormous thumbs up for the great
info you have here on this blog. I'll be returning in your web site for
additional information soon.

Also visit my homepage :: greenville locksmith

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Internal paths in double quotes, written as "internal:node/99", for example, are replaced with the appropriate absolute URL or relative path.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <del> <img>
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Allowed HTML tags: <a> <blockquote> <br> <cite> <code> <dd> <del> <div> <dl> <dt> <em> <li> <ol> <p> <span> <strong> <ul>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <bash>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <mysql>, <php>, <python>, <ruby>, <sql>. The supported tag styles are: <foo>, [foo].
  • Each email address will be obfuscated in a human readable fashion or (if JavaScript is enabled) replaced with a spamproof clickable link.