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.
Kategória: 
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..

Hozzászólások

Greetings, I do think your

Greetings, I do think your website may be having internet browser compatibility issues.
When I take a look at your web site in Safari, it looks fine but when opening in I.E., it has some overlapping issues.

I just wanted to give you a quick heads up!
Other than that, wonderful blog!

Good post however , I was

Good post however , I was wanting to know if you could
write a litte more on this topic? I'd be very grateful if you could elaborate a little bit more.
Kudos!

If no one is around to make

If no one is around to make note of who is standing in that light,
it just gives a thief better light for breaking in. He explains how to plan for settling as well as
details on proper chainsaw stance for cutting a lateral groove in the wood.
Once you are able to accomplish the directions of a simple plan for a cabin, you can move on to more
complex plans should you desire it. And all around you is the spectacular
proof of spring: thousands of wildflowers in full bloom.
What will Matt use to get his dinner and for protection.

You actually make it seem so

You actually make it seem so easy with your presentation but I find this topic
to be really something which I think I would never understand.
It seems too complex and extremely broad for me. I am looking forward for your next post, I'll try to get the
hang of it!

To dlminate the competition

To dlminate the competition in digital space,
a proper strategic approach as well as cost effective plan is necessary.

In the present times of advanced technology, every business haas
to have an online presence as internet has been considered to be the best means of finding any
product or service. The Page Rank Metric (detailed in part two of this series) is actually in the pocess of being patented by
Google, so there will be secrets, but we do know that several things go int
the calculation of which pazges show upp where on the results page.

Just would like input on

Just would like input on few general things, The website design is
perfect, the articles are very great : D.

Feel free to surf to my website ... hgh boosters

Very good blog post. I

Very good blog post. I certainly love this
website. Continue the good work!

Truly when someone doesn't

Truly when someone doesn't know after that its up to other viewers
that they will assist, so here it takes place.

Super-Duper website! I am

Super-Duper website! I am loving it!! Will be back
later to read a lot more. I am bookmarking your feeds as well.

Take a look at my weblog :: best earthing mat

Howdy! This post couldn't be

Howdy! This post couldn't be written any better! Reading this post rekinds me of my good old room mate!
He always kept talking about this. I will forward
this page to him. Pretty sure he will have a good read.

Thank you for sharing!

Hmm is anyone else

Hmm is anyone else encountering problems with the images on this blog
loading? I'm trying to find out if its a problem on my end or if it's the blog.
Any suggestions would be greatly appreciated.

I love what you guys are up

I love what you guys are up too. Such clever work and exposure!

Keep up the very good works guys I've incorporated you guys
to my blogroll.

This type of relaxed,

This type of relaxed, rhythmical, submaximal resistance training allows a higher quantity of training and therefore leads to boosted fitness.

As you pass the kettlebell in

As you pass the kettlebell in between the legs, keep a neutral
spinal column location and crease in the hips.

Laura Alton,

Laura Alton, Russian-certified kettlebell instructor; certified
trainer; physical therapist, Cooper Fitness Center, Dallas.

How You Can DO IT: Clean both

How You Can DO IT: Clean both kettlebells around the front-rack
location and also interlace your fingers.

It will be a follow along

It will be a follow along book where the viewers could do the very same diet regimen and
also exercise programs as well.

Truly fantastic information

Truly fantastic information can be seen on this web blog.

Here is my weblog - earthing material

Hi, of course this post is in

Hi, of course this post is in fact pleasant and I have
learned lot of things from it about blogging. thanks.

Attractive component &#11423f

Attractive component &#11423f content.
I ϳust stumbledd &#6489pon ʏour site and іn accession capital to ѕay thazt &#8544 ցеt
actually loved acclunt уοur blog posts.
&#5034ny &#7457ay І'll Ье subscribing in ʏߋur feeds and е&#7456еn І
fulfillment ʏou g&#6513t entry t&#1086 persistently rapidly.

Feel free tto surf tο mу &#1211omepage; Embroidery Services Singapore;
Lavina,

I am lucky that I found this

I am lucky that I found this web blog, precisely the right information that I
was trying to find!

My homepage plumbing waltham ma (www.rebelmouse.com)

d had lunch and cleared up,

d had lunch and cleared up, locked-up gone to the loo etc.

I would say if you are on the hunt for a summer home, then you've "arrived" as they say.
It gives an aura of being one in nature when constructed
in the middle of the urbanity. Muckross house is a lavish mansion that was built in 1843 for a wealthy couple.
Stitching long straight stitches by hand is a wearisome
task.

My partner and I stumbled

My partner and I stumbled over here by a different page and thought I might check things out.
I like what I see so now i'm following you. Look forward to exploring
your web page repeatedly.

Many thanks, I’ve just been

Many thanks, I’ve just been in search of information about this
topic for some time and yours is the best I’ve found until
now.

Also visit my page: Ireland SEO

You're a very persuasive

You're a very persuasive writer. I can easily see this in your article.
You've got a way how to get rid of acne scars writing
compelling information that sparks much interest.

Hello there! I could have

Hello there! I could have sworn I've visited this blog before but after
browsing through a few of the posts I realized it's new to me.
Anyways, I'm certainly happy I discovered it and I'llbe bookmarking it and
checking back regularly!

A number of search results

A number of search results will appear which relates to your forte and then choose the
first results. I imagined I would be getting a software system that was fully automated , but this was nothing like that.
Those who wonder how can I learn to invest safely in the Forex market must first understand the risk.

Keep on working, great job!

Keep on working, great job!

I know this if off topic but

I know this if off topic but I'm looking into starting
my own blog and was wondering what all is required to get set
up? I'm assuming having a blog like yours would
cost a pretty penny? I'm not very internet savvy so I'm not 100% positive.
Any suggestions or advice would be greatly appreciated.

Appreciate it

I appreciate you for sharing

I appreciate you for sharing this article, I am a big
fan of this site would like to be kept up to date.

Feel free to surf to my web-site san luis obispo website design

I loved as much as you'll

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

My website ... my latest blog post (http://applications.itbfr.org)

I am normally to running a

I am normally to running a blog and i actually appreciate your written content.

The article has truly speaks my interest. I am going
to bookmark your website and also keep checking for brand new
information.

Also visit my web blog lat=53.402043&lng=-6.407634&zoom=16

Ahaa, its nice discussion

Ahaa, its nice discussion concerning this paragraph at this place at this weblog, I have read all that, so at this time me also commenting
at this place.

I am surely bookmarking this

I am surely bookmarking this website as well as sharing it with my
acquaintances. You'll be getting a lot of visitors to your site from
me!

Also visit my web page :: SEO Ireland

Outstanding data is found on

Outstanding data is found on your blog. Keep up the excellent work.

Also visit my web-site - SEO Ireland

Use a conditioner on your

Use a conditioner on your hair that will allow it to rebuild nutrients and be more
manageable. Anagen: The growth stage that lasts around three years and the hair
grows at an average rate of half an inch per month. And it should be as
the hair adds a prominent element in the personality.

Feel free to visit my web page: hairloss

Good Job! I quite like your

Good Job! I quite like your web site. You might want to do a bit more to promote this.
Creating back links will increase your visibility hugely.

It's always best to buy backlink

I'd like to find out more?

I'd like to find out more? I'd want to find out
more details.

It helps the customer

It helps the customer acknowledge their resistance to change and then,
exactly what they could do to reach their specified goals.

Feel free to visit my web site :: nutrition health coach salary - Cathern -

It helps the customer

It helps the customer acknowledge their resistance to change and then,
exactly what they could do to reach their specified goals.

Feel free to visit my web site :: nutrition health coach salary - Cathern -

This is why some people

This is why some people collaborate with a train for a fairly brief time period
and after that efficiently preserve their modifications.

Review my weblog; nutrition advisor jobs [Monika]

Hey there, I think your

Hey there, I think your website might be having browser
compatibility issues. When I look at your website in Ie, it looks fine but when opening in Internet Explorer,
it has some overlapping. I just wanted to give you a quick heads up!
Other then that, wonderful blog!

I'm now not certain where

I'm now not certain where you're getting your info, but good topic.
I needs to spend some time studying more or figuring out more.
Thank you for great info I used to be looking for this
info for my mission.

It's a shame you don't have a

It's a shame you don't have a donate button! I'd
definitely donate to this brilliant blog! I guess for now i'll
settle for bookmarking and adding your RSS feed to my Google account.
I look forward to brand new updates and will talk about this site with my Facebook group.

Talk soon!

Feel free to surf to my web site click the next webpage [http://advanceflight.com/profile/ottoosteen]

Admiring the hard work you

Admiring the hard work you put into your site and in depth information you
offer. It's nice to come across a blog every once in a while that isn't the same outdated rehashed material.
Wonderful read! I've bookmarked your site and I'm adding your RSS feeds to
my Google account.

Here is my web page :: all kws

Some songs are based on

Some songs are based on history, such as 'City Of Steel' (an objective synopsis of the World War II battle of Stalingrad) or 'Bloodsoaked Mission' (about a
schoolmate that was killed by an IED while training Afghan police officers).
It is important, in such instances, to consider how you may add storage on your
property. Bow-tied, pencil-thin moustached maitre d's named
Enzo have been exchanged for eyelid-pierced, soul-patched
waiters named Elijah or Chloe.

It is vital that the store

It is vital that the store you purchase from is trustworthy
when you are looking to buy video clip games.
According to Inquirer, the most significant Clash
of Clans" competition in the Philippines was held on Aug. To understand a video game, technique all that you can.

Take a look at my blog - wishmastercommunity.com

Wonderful post but I was

Wonderful post but I was wanting to know if you
could write a litte more on this topic? I'd be very grateful if you could elaborate a little bit more.
Thanks!

Attention-grabbing article ,

Attention-grabbing article , I am going to spend more hours learning
about this subject.

Review my web page test x180 ignite review (finance.yahoo.com)

The software has been

The software has been meticulously created іn orɗеr t&#1211at ɑ novice can ttoo սs&#6513 thee cheats &#7457ith none
difficulties.

Have ɑ l&#1086&#11423k at my blog post: Game of War Fire Age Hack

Hozzászólás

A mező tartalma nem nyilvános.
  • Internal paths in double quotes, written as "internal:node/99", for example, are replaced with the appropriate absolute URL or relative path.
  • Engedélyezett HTML elemek: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <del> <img>
  • A webcímek és email címek automatikusan linkekké alakulnak.
  • A sorokat és bekezdéseket a rendszer automatikusan felismeri.
  • Engedélyezett HTML elemek: <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].
  • Minden email cím át lesz alakítva ember által olvasható módon, vagy (ha a JavaScript engedélyezett) ki lesz cserélve kattintható, de biztonságos hivatkozásra.