OK, got it.
Hey, thanks for coming. Would you mind signing in?

sean hess

  • Twitter
  • LinkedIn
  • i.TV
    • 0
      3 Aug 2009

      Easy Daemons in PHP

      • Edit
      • Delete
      • Tags
      • Autopost

      This class and script makes it easy to turn a one-shot php script into a daemon that runs in the background on a timer. This is good for things you want to happen infrequently. For example, your site may need to use curl to fetch several different rss feeds, combining them into one feed. You don’t want to fetch all the feeds every time someone asks for the combined one. It is better to generate the combined feed every 5 minutes or so.

       

      Daemon source files

       

      daemon generate_feed.php start
      daemon generate_feed.php stop     
      daemon generate_feed.php restart

      generate_feed.php might look like this

      require_once("BlogDatabase.php");
      echo "Generating Feed"; // this will go to the log  
      
      $data = new BlogDatabase();
      $posts = $data->getAllNewPosts();
      
      foreach($posts as $post)
      {
          // generate an rss feed
      }                          
      
      file_put_contents("feed.xml", $contents);

      Alternatives

      • crontab - This is the easiest way to get a script to run on a timer, but there are a few disadvantages. Specifically, you don’t have exact control over the timer, nor can you prevent the script from running if the system isn’t ready. Refer to this article for more information.

      • System_Daemon - This was built using System_Daemon. I created this because my daemons were behaving strangely. Our database calls weren’t working properly, and things were just generally messed up.

      What it does

      This uses the pear System_Daemon to create a daemon that runs your script every 5 seconds (by default). It uses shell_exec to run it, giving you the following advantages.

      • Your script runs in a “sandboxed” environment. You don’t have to worry about stale variables or strange processes making your code behave strangely.

      • You can test your script by running it on the command-line, since this is exactly what the daemon does.

      • You can easily start and stop the daemon using the included tool.

      • It puts output from the script into the daemon’s log (/var/log/generate_feed.log on my system)

      Sorry it isn’t more flexible right now, but the timer can be easily changed in Daemon.php

      Installation

      You need to install the pear System_Daemon module. Refer to this article for instructions.

      Put your daemons in the same folder as these scripts, or else make sure Daemon.php is in your include path when you run daemon from the command line.

      anon
      there is also sonic server daemon written in PHP where you can write your own plugin and load it via command line

       

      • views
      • Tweet
      • Tweet
    • 0
      30 Jul 2009

      Page Stack - Navigate by Page Name

      • Edit
      • Delete
      • Tags
      • Autopost

      Building a navigation system in Flex is harder than it should be. Trying to figure out how to decouple your navigation model from the view is unintuitive. ViewStack is the obvious choice for it, but it leaves you with only two options, neither of which is good.

      1. Use selectedIndex - You can throw your pages/containers in a ViewStack and store the currently selected page index in a model. This works great, except that your model is coupled to the order of the pages in the ViewStack. You can store the indices in constants, but if someone adds a new page in there, it will throw off all the indices.

      2. Use selectedChild - You can mitigate the index problem by storing the selectedChildproperty on your model, but then your model has a reference to a view. Yuck.

      The solution - Page Stack

      This simple component allows you to store the name of the selected page. You can set the page. You can then store a string on your model that you bind to the selectedPage property of PageStack. If you set the name on each child of the PageStack, they’ll match up.

      MyView.mxml

      <components:PageStack selectedPage="{navigation.selectedPage}">
          <mx:Canvas name="{Navigation.FIRST}"/>
          <mx:Canvas name="{Navigation.SECOND}"/>
          <mx:Canvas name="{Navigation.THIRD}"/>                
      </components:PageStack>
      
      <mx:Button label="Go to Page 3" click="navigation.selectedPage = Navigation.THIRD"/>

      Navigation.as

      public class Navigation
      {
          public static const FIRST:String = "first";
          public static const SECOND:String = "second";
          public static const THIRD:String = "third";
              
          [Bindable]            
          public var selectedPage:String = FIRST;
      }

      And here’s the source!

      package net.seanhess.components
      {
          import flash.display.DisplayObject;
      
          import mx.containers.ViewStack;
          import mx.core.Container;
      
          public class PageStack extends ViewStack
          {
              protected var pages:Object = {};
              protected var newChildren:Boolean = false;
      
              override public function addChildAt(child:DisplayObject, index:int) : DisplayObject
              {
                  newChildren = true;
                  invalidateProperties();
                  return super.addChildAt(child, index);
              }
      
              override protected function commitProperties() : void
              {
                  super.commitProperties();
      
                  if (newChildren)
                  {
                      newChildren = false;
                      pages = {};
      
                      for each (var child:DisplayObject in getChildren())
                      {
                          var name:String = child.name;
                          pages[name] = child;
                      }
                  }
              }
      
              public function set selectedPage(value:String):void
              {
                  var child:Container = pages[value] as Container;
      
                  if (!child)
                      throw new Error("Could not find page: " + value);
      
                  selectedChild = child;
              }
      
              public function get selectedPage():String
              {
                  return selectedChild.name;            
              }
          }
      }

       

       


      Jonathan
      Sean, Nice post. I am eager to try this out and maybe solve problems with the Glue Library Example. Just to let you know your missing a " after Canvas..{Navigation.Number}. After I added the code and set "public var navigation:Navigation;" on the view I receive an error "Cannot access a property or method of a null object reference." I must be missing something pretty obvious. Thanks!


      Sean Hess
      Thanks for the catch Jonathan. I've corrected it. If I were using Glue, I would make Navigation a manager, put it in the glue map, and inject only the selectedPage to MyView. So, MyView would have public var selectedPage:String, and the page stack would just bind to that.


      Jex Chan
      Hi Sean, i just found Glue from github, it's very interested framework. is any possible you can give one example for this Page Stack sample, You said to make Navigation a manager and combine with Glue. thanks a lot

       

      • views
      • Tweet
      • Tweet
    • Search

    • Sites I Like

      • Lessons Learned
      • Both Sides of the Table 2x Entrepreneur turned VC
    • Tags

      • tutorial
      • flex
      • framework
      • php
      • ruby
      • sinatra
      • agile
      • code
      • git
      • iphone
      • sequel
      • spark
    • Archive

      • 2012 (1)
        • February (1)
      • 2011 (8)
        • December (1)
        • October (2)
        • June (2)
        • May (2)
        • January (1)
      • 2010 (4)
        • December (2)
        • May (2)
      • 2009 (7)
        • November (1)
        • August (2)
        • July (4)
    • Obox Design
  • sean hess

    Sean is cofounder and CTO of i.TV.

    62454 Views
  • Get Updates

    Follow this Space »
    You're following this Space (Edit)
    You're a contributor here (Edit)
    This is your Space (Edit)
    Follow by email »
    Get the latest updates in your email box automatically.
    Loading...
    Subscribe via RSS
    TwitterLinkedIn