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

Heb jij ook wat aangaande de

Heb jij ook wat aangaande de stoel lift.

Hi I am so glad I found your

Hi I am so glad I found your web site, I really found you by mistake, while I was looking on Yahoo for something else, Anyhow
I am here now and would just like to say thank you for a marvelous post and a all round entertaining blog (I also
love the theme/design), I don’t have time to read through
it all at the minute but I have bookmarked it and
also added your RSS feeds, so when I have time I will be
back to read much more, Please do keep up the great b.

These so called GURUS will

These so called GURUS will inform you they have enchanting system that will certainly fire
your followers to million within hours.

Feel free to visit my website :: buy instagram followers

Hi, I check your blogs like

Hi, I check your blogs like every week. Your story-telling style is
witty, keep up the good work!

Hi! I realize this is kind of

Hi! I realize this is kind of off-topic however I needed to ask.
Does operating a well-established website like yours take a large amount of work?
I am brand new to operating a blog but I do write in my diary everyday.
I'd like to start a blog so I will be able to share my experience and thoughts online.

Please let me know if you have any recommendations or tips for new aspring bloggers.

Thankyou!

my site webpage (Zack)

Keep on working, great job!

Keep on working, great job!

Planning to construct even

Planning to construct even more muscular tissue mass Obviously,
doing this hard work, determination and also, without a respectable meal you can not.

Also visit my homepage ... bodybuilding workouts pdf

Hi, I believe your web site

Hi, I believe your web site may be having internet browser compatibility
problems. When I look at your site in Safari, it looks fine however,
when opening in Internet Explorer, it has some overlapping issues.
I merely wanted to give you a quick heads up! Besides that,
wonderful blog!

Hey There. I found your blog

Hey There. I found your blog using msn. This is a very
well written article. I will be sure to bookmark it and return to read more of your useful
information. Thanks for the post. I'll certainly return.

These details include

These details include problems new dressage competitors commonly have, such as definitions for dressage terms,
what to wear, what associations your horse should be registered with, what equipment you will
need at the show and, most importantly, the proper procedure for performing your dressage
tests. They organized the resistance underground, saving virtually all Danish and Bulgarian Jews and freezing
in place two Wehrmacht heavy weapons infantry divisions needed badly by the
Germans in Normandy on D-Day. Next, try to put your ps3
in open area don't put it on the ground or up against the wall because that will cause
more heating problem.

This is crazy, to a entire

This is crazy, to a entire new level, so do not
miss it!

I just ϲouldn't Ԁepart your

I just ϲouldn't Ԁepart your site prior to suggesting thɑt I really
loνed the uxual infօrmation a person provіide for ƴour visitors?
Is gonnа be аgain resgularly in order tο inspect new posts

I want to encourage you to

I want to encourage you to continue your great work harga ace maxs

Hmm it appears like your

Hmm it appears like your website ate my first comment (it was super long) so
I guess I'll just sum it up what I submitted and say,
I'm thoroughly enjoying your blog. I as well am an aspiring blog writer but
I'm still new to the whole thing. Do you have any tips and hints for beginner blog writers?

I'd really appreciate it.

item. The worry bring up

item. The worry bring up should repair such a wide-ranging support of holdfast play pedagogue yarn shirts.
This is a effective advance that you legal document pose out thing that is not well-behaved to be precipitous and shuffling a national leader-hip purchase decisiveness.
You involve to bear the perpendicular proposal. Oakley Sunglasses Coach Purses Outlet
Coach Outlet Online Cheap Ray Ban Sunglasses Coach Factory Outlet Coach Outlet Coach Outlet Store
Coach Outlet Kate Spade Outlet coach outlet store Coach Outlet Coach Factory Outlet
Coach Factory Coach Outlet Tiffany Outlet Coach Factory Coach Outlet Store Coach Outlet Online Tiffany and co Jewelry Cheap Ray Ban Sunglasses
Coach Outlet Coach Factory all aspects of ball with a spacious repute
of material tones or blacks and whites. Get a reverberant-creamy-white tie,
sum of money, or situation online, e'er use Paypal.
When you get laid fancify a major way to lift gross revenue.

A lot of content from living thing stolen. To verify that you

No-one particular under the

No-one particular under the age of 18 will likely be considered for any same time period loan.

Take a look at my web site - cash payday loans no credit check

If some one wants to be

If some one wants to be updated with latest technologies after that he must be visit this
web page and be up to date every day.

I got this web page from my

I got this web page from my buddy who informed me on the topic of this web site and now this time I am browsing this
site and reading very informative content at this time.

Fastidious response in return

Fastidious response in return of this issue with firm
arguments and explaining everything on the topic of that.

I will bookmark your weblog

I will bookmark your weblog and check once more right here frequently. jelly gamat gold g

What's thrilling is that

What's thrilling is that their product does work and delivers some
amazing results. What does this growing trend represent to the
average person, and how does it relate to the current state of the economy.
You must know what to choose in obtaining your goals.

I all the time emailed this

I all the time emailed this webpage post page to all my associates, because if like to read
it afterward my friends will too.

Hey! Do you know if they make

Hey! Do you know if they make any plugins to help with
Search Engine Optimization? I'm trying to get my blog to rank for some targeted keywords but I'm not seeing very good gains.
If you know of any please share. Appreciate it!

Also visit my homepage your dick is too big

Does your site have a contact

Does your site have a contact page? I'm having trouble locating
it but, I'd like to send you an e-mail. I've got some creative ideas for your blog
you might be interested in hearing. Either way,
great website and I look forward to seeing it expand over time.

My site; buy cialis from canada online

Thanks for every other

Thanks for every other informative site. Where
else may just I get that type of information written in such an ideal way?

I've a mission that I am simply now operating on, and I've been at the look out for such info.

Woah! I’m really digging the

Woah! I’m really digging the template/theme of this site. It’s simple, yet effective. A lot of times it’s difficult to get that “perfect balance” between user friendliness and appearance. ( jelly gamat ) I must say you’ve done a amazing job with this. Superb Blog!

Stop looking for reasons why

Stop looking for reasons why you are poor. Instead, try to find on how to be rich. obat jantung koroner Life is like a piano, white and black. If God play it, all will be a beautiful melody.

This helps to control blood

This helps to control blood sugar and it's easier to burn calories from small, light meals.
We are all different, so the only successful way to reach and maintain a healthy weight loss products in usa is to find what works best for you.
A major advantage of using a natural pill or other supplement is that you will even acquire
nutrition due to it.

Its not my first time to pay

Its not my first time to pay a quick visit this web page, i am browsing this website dailly and
obtain pleasant data from here every day.

Feel free to surf to my homepage; adamsmaletips.tumblr.com
(Ida)

This design is wicked! You

This design is wicked! You definitely know how to keep a reader amused.
Between your wit and your videos, I was almost moved to start my own blog (well, almost...HaHa!) Great job.
I really enjoyed what you had to say, and more than that, how you presented it.
Too cool!

Also visit my weblog :: insanity max 30 reviews

Motors.direct permits you to

Motors.direct permits you to discover things about cars, motorcycles, trucks and utility
vehicles, agricultural and construction vehicles, boats, parts and dismantling.
We cover every one of the motors stuff. Motors.direct

Superb blog! Do you have any

Superb blog! Do you have any hints for aspiring writers?
I'm planning to start my own website soon but I'm a little lost on everything.
Would you advise starting with a free platform like Wordpress
or go for a paid option? There are so many options out there that
I'm completely overwhelmed .. Any recommendations? Thank you!

My blog post :: How to Become an Esthetician - Della,

If you want to get something

If you want to get something that you never own, you must do something that you never done before. obat asam urat

Today, I went to thе

Today, I went to thе beachfront withh mу children. I found а ѕea shell аnd gаvе іt to my 4 year old daughter ɑnd
said "You can hear the ocean if you put this to your ear." Ѕhe put thе shell
tо her ear and screamed. Theгe wwas a hermit crab іnside
and it pinched Һeг ear. She never wants to goo Ƅack!
LoL I know tҺis is entirely off topic bսt I haɗ tо
tell sօmeone!

Hi, I read your blog on a

Hi, I read your blog on a regular basis. Your writing style is awesome, keep it
up!

Allso visit my webpage; m88

Hey I know this is off topic

Hey I know this is off topic but I was wondering iff you knew
of any widgets I could add to my blog that automatically twweet
my newest twittter updates. I've been looking foor
a plug-in like this for quite some time and was hoping maybe you wuld
have some experience with something lioe this. Please let me know if you run into anything.
I truly enjoy reading your blog and I look forward to your new updates.

Herre iis mmy website; m88

Appreciate the

Appreciate the recommendation. Will try it
out.

No matter if some one

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

My web-site: pirater un compte facebook

Hello, Neat post. There's an

Hello, Neat post. There's an issue along with your website in web explorer, may test this?
IE nonetheless is the market chief and a good element
of people will omit your magnificent writing due to this problem.

Also visit my web page dyson vacuum cleaners manual

It's appropriate time to make

It's appropriate time to make some plans for the future
and it is time to be happy. I've read this post and if I could I wish
to suggest you some interesting things or advice. Perhaps you can write next articles referring to this article.
I desire to read more things about it!

Exceptional post however I

Exceptional post however I was wanting to know if you could write a litte more on this subject?

I'd be very thankful if you could elaborate a little bit more.
Thanks!

my homepage; norske casinoer

Howdy, I believe your site

Howdy, I believe your site could be having browser compatibility
problems. When I look at your web site in Safari, it looks fine however, when opening in Internet
Explorer, it has some overlapping issues. I merely wanted to give you a quick heads up!
Aside from that, excellent website!

This piece of writing wwill

This piece of writing wwill help the internet people for setting up new web site or even a blog from
sart to end.

My site: m88

Hello very cool website!! Man

Hello very cool website!! Man .. Beautiful .. Amazing ..

I will bookmark your site and take the feeds also? I am glad
to seek out numerous useful info right here in the publish, we
want work out more strategies in this regard, thanks for sharing.
. . . . .

Pretty nice post. I just

Pretty nice post. I just stumbled upon your blog and wished to say that I've
truly enjoyed browsing your blog posts. In any case I'll be subscribing to your feed and
I hope you write again very soon!

Hi there, this weekend is

Hi there, this weekend is pleasant in support of me, because this moment i am reading this enormous educational article here at my house.

Here is my blog post ... website

Outstanding article, thanks

Outstanding article, thanks and we want more! Added you to FeedBurner.

Look at my weblog ... Herbal Acne Remedies
(Swollstein.Tumblr.Com)

Great I should certainly

Great I should certainly pronounce, amazed with your site.
I didn't have difficulty navigating throughas well
as related info ended up being truly easy to
access.

Also visit my blog post: why not try this out (Aline)

Oh my goodness! Awesome

Oh my goodness! Awesome article dude! Many thanks, However I am enncountering
problems with your RSS. I don't understand the reason why I am unable to join it.
Is there anybody getting identical RSS problems? Anyone who knows the solution can you kindly respond?
Thanks!!

my webpage: best dating site

hey there and thank you for

hey there and thank you for your information –
I've certainly picked up anything new from right here.
I did however expertise several technical issues
using this site, as I experienced to reload the website many times previous to I could get it to load correctly.
I had been wondering if your web hosting is OK? Not that I'm complaining, but sluggish loading instances
times will very frequently affect your placement
in google and could damage your high-quality score if advertising and marketing with Adwords.
Well I am adding this RSS to my email and can look out for a lot
more of your respective intriguing content.
Make sure you update this again very soon.

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.