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

Spot on with this write-up, I

Spot on with this write-up, I absolutely think this website needs far more attention. I'll probably be back again to read more, thanks
for the information!

I truly appreciate this post.

I truly appreciate this post. I have been looking all over for this! Thank goodness I found it on Bing. You have made my day! Thanks again!

Very nice article and

Very nice article and straight to the point. I am not sure if this is really the best place to ask but do you folks have any thoughts on where to employ some professional writers? Thanks in advance

My brother recommended I

My brother recommended I might like this web site.
He was entirely right. This put up actually made my day. You can not believe just how much time I had spent for this information! Thanks!

You ave made some decent

You ave made some decent points there. I checked on the internet to find out more about the issue and found most individuals will go along with your views on this site.

Now it's time to choose your

Now it's time to choose your particular investments.

Hi, I believe your web site

Hi, I believe your web site might be having browser compatibility problems.
Whenever I look at your web site in Safari, it looks fine however, if opening
in I.E., it has some overlapping issues. I just wanted to provide you with a quick heads
up! Apart from that, great website!

It's a pity you don't have a

It's a pity you don't have a donate button! I'd without a doubt donate
to this excellent blog! I suppose for now i'll settle for book-marking and adding your RSS
feed to my Google account. I look forward to new updates and will share this website
with my Facebook group. Talk soon!

Right now it looks like

Right now it looks like Movable Type is the preferred
blogging platform available right now. (from what I've read) Is that what you're using on your blog?

I think this is a real great

I think this is a real great blog article.Thanks Again. Cool.

Wow! Thank you! I constantly

Wow! Thank you! I constantly wanted to write on my website something like that. Can I take a portion of your post to my blog?

I was curious if you ever

I was curious if you ever considered changing the layout of your blog?
Its very well written; I love what youve got to say.
But maybe you could a little more in the way of content so people
could connect with it better. Youve got an awful lot
of text for only having 1 or 2 images. Maybe you could space it out
better?

the original materials to

the original materials to represent skin etc. As for the technology of air cushion; it is useful

I'll right away snatch your

I'll right away snatch your rss as I can't in finding your email subscription link or e-newsletter service.
Do you've any? Please permit me know so that I may subscribe.

Thanks.

ugg australia

ugg australia аАа’аАТ‚аЂТ˜classic tall bomber a boot women

No matter if some one

No matter if some one searches for his vital thing, therefore he/she wishes to be available that in detail, so that
thing is maintained over here.

That is true for investment

That is true for investment advice as effectively.

Hello to every one, the

Hello to every one, the contents existing at this web site are truly amazing for people experience,
well, keep up the good work fellows.

Hey! I could have sworn I've

Hey! I could have sworn I've been to this blog before but after reading through some of the post I realized it's new to me.

Anyhow, I'm definitely glad I found it and
I'll be book-marking and checking back frequently!

Hi, constantly i used to

Hi, constantly i used to check webpage posts here early in the break
of day, because i like to gain knowledge of more and more.

This is very fascinating, You

This is very fascinating, You are a very skilled blogger.

I've joined your feed and stay up for looking for extra of your excellent post.
Additionally, I have shared your website in my social networks

Excellent post. I was

Excellent post. I was checking continuously this
blog and I am impressed! Extremely helpful info particularly the
last part :) I care for such information much.
I was looking for this particular information for a long time.

Thank you and good luck.

All types of investments come

All types of investments come with sure dangers.

you might have a terrific

you might have a terrific blog here! would you wish to make some invite posts on my blog?

I am extremely impressed

I am extremely impressed together with your writing talents and
also with the structure for your weblog. Is this a paid subject or did you modify it yourself?
Anyway keep up the excellent high quality writing, it's uncommon to
look a great blog like this one nowadays..

Hey! Do you know if they make

Hey! Do you know if they make any plugins to assist with SEO?
I'm trying to get my blog to rank for some targeted keywords but I'm not seeing
very good results. If you know of any please share. Kudos!

Spot taking place with this

Spot taking place with this write-up, I rightly ponder this website wants much further issue. I all in every probability be yet again to read a long way additional, merit for that info.

Hi, yup this post is really

Hi, yup this post is really fastidious and I have learned lot of things from it about
blogging. thanks.

My brother recommended I

My brother recommended I might like this web site. He was entirely right.
This post truly made my day. You cann't imagine just how much time
I had spent for this information! Thanks!

stumbleupon everyday. It as

stumbleupon everyday. It as always useful

I wanted to thank you for

I wanted to thank you for this great read!! I definitely loved

Sweet blog! I found it while

Sweet blog! I found it while surfing around on Yahoo News.

I've been exploring for a

I've been exploring for a little bit for any high quality
articles or blog posts on this kind of space .
Exploring in Yahoo I eventually stumbled upon this site.
Studying this information So i'm satisfied to exhibit that I've
a very good uncanny feeling I found out exactly what I needed.
I such a lot for sure will make certain to don?t fail to remember this website and provides it a look on a continuing basis.

Take into account investments

Take into account investments that offer speedy annuities.

Have you ever considered

Have you ever considered about including a little
bit more than just your articles? I mean, what you say is important and everything.
However imagine if you added some great pictures or video clips to give
your posts more, "pop"! Your content is excellent but with images and videos, this website could definitely be one of the best
in its field. Awesome blog!

Magnificent goods from you,

Magnificent goods from you, man. I've take into
account your stuff previous to and you're simply extremely fantastic.
I actually like what you've received right here, really like
what you are saying and the best way through which
you are saying it. You're making it entertaining and you continue to care
for to keep it smart. I can't wait to learn much more from you.

This is actually a great site.

Spot on with this write-up, I

Spot on with this write-up, I really feel this web site
needs a great deal more attention. I'll probably be returning to see more, thanks for the information!

Actually when someone doesn't

Actually when someone doesn't understand after that its
up to other users that they will assist, so here it happens.

There is certainly a lot to

There is certainly a lot to find out about this subject. I like all of the points you ave made.

I am from Slovenia. I can

I am from Slovenia. I can help with build this forum. Thanks for approved.

Jaz sem Slovenka. Lahko pomagam pri razvoju foruma.

This is really interesting,

This is really interesting, You are a very skilled blogger. I have joined your rss feed and look forward to seeking more of your wonderful post. Also, I ave shared your site in my social networks!

Major thankies for the

Major thankies for the article.Thanks Again. Fantastic.

This actually answered my

This actually answered my predicament, thank you! jordans free shipping

It as difficult to find

It as difficult to find well-informed people on this topic, however, you seem like you know what you are talking about! Thanks

All kinds of investments

All kinds of investments include certain risks.

Hi there! I understand this

Hi there! I understand this is sort of off-topic however I needed to ask.
Does operating a well-established website such as yours take a massive amount work?
I'm brand new to operating a blog but I do write in my journal daily.
I'd like to start a blog so I can share my own experience
and thoughts online. Please let me know if you have any
kind of ideas or tips for new aspiring blog owners.
Appreciate it!

What as up, I read your blog

What as up, I read your blog regularly. Your writing style is awesome, keep up the good work!

Very soon this website will

Very soon this website will be famous amid all blog viewers, due to
it's nice posts

Wow, great post.Really

Wow, great post.Really looking forward to read more. Really Great.

Attractive section of

Attractive section of content. I just stumbled upon your weblog and in accession capital to assert
that I acquire actually enjoyed account your blog posts.
Anyway I will be subscribing to your augment and even I achievement you access consistently quickly.

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.