Engine.dcl 3.91 KB
Newer Older
1 2 3 4
definition module iTasks.Engine
/**
* This module provides the iTasks engine.
* This is the primary function that creates the complete
5
* environment in which tasks can be executed.
6 7 8
*/

from Data.Maybe      import :: Maybe
9
from Data.Error      import :: MaybeError
10
from System.FilePath import :: FilePath
11
from System.Time     import :: Timespec
12
from Internet.HTTP   import :: HTTPRequest
13 14 15

import iTasks.WF.Definition

16 17 18 19 20 21 22 23 24 25
/*
* There are two ways tasks can be started:
* Interactively when a user requests it through the web,
* or directly when the application (server) is started,
*/
:: StartableTask
  = WebTask !WebTask
  | StartupTask !StartupTask

:: WebTask =
26
	{ path :: !String
27 28 29 30 31 32 33 34 35 36 37 38
	, task :: !WebTaskWrapper
	}

:: StartupTask =
	{ attributes :: !TaskAttributes 
	, task       :: !TaskWrapper
	}

:: WebTaskWrapper = E.a: WebTaskWrapper (HTTPRequest -> Task a) & iTask a
:: TaskWrapper = E.a: TaskWrapper (Task a) & iTask a

//Utility functions for creating collections of startable tasks
39 40 41 42 43 44 45
//Common cases:
onStartup :: (Task a) -> StartableTask | iTask a
onRequest :: String (Task a) -> StartableTask | iTask a

//Less common cases:
onStartupWithAttributes :: (Task a) TaskAttributes -> StartableTask | iTask a
onRequestFromRequest :: String (HTTPRequest -> Task a) -> StartableTask | iTask a
46

47
publish :== onRequestFromRequest //Backwards compatibility
48 49 50 51 52 53 54 55 56

class Startable a
where
	toStartable :: !a -> [StartableTask]

instance Startable (Task a) | iTask a //Default as web task
instance Startable (HTTPRequest -> Task a) | iTask a //As web task
instance Startable StartableTask
instance Startable [StartableTask]
57
instance Startable (a,b) | Startable a & Startable b
58

59
:: EngineOptions =
60 61
	{ appName       :: String
	, appPath       :: FilePath // Location of the application's executable
62
	, appVersion    :: String
63 64
	, serverPort    :: Int
	, serverUrl     :: String
65
	, allowedHosts  :: [String] // Only allow connections from these hosts (default ["127.0.0.1"])
66
	, keepaliveTime :: Timespec
67 68
	, sessionTime   :: Timespec
	, persistTasks  :: Bool
Bas Lijnse's avatar
Bas Lijnse committed
69
	, autoLayout    :: Bool
70
	, maxEvents     :: Int
71
	, timeout       :: Maybe Int // The timeout
72
	, distributed   :: Bool
73 74 75 76
	, sdsPort       :: Int
	, webDirPath    :: FilePath  // Location of public files that are served by the iTask webserver
	, storeDirPath  :: FilePath  // Location of the application's persistent data files 
	, tempDirPath   :: FilePath  // Location for temporary files used in tasks
77
	, byteCodePath  :: FilePath  // Location of the application's bytecode
78
	}
79
derive class iTask EngineOptions
80 81

/**
82
* Executes the task framework with a collection of startable task definitions.
83 84 85 86 87
*
* @param Tasks to start
* @param The world
* @return The world
*/
88 89
doTasks :: a !*World -> *World | Startable a
startEngine :== doTasks //Backwards compatibility
90 91 92 93 94 95 96 97

/**
* Starts the task engine with options and a list of published task definitions.
*
* @param Tasks to start
* @param An initialization function to set the engine options with:
      @param The command line arguments
      @param The default options
98 99
      @return When Ok the engine options the engine is not started,
              when Error, a message is printed to the console
100 101 102
* @param The world
* @return The world
*/
Bas Lijnse's avatar
Bas Lijnse committed
103
doTasksWithOptions :: ([String] EngineOptions -> MaybeError [String] (a,EngineOptions)) !*World
104 105 106
	-> *World | Startable a

startEngineWithOptions :== doTasksWithOptions
107 108 109 110 111

/**
* The function that takes the 'standard' command line options of an itask engine and
* shows the default help and startup message
*
112
* Essentially: doTasks = doTasksWithOptions defaultEngineCLIOptions 
113 114 115

* @param The command line arguments
* @param The default options
116 117
* @return When Ok the engine options the engine is not started,
*         when Error, the message to printed to the console
118
*/
Bas Lijnse's avatar
Bas Lijnse committed
119
defaultEngineCLIOptions :: a [String] EngineOptions -> MaybeError [String] (a, EngineOptions)
120 121 122 123 124

/**
* Determines the default options for an application
*/
defaultEngineOptions :: !*World -> (!EngineOptions,!*World)