job snippets

How to execute a CakePHP controller's action from a cron job

Tagged job, cron, cakephp  Languages php

First copy webroot/index.php to webroot/cron_scheduler.php. Replace everything below require CORE_PATH . 'cake' . DS . 'bootstrap.php'; with the following code:

#
# BEGIN
#
# This was added to webroot/cron_scheduler.php
#

# Check that URI was specified and that we're called from the command line (not the web)
if($argc == 2 && php_sapi_name() === "cli") 
{
    # Set request URI
    $_SERVER['REQUEST_URI'] = $argv[1];
    # Set user-agent, so we can do custom processing
    $_SERVER['HTTP_USER_AGENT'] = 'cron';
    
    $Dispatcher= new Dispatcher();
    $Dispatcher->dispatch($argv[1]);
} 

#
# END
#
# 
#

Now you can execute CakePHP from the command line with the following command:

$ php cron_scheduler.php /controller/action

If you get the following error, remove the lines containing line feeds in bootstrap.php:

Warning: Cannot modify header information - headers already sent by (output started at .../app/config/b
ootstrap.php:48) in .../app/app_controller.php on line 46

How to fetch URLs in parallell with EventMachine and Ruby

Tagged asynchronous, job, ruby, eventmachine  Languages ruby

Save time by doing things in parallell:

require 'rubygems'
require 'eventmachine'
require 'open-uri'
require 'pp'
require 'thread'
require 'benchmark'

class Worker
  include EM::Deferrable

  def run
    get_google
    set_deferred_status :succeeded
  end
end

def get_google
  # sorry for spamming you
  open('http://www.google.com/') do |f|
    #pp f.meta
  end
end

def asynchronous(i)
  worker = Worker.new
  # on success
  worker.callback do
    p "#{Thread.current} done #{i}!"
  end 
  worker.errback do 
    p "Unexpected error"    
    EM.stop  
  end
  #
  Thread.new do
    worker.run
    EM.stop
  end 
  #puts "scheduling done!"
end

def synchronous(i)
  get_google
end

# on error
EM.error_handler do |e|  
  p "Unexpected error: #{e}" 
end

EM.run do
  seconds = Benchmark.realtime do
    50.times do |i|
      asynchronous i
    end
  end
  p "With EventMachine: #{seconds} elapsed..."

  seconds = Benchmark.realtime do
    50.times do |i|
      synchronous i
    end
  end
  p "Without EventMachine: #{seconds} elapsed..."
end

Output:

With EventMachine: 9.05974316596985 elapsed...
Without EventMachine: 19.1381118297577 elapsed...

Conclusion

* Speeds up blocking operations. * EventMachine is currently limited to one CPU core (native thread) per process.

References * http://ujihisa.blogspot.com/2009/08/try-eventmachine.html * http://www.scribd.com/doc/21017871/Event-Machine-Presentation

* http://www.scribd.com/doc/25939580/Event-Machine