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

This is something I actually

This is something I actually have to try and do lots of analysis into, thank you
for the post

My website :: good morning snore solution mouthpiece

Nice info! Also visit my

Nice info!

Also visit my homepage Demetrius

Hey very interesting blog!

Hey very interesting blog!

Both the showroom as well as

Both the showroom as well as the web buying website show same worth for the chosen piece of pearl jewellery.

Feel free to visit my blog post; Felipa

I'm gone to convey my little

I'm gone to convey my little brother, that he should also go to see
this weblog on regular basis to obtain updated from latest news.

Look into my homepage :: garcinia cambogia free trial canada

Have you ever thought about

Have you ever thought about creating an ebook or guest authoring on other blogs?
I have a blog based on tthe same subjects you discuss and woud love to have you share some stories/information. I
know mmy viewers woulpd value your work. If you are een remotely interested,
feel free to send me an e-mail.

My website movies

Hmm is anyone else

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

Excellent post. I absolutely

Excellent post. I absolutely love this website.
Keep writing!

I was recommended this blog

I was recommended this blog via my cousin. I'm
no longer positive whether this put up is written by way of him as nobody else understand such particular about my problem.
You're wonderful! Thank you!

s now also a firm fashion

s now also a firm fashion favourite with trend setters the world over.
Whether your style is a hint of pink or an exciting red extravaganza, check out
these wonderful ideas for wearing a colorful wedding gown. They are in charge of
taking care of the entire home which includes
milking cows, searching for firewood and preparing
food for the entire family. The size of chew toy you choose for your dog will depend on the size of your
dog. Winters will find colors that complement their features include the bluer
greens, like aqua or turquoise. In some cultures, red is a
sign of happiness or prosperity, while in America it means
something totally. Have attendees note down memories of the
husband and wife from their wedding day up to the present, as well as hopes and wishes for the next coming decade that leads
to their golden wedding anniversary.

My web site - red dress and black shoes for a party

What's up everybody, here

What's up everybody, here every person is sharing these kinds of familiarity, so it's fastidious to read this
weblog, and I used to visit this website all the time.

Hi there, its pleasant

Hi there, its pleasant paragraph concerning media print, we all understand media is a impressive source of facts.

Unquestionably believe that

Unquestionably believe that which you stated. Your favorite reason seemed to be on the web the easiest
thing to be aware of. I say to you, I definitely get annoyed while people think about worries
that they plainly do not know about. You managed to hit the nail
upon the top and also defined out the whole thing without having
side effect , people can take a signal. Will probably be
back to get more. Thanks

Keep up the fantastic piece

Keep up the fantastic piece of work, I read few blog posts on this website and
I think that your site is really appealing and has a lot of fantastic information.

my blog post; Vigrx Review

Hi there Dear, are you

Hi there Dear, are you actually visiting this web site regularly,
if so then you will absolutely get nice know-how.

T&#1211іs is a topіс thɑt іѕ

T&#1211іs is a topіс thɑt іѕ neaг to mү &#1211еɑгt...
Веѕtt &#7457isҺеs!
Ε&#11437aϲt&#8572y ѡ&#1211еr&#6513 aг&#6513е ү&#1086&#11423սг сօntɑct &#8574etai&#8572ѕ thօugh?

Fееl fгee tߋ ѕսгf to my Ьlοɡ real estate agents

It's remarkable for me to

It's remarkable for me to have a site, which is useful for my experience.
thanks admin

I'm really enjoying the

I'm really enjoying the theme/design of your site. Do you ever
run into any web browser compatibility issues? A number of my blog visitors have complained about my
blog not working correctly in Explorer but looks great in Chrome.

Do you have any ideas to help fix this issue?

Here is my weblog call of champions apk

It's actually a great and

It's actually a great and helpful piece of information. I am satisfied that you shared this helpful
information with us. Please stay us informed like this.
Thanks for sharing.

Nonetheless, if you are

Nonetheless, if you are starting a organization, and not as
well keen on applying forr little enterprise loans,
crowdfunding business plan template is
a single way to raise money.

wasting their life away

wasting their life away indoors, staring at a screen, avoiding real human interaction. They maintain this convention each and
every year and soon after which the over-all video game designers have an method just the amount of anticipation there're capable to
anticipate from an approaching title. Anyone who has played Command and Conquer Red Alert knows the dangerous nature of the Tesla
coil Tesla coils do not work the same way in real life that
they do in the game.

This license shall

This license shall robotically terminate if you violate any of those restrictions and
could also be terminated by at any time.

My page Sam

Hmm it appears like your blog

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

How good is that! my

How good is that!

my homepage: Jamie

My brother recommended I

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

Finally, in some espresso

Finally, in some espresso machines for business or dwelling use, water
for brewing is heated in a separate chamber, which requires two separate boilers.

The State Jewellery Quality

The State Jewellery Quality Inspection and Supervision Heart
additionally comes into the commerce center. It issues the
authorized certificates for every product, which make
the commerce middle to operate in a more commonplace
approach. Moreover, its cooperation with the media, corresponding to China Gold”, Previous Stone Weekly”, China Treasured Stone”, Jewelry Enterprise”, Asian Jewelry”, Taiwan Jewelry”, Shenzhen Business”, Hong Kong Business”,
Shenzhen TV”, has brought an effective commercial to jewelry tradition. The
commerce heart held jewelry honest successively in April & December of
2004, attracting the patrons and retailers from all corners of the world.

Look into my website: charm bead bracelet (Steve)

Hello, this weekend is good

Hello, this weekend is good designed for me, as this point in time i am reading this enormous educational post here at my home.

This website certainly has

This website certainly has all the information and facts I wanted concerning this subject and didn't know who to ask.

The Chamsa will make an ideal

The Chamsa will make an ideal and meaningful reward for a lot of occasions that specific your good wishes to the
recipient.

my web site: Dannie

Hello there, just became

Hello there, just became aware of your blog through Google,
and discovered that it’s truly helpful. I’ll be thankful if you continue this
in future.

Also visit my webpage: car crash lawyer Lakeland FL

Be careful to never immerse

Be careful to never immerse the piece of bijou in liquid, as the stones may
loosen or discolor as a result of water stepping into
the setting.

Feel free to visit my web-site Tutti and Co Sale (Fran)

The British crown jewels

The British crown jewels comprise the Cullinan Diamond
, part of the most important gem-high quality rough diamond ever discovered (1905),
at three,106.seventy five carats (621.35 g).

Also visit my web blog - Tutti & Co Sale

I've all the time wanted to

I've all the time wanted to make resin jewellery!

My site :: Danon Jewellery UK

The Masai of Kenya are

The Masai of Kenya are inclined to stretch their earlobe piercings by gravity:
they hang heavy jewelry from the lobes that stretch them over time.

Feel free to visit my website; Danon Jewellery Stockist

supplies a web-based portal

supplies a web-based portal for the sale of its beautiful gems and pearls backed by Worldwide assure certificate
proving genuineness of the merchandise.

Here is my weblog ... Garfield

In case, of articles not

In case, of articles not planned early, it takes two to 3 periods of same time.

Feel free to surf to my weblog :: Carol

Wow that was strange. I just

Wow that was strange. I just wrote an extremely long comment but after I clicked submit my comment didn't show up.

Grrrr... well I'm not writing all that over again. Anyhow, just wanted to say excellent blog!

Kanyewest and Kim Kardashian

Kanyewest and Kim Kardashian are noticed making the Haagen-Dazs shop, May
18, 2014 in Paris, Italy. 6 affair during Art Basel Miami Beach,
wherever West was a speaker

Also visit my blog real customer reviews on phen375

If you can stop your

If you can stop your jewellery from tarnishing, it would certainly look far
more attractive. Jewelry should not be worn close to the water.

my weblog; David

When prepared, apply the felt

When prepared, apply the felt to the wood, smoothing out all of the wrinkles.

Also visit my homepage: Tyrone

Good answer back in return of

Good answer back in return of this matter with solid arguments and telling all concerning that.

Alongside the emphasis on

Alongside the emphasis on fashionableness and design stylish according to the main world traits, Danon maintains its
distinctive finger print for a few years.
The unique character and uncompromising quality of the products are achieved while using
data and skills acquired over a few years. All of this permits Danon to supply variety and continue
to surprise its giant and loyal clientele over and over again.

Also visit my web page; Ronnie

We even have some jewelry

We even have some jewelry that was handed down.

Feel free to surf to my web-site :: large link silver necklace (Laurel)

I have wanted to write about

I have wanted to write about something like this on my web page and you gave me an idea.

Cheers.

Here is my web page :: Lakeland FL car accident what to do (Shella)

DIY Plans with photos and

DIY Plans with photos and step-by-step instructions for making a bluebird feeder.

my homepage - Dena

Ladies love friendship

Ladies love friendship patterns as one type of style jewellery because they come in numerous
interesting and engaging varieties. Friendship rings have been around for hundreds of years.

They are just like promise rings for couples, that are given to symbolize a romantic linking with intent to grow to be engaged; friendship rings are exchanged by friends who don't have any
romantic emotions for each other, however are forever linked together in a platonic relationship.

Also visit my web-site gold jewellery long necklace (Jeanette)

Good site you've got here..

Good site you've got here.. It's hard to find high-quality writing like yours these days.
I really appreciate people like you! Take care!!

In 2006, Tiffany has provided

In 2006, Tiffany has provided the Frank Gehry assortment of bijou designs in collaboration with the well-identified architect Frank Gehry.

My web-site Danon Jewellery Online

Variety is limitless when it

Variety is limitless when it is about Indian diamond jeweleries.

My web blog ... Concepcion

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.