2008-07-08

Service is now reliable

(2008-07-17) The service is now running well.  The code crashes or timesout every few days but the error handling below handles the exception and it just keeps on going. My Windows service is still not reliable.  After a few days it seems to just stop in a normal fashion.  I suppose the only thing to do is to keep monitoring.  It currently looks like this (today I have added an error message to exception handling and moved the sleep to be inside the exception handler):

#
# This is the file to run as a daemon.

LOG_FILE = 'C:\\daemon_error.log'

begin
  require 'win32/daemon'
  require 'process_files'

  include Win32

  class DemoDaemon < Daemon
    def service_main
      File.open(LOG_FILE, "a"){ |f| f.puts "Service 2008-07-07 is running #{Time.now}" }
      s = ProcessFiles.new()
      while running?
        begin
         s.getAndProcessOnce
         sleep 10

        rescue Exception => ex
           File.open(LOG_FILE, "a"){ |f| f.puts "Service had an exception #{Time.now} #{ex.class}:#{ex.message}" } 
        end
        #File.open(LOG_FILE, "a"){ |f| f.puts "Service is running #{Time.now}" }
      end
    end

    def service_stop
      File.open(LOG_FILE, "a"){ |f| f.puts "***Service responded to stop and stopped #{Time.now}" }
      exit!
    end
  end

  DemoDaemon.mainloop

rescue Exception => err
  File.open(LOG_FILE,'a+'){ |f| f.puts "***Service Failed #{Time.now} due to #{err.class}:#{err.message}" }
end

I will just have to wait and see if this works more reliably. I have waited and it is looking good! I have some plans to put in a drb client and then allow me to have a local ruby shoes interface to see how it is working.

2008-07-02

Scripting Mathematica

Update 2008-07-02
i have been trying to script a mathematica notebook that I have created that creates documents.  I found it not as straightforward as it seems.
 
I started with at test notebook:
image
My method of getting this scriptable was to save as a .m file.
Then edit it with notepad by removing some of the comments (* to this:

(* ::Package:: *)

(* ::Input:: *)
(*This is a comment.*)

(* ::Input:: *)
(*a = {1,2,3}*)

(* ::Input:: *)
Export[NotebookDirectory[] <> "test.gif",Plot[Sin[x],{x,0,10}]]

(* ::Input:: *)
Exit[]
(**)

Then run it from a batch file like this:

F:
CD "\2008\Mathematica Auto"
"C:\Program Files\Wolfram Research\Mathematica\6.0\math"  <"F:\2008\Mathematica Auto\test.m"

When I ran this didn't work as I couldn't use NotebookDirectory so I had to make the directory explicit:

(* ::Package:: *)

(* ::Input:: *)
(*This is a comment.*)

(* ::Input:: *)
(*a = {1,2,3}*)

(* ::Input:: *)
Export["F:\2008\Mathematica Auto\test.gif",Plot[Sin[x],{x,0,10}]]

(* ::Input:: *)
Exit[]
(**)

And this did run although not very elegant and I don't have a solution for running complex notebooks yet.  When you try and load a notebook Mathematica says it wants a front end.  It may be easier to script this inside Mathematica front end.

2008-06-27

Making the service a bit more robust

Windows Services series Update 2008-6-27
Having got the service up and running I wanted to be able to test the new service before running it as a service. I also needed to make it a bit more robust. This is a polled service, so every so often it will run and do something. I had my first version up and running it died but randomly after a few days (I guessed this was due to occasional problems in the guts of it eg FTP failures).
To be able to debug it easily I broke it into two parts. One file has all the guts of the service:

class ProcessFiles

def processOnce()
sleep(3)
end

end

This allows the code to be testing in a simple wrapper and run from the command line:

require 'process_files'

s = ProcessFiles.new()
while true
s.processOnce
puts "Slept for #{sleep 10} seconds"
end


Alternatively here is the a sample daemon code taken from the demo plus I added a timestamp and a comment on close:

LOG_FILE = 'C:\\test.log'
require 'process_files'


begin
require 'win32/daemon'

include Win32

class DemoDaemon < Daemon

def service_main
my_service
= ProcessFiles.new()
while running?
begin
my_service.processOnce
rescue Exception => err
File.open(LOG_FILE, "a"){ |f| f.puts "Service had a failure at #{Time.now} error=#{err} " }

sleep 10
end
sleep 3
File.open("c:\\test.log", "a"){ |f| f.puts "Service is running #{Time.now}" }
end
end

def service_stop
File.open("c:\\test.log", "a"){ |f| f.puts "***Service stopped #{
Time.now}" }
exit!
end
end

DemoDaemon.mainloop
rescue Exception => err
File.open(LOG_FILE,'a+'){ |f| f.puts " ***Daemon failure #{Time.now} err=#{err} " }
raise
end

2008-06-10

Deploying a Windows Service

Just to say I have used InnoSetup in the past and it is again useful for packaging up all the files you need.  See also the Ruby on Windows blog.

2008-06-09

Lookback Options payout

A look back option allows you to get a payout assuming perfect hindsight.  These have been used by Fung and Hsieg as a proxy for a perfect trend follower.  The payout function for an asset that varies over time is:

Lookback Option 2008-06-09_1

Lookback Option 2008-06-09_2

Lookback Option 2008-06-09_3

Lookback Option 2008-06-09_4

Lookback Option 2008-06-09_5

spikeyIcon

2008-06-06

Changing blogging editors

Just giving Windows Live Writer a try out as I had problems with scribefire as I was trying to round trip edits.  So far so good.

2008-06-04

Getting the load path right for Windows

I used a startup for my daemon like this

c:\bin\ruby\rubyw.exe c:\mydir\mydaemon.rb


However it didn't work as the default load path was not set up correctly and so it couldn't find the library files however this does work:

c:\bin\ruby\rubyw.exe -C c:\mydir mydaemon.rb

The -C option changing the working directory and so I could get my Ruby service running quietly in the background.

I will leave for another day the problem of getting spaces in the directory path - for the moment I just used a path without spaces.