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

Very energetic article, I

Very energetic article, I enjoyed that bit. Will there be a part
2?

Also visit my web page Muscle Pharm Canada

Referring to our definition

Referring to our definition on the previous page of simulation as a spectrum
between exacting realism and simplified entertainment we can offer some explanation. Great game for up to 4 participants ' that is if you have 4 joysticks connected.

This balance between realism and entertainment (or raw action, if you
will) is really the pinnacle in defining just what a simulation game is,
and this is also the concept from which we will view the rest of our discussion.

I loved as much as you will

I loved as much as you will receive carried out right here.
The sketch is attractive, your authored subject matter stylish.
nonetheless, you command get bought an nervousness 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 hike.

Thanks for another

Thanks for another informative site. The place else may just I get
that kind of information written in such a perfect approach?
I've a project that I am simply now running
on, and I have been at the glance out for such info.

Spot on with this write-up, I

Spot on with this write-up, I seriously feel this web site needs much more
attention. I'll probably be back again to see more, thanks for the advice!

I've considering that left

I've considering that left Inobx Dollars (wasn't qualifying any longer) and Vindale Analysis (also higher of
cash out-$50-and not qualifying).

my homepage: make money online surveys no scams

I truly love the theme on

I truly love the theme on your website, I run a web site, and i would adore to utilize
this theme.

Visit my weblog Beverly Hills cosmetic dentist

Hey there! This is my first

Hey there! This is my first visit to your blog!

We are a group of volunteers and starting a new project in a community in the same niche.
Your blog provided us useful information to work on. You have done a extraordinary job!

Annualized franchises,

Annualized franchises, especially in sports, can be tough to innovate on year after year, and iteration after iteration might feel stale
or marginally enhanced. At one point I had seven of my first team out injured with a variety of ailments and building a settled team has so far proved difficult.
Take control of Terry to head the ball home from a variety of corner kicks and against
a variety of opponents on your way to winning the World Cup.

Hello There. I found your

Hello There. I found your weblog the usage of msn. This is a really well
written article. I will be sure to bookmark it and return to read more
of your helpful info. Thank you for the post.
I will definitely comeback.

I've been browsing online

I've been browsing online more than 3 hours today, yet I never found any interesting article like yours.

It is pretty worth enough for me. In my opinion, if
all site owners and bloggers made good content as you did, the web will be a
lot more useful than ever before.

Feel free to surf to my web site :: frozen ver online

Very good info. Lucky me I

Very good info. Lucky me I recently found your blog by chance (stumbleupon).
I have book marked it for later!

This excellent website

This excellent website ϲertainly Һas all the info I wantеԀ аbout this subject аnd didn't knoѡ who tο ask.

Αlso visit mmy bloog post car insurance north chatham ma

Any sort of innovative

Any sort of innovative enterprise, even these that seem muuch morde strange, can succeed.

My site; online home business in india (Filomena)

Another major difference

Another major difference between the two is that while PPC is a paid form
of advertising, organic search engine optimization is absolutely free.
Any business to conduct business on the web should have a web site created especially for creating an awareness of their products and
ser-vices to the planet at large. But it
is extremely important that anyone looking to hire an SEO professional be aware of the differences.
This mode of advertisement was faster than the earlier ones and reached more people but it had
its own limitations.

The e-mail list on the other

The e-mail list on the other hand is independent of any internet site.
You can constantly go to a various provider to e maill your audience.

Here iis my webpage: email marketing services

Obat Penurun Gula Darah Di

Oh my goodness! Incredible

Oh my goodness! Incredible article dude! Thanks, However I am experiencing issues with your RSS.
I don't know the reason why I cannot join it. Is there anyone
else getting similar RSS problems? Anybody who knows the solution can you kindly respond?
Thanx!!

Τhiѕ infoгmation iѕ

Τhiѕ infoгmation iѕ invaluable. Hߋԝ can I
find out more?

Feel free to surf to my web blog - cara pendaftaran sbobet

For most up-to-date

For most up-to-date information you have to pay a quick visit world wide web and on world-wide-web I found this
web site as a best site for latest updates.

The 3D pregnancy ultrasound

The 3D pregnancy ultrasound conducted in the 2nd
and 3rd trimester is meant so you can get a clear picture from
the placenta as well as the fetus and for providing specifics of growth and
position with the baby. Those were just one or two in the less
serious unwanted effects, which will either continue for fourteen days or up
to a month. Anything you choose to do is often a benefit and you will be ok with things right
at the end of your workout.

Feel free to visit my homepage - medicament pour tomber enceinte rapidement

Hi there to all, how is all,

Hi there to all, how is all, I think every one is getting more from
this web page, and your views are pleasant in support
of new viewers.

'Are people really searching

'Are people really searching online for my product or services'.
Thirdly, the search engines need legit companies to do site optimization. But it is extremely important
that anyone looking to hire an SEO professional be aware
of the differences. Tests with a model like Page - Rank have shown that the system is
not infallible.

Have you ever thought about

Have you ever thought about including a little bit more thann just
your articles? I mean, what you ssay is important and everything.
Butt think of if you added some great photos or video clips to
give your posts more, "pop"! Your content is excellent but
with images and clips, this website could undeniably be one of the
very best in its field. Fantastic blog!

Postcards are part of several

Postcards are part of several collections.

my webpage make money online free and fast

Hello excellent blog! Doess

Hello excellent blog! Doess running a blog similar
to this take a massive amount work? I have absolutely no
knowledge of coding but I was hoping to start my own blog in the near future.
Anyway, should yoou have any ideas or techniques for new blog owners please share.

I understand this is off subject but I simply needed to ask.
Cheers!

Even so, you need to be

Even so, you need to be highgly skeptical of an affiliate marketing forum program with no site.

My family members all the

My family members all the time say that I am wasting my
time here at web, except I know I am getting knowledge all the time by reading such
fastidious articles or reviews.

Nutritional therapists see

Nutritional therapists see people on a personal basis who desire
to think about alternative/complementary medicine.

Here is my page :: Shelley

Continue reading for your

Continue reading for your clear-cut guide to finding the nutrition advisor which corrects for you.

my webpage; Shane

Try to think of instances in

Try to think of instances in your life which could
highlight any of these qualities you especially have.

Here is my site :: Williemae

A nutritional expert is not

A nutritional expert is not managed similarly as a Dietitian.

Feel free to visit my page - Corinne

Calio takes a fresh technique

Calio takes a fresh technique to calorie checking and the iOS
application is officially offered!

Also visit my website :: Dawn

Ahaa, its good dialogue

Ahaa, its good dialogue regarding this paragraph at
this place at this blog, I have read all that, so at this time me also commenting
at this place.

Participants which sign up

Participants which sign up with the CNHC describe themselves as 'CNHC Registered Nutritional Therapists'.

My page - Aurora

There are dozens of email

There are dozens of email marketing strategy pdf advertising solutions out there.

For instance, the US Army

For instance, the US Army uses a UID system for its belongings.

The Sauna There is very

The Sauna There is very much nothing in life which someone won't
find something bad or at the least not good about. What you
have in your hands is a games console, an MP3 player, a movie player, an Internet browser, a photo viewer plus a lot lot more.
can not suppose that the pass will build it which it'll be intercepted however trust Maine it won't and it'll go straight to your player ninefold out of 10.

Feel free to visit my webpage :: tranny web cams (tinyurl.com)

An intriguing discussion is

An intriguing discussion is definitely worth comment.
I do believe that you ought to publish more about
this topic, it might not be a taboo subject but usually folks don't speak about these subjects.
To the next! Cheers!!

Also visit my web site ... Dude Perfect 2 Cheats

You likewise need to really

You likewise need to really hope that your General Practitioner acknowledges that your problem is
diet-related and therefore wants to refer accordingly.

Visit my blog post :: Ava

In Brisbane, Queensland the

In Brisbane, Queensland the mean is AU$ 49,167 to AU$ 67,921.

Also visit my web page ... Yong

Accredited to engage in diet

Accredited to engage in diet regimen and nourishment appointment.

Feel free to visit my web-site ... Thorsten

We supply expert

We supply expert evidence-based clinical nourishment
therapy to guarantee you are receiving the very
best treatment feasible.

my website; Jorja

They will have RNutrs after

They will have RNutrs after their name and will be located as a registrants with the UK Voluntary Register of Nutritionists.

Also visit my blog - Cindy

. . . . My blog post -

. . . .

My blog post - about.com

On-the-go understanding.

On-the-go understanding. Watch from your personal computer, tablet, or mobile device.

Feell free to visit my weblog; email marketing strategy best practices

I'm gone to say to my little

I'm gone to say to my little brother, that he should also pay
a visit this web site on regular basis to take updated from most recent reports.

This means not every person

This means not every person could call themselves a Dietitian.

My web blog :: Adrianne

What's up it's me, I am also

What's up it's me, I am also visiting this site daily, this website is actually good and the viewers are actually sharing pleasant
thoughts.

This free online Clash of

This free online Clash of Clans Hack I'm giving is 100% working, reputable, and free.

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.