Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Time Travelling in PHP

Time Travelling in PHP

A lightning talk given at PHPNW August 2013. How do we solve the problem of time-triggered events?

adrianhardy

August 06, 2013
Tweet

More Decks by adrianhardy

Other Decks in Technology

Transcript

  1. Who? Adrian Hardy I've been doing this for 10+ years

    Currently enthused by: Zend Framework 2, Laravel Magma Digital Development Team Lead Purveyor of fine software solutions
  2. What problems are we solving? Commence a process after 3

    days of inactivity Re­evaluate an environment/record every month Prevent X from happening if Y happened > $threshold ago In general, process an environment / data at a future point
  3. Dude, just use DateTime::diff() It's not about calculating elapsed time

    It's about testing your system Call me a sceptic, but I want to see it working
  4. Can we change existing code? d a t e (

    ' Y - m - d ' ) ; t i m e ( ) ; n e w D a t e T i m e ( ) ; s t r t o t i m e ( " + $ r e l a t i v e _ m o d i f i e r " ) ; N O W ( ) ; / / M y S Q L ' s e n t o u r a g e In general, code which compares against "now" scuppers us
  5. If I could start some projects again Banish anyone who

    uses MySQL's NOW() Extend DateTime, or better yet use "Carbon" Introduce indirection to determine "now" Allow "now" to be offset by a new DateTime https://github.com/briannesbitt/Carbon/pull/17
  6. Let's do this Legacy code, no easy injection method without

    disruptive changes The most surefire way of testing this is by changing our PC's clock A little clumsy This leads to nasty problems on reboot Enter: A third party tool, which intercepts calls to Linux's c l o c k _ g e t t i m e
  7. simple code / / t e s t . p

    h p e c h o d a t e ( ' r ' ) ; s l e e p ( 1 0 ) ; e c h o d a t e ( ' r ' ) ; . / f l u x - c a p a c i t o r t e s t . p h p The application doesn't pause, but the clock advances
  8. serving pages Couldn't get httpd to work with flux­capacitor :(

    But PHP 5.4's built in server does! . / f l u x - c a p a c i t o r - - i d l e n e s s = 1 1 0 0 0 0 0 0 0 - - p h p - S l o c a l h o s t : 8 0 8 0 We need to set the idleness to > 1 second to prevent flux capacitor truncating the one second socket timeout used by PHP's internal web server