1 /*******************************************************************************
2 
3     Extension to automate typical task scheduler setup.
4 
5     Reads scheduler configuration from app config from [SCHEDULER] section,
6     initializes global scheduler instance using it and provides a method to
7     start main application method inside a task.
8 
9     Expected configuration file format (all fields are optional):
10 
11     [SCHEDULER]
12     worker_fiber_stack_size = 102400
13     worker_fiber_limit = 5
14     task_queue_limit = 10
15     suspended_task_limit = 16
16     specialized_pools =
17         pkg.mod.MyTask:1024
18         pkg.mod.MyOtherTask:2048
19 
20     Copyright:
21         Copyright (c) 2017 dunnhumby Germany GmbH.
22         All rights reserved.
23 
24     License:
25         Boost Software License Version 1.0. See LICENSE_BOOST.txt for details.
26         Alternatively, this file may be distributed under the terms of the Tango
27         3-Clause BSD License (see LICENSE_BSD.txt for details).
28 
29 *******************************************************************************/
30 
31 module ocean.util.app.ext.TaskExt;
32 
33 import ocean.meta.types.Qualifiers;
34 import ocean.util.app.ext.model.IConfigExtExtension;
35 import ocean.task.Scheduler;
36 import ocean.util.config.ConfigParser;
37 import ocean.util.config.ConfigFiller;
38 
39 /// ditto
40 class TaskExt : IConfigExtExtension
41 {
42     import ocean.application.components.TaskScheduler;
43 
44     /***************************************************************************
45 
46         Default scheduler configuration to use in absence of explicit
47         configuration setting.
48 
49     ***************************************************************************/
50 
51     SchedulerConfiguration config;
52 
53     /***************************************************************************
54 
55         Constructor
56 
57         Params:
58             config = optional, scheduler configuration to use in absence
59                 of config file overrides
60 
61     ***************************************************************************/
62 
63     this ( SchedulerConfiguration config = SchedulerConfiguration.init )
64     {
65         this.config = config;
66     }
67 
68     /***************************************************************************
69 
70         Parse the configuration file options to get the scheduler configuration
71 
72         Params:
73             app = the application instance
74             parser = configuration instance
75 
76     ***************************************************************************/
77 
78     public override void processConfig ( IApplication app, ConfigParser parser )
79     {
80         scope(exit)
81             initScheduler(this.config);
82 
83         parseSchedulerConfig(parser, this.config);
84     }
85 
86     /***************************************************************************
87 
88         Wraps given delegate returning app exit status code in a new allocated
89         task object and immediately starts the scheduler event loop to handle
90         it.
91 
92         Params:
93             dg = delegate forwarding to the app entry point
94 
95         Returns:
96             app return status code
97 
98     ***************************************************************************/
99 
100     public int run ( scope int delegate () dg )
101     {
102         return runInTask(dg);
103     }
104 
105     /***************************************************************************
106 
107         Extension order. Doesn't matter as long as it happens after LogExt.
108 
109         Returns:
110             the extension order
111 
112     ***************************************************************************/
113 
114     public override int order ( )
115     {
116         return -1;
117     }
118 
119     /***************************************************************************
120 
121         Unused IConfigExtExtension method.
122 
123         We just need to provide an "empty" implementation to satisfy the
124         interface.
125 
126         Params:
127             app = the application instance
128             config = configuration instance
129 
130     ***************************************************************************/
131 
132     public override void preParseConfig ( IApplication app, ConfigParser config )
133     {
134         // Unused
135     }
136 
137 
138     /***************************************************************************
139 
140         Unused IConfigExtExtension method.
141 
142         We just need to provide an "empty" implementation to satisfy the
143         interface.
144 
145         Params:
146             app = the application instance
147             config = configuration instance
148             files = current list of configuration files to parse
149 
150         Returns:
151             new list of configuration files to parse
152 
153     ***************************************************************************/
154 
155     public override istring[] filterConfigFiles ( IApplication app,
156                                          ConfigParser config,
157                                          istring[] files )
158     {
159         // Unused
160         return files;
161     }
162 }