lazy evaluation drupal camp moscow 2014

13
Lazy evaluation and asynchronous data processing in Drupal by Yevgeny Nikitin

Upload: evgeny-nikitin

Post on 29-Jul-2015

104 views

Category:

Internet


4 download

TRANSCRIPT

Page 1: Lazy evaluation drupal camp moscow 2014

Lazy evaluationand

asynchronous data processing in Drupal

by Yevgeny Nikitin

Page 2: Lazy evaluation drupal camp moscow 2014

Problem / Motivation

● Performance● Reliability● Scalability

Page 3: Lazy evaluation drupal camp moscow 2014

Process data later!

Page 4: Lazy evaluation drupal camp moscow 2014

Storage - queue

● System Queue.● Queue services (RabbitMQ, AWS SQS,

Beanstalkd, OpenStack Marconi).● Custom implementation.

Page 5: Lazy evaluation drupal camp moscow 2014

Using different queues

Override default class$conf['queue_default_class'] = 'RabbitMQQueue';

Use specific queue$conf['queue_class_{queue_name}'] = 'AwsSqsQueue';

Page 6: Lazy evaluation drupal camp moscow 2014

Add item to queuefunction example_add_item_queue($data) { $queue = DrupalQueue::get('example_queue'); $queue->createQueue(); $queue->createItem($data);}

Page 7: Lazy evaluation drupal camp moscow 2014

Define handler

● hook_cron_queue_info (core).● hook_cron (core).● hook_cronapi (ultimate_cron, elysia_cron).

Page 8: Lazy evaluation drupal camp moscow 2014

Processes management

Ultimate cron, Elysia cron:● job schedule;● parallel execution of cron jobs;● overview of cron jobs;● log history of cron jobs;● configuration per job

Page 9: Lazy evaluation drupal camp moscow 2014

Queue handlerfunction example_queue_handler() { if (!lock_acquire('example_lock')) { watchdog('example', 'Queue process is already running.', array(), WATCHDOG_WARNING); return; } $queue = DrupalQueue::get('example_queue'); $queue->createQueue(); $item = $queue->claimItem(); try { example_process_data($item->data); $queue->deleteItem($item); watchdog('example', 'Data have been processed'); } catch (Exception $e) { watchdog_exception('example', $e); } lock_release(example_lock);}

Page 10: Lazy evaluation drupal camp moscow 2014

Parallel data processing - HTTPRL

Page 11: Lazy evaluation drupal camp moscow 2014

HTTPRL examplefunction example_parallel_node_load_multiple($nids) { $nodes = array_fill_keys($nids, ''); foreach ($nodes as $nid => &$node) { // Setup callback options. $callback_options = array( array( 'function' => 'node_load', 'return' => &$node, // Blocking mode is enabled. // Setup options array. 'options' => array( 'domain_connections' => 2, // Use 2 threads for this request. ), ), $nid, ); // Queue up the request. httprl_queue_background_callback($callback_options); } httprl_send_request(); // Execute request. return $nodes;}

Page 12: Lazy evaluation drupal camp moscow 2014

Links

RabbitMQ - https://www.drupal.org/project/rabbitmqAWS SQS - https://www.drupal.org/project/aws_sqsBeanstalkd - https://www.drupal.org/project/beanstalkdUltimate cron - https://www.drupal.org/project/ultimate_cronElysia cron - https://www.drupal.org/project/elysia_cronHTTPRL - https://www.drupal.org/project/httprl

Page 13: Lazy evaluation drupal camp moscow 2014

Yevgeny NikitinSkype: yenyasinn

LinkedIn: www.linkedin.com/in/nikitinevgeny