43
43
44
44
class SchedulerShell extends Shell{
45
45
46
- public $ tasks = [];
47
-
48
- /**
49
- * The array of scheduled tasks.
50
- */
46
+ /**
47
+ * The array of scheduled tasks.
48
+ */
51
49
private $ schedule = [];
52
50
53
- /**
54
- * The key which you set Configure::read() for your jobs
55
- */
51
+ /**
52
+ * The key which you set Configure::read() for your jobs
53
+ */
56
54
private $ configKey = 'SchedulerShell ' ;
57
55
58
- /**
59
- * The path where the store file is placed. null will store in Config folder
60
- */
61
- private $ storePath = null ;
56
+ /**
57
+ * The path where the store file is placed. null will store in Config folder
58
+ */
59
+ private $ storePath = TMP ;
62
60
63
- /**
64
- * The file name of the store
65
- */
61
+ /**
62
+ * The file name of the store
63
+ */
66
64
private $ storeFile = 'cron_scheduler.json ' ;
67
65
68
- /**
69
- * The number of seconds to wait before running a parallel SchedulerShell
70
- */
71
- private $ processingTimeout = 600 ;
66
+ /**
67
+ * The file name of the processing flag file (indicates that existing job is running)
68
+ */
69
+ private $ processingFlagFile = ' .cron_scheduler_processing_flag ' ;
72
70
73
- /**
74
- * The main method which you want to schedule for the most frequent interval
75
- */
71
+ /**
72
+ * The number of seconds to wait before running a parallel SchedulerShell
73
+ */
74
+ private $ processingTimeout = 600 ;
76
75
77
- /**
78
- * main function.
79
- *
80
- * @access public
81
- * @return void
82
- */
76
+ /**
77
+ * The main method which you want to schedule for the most frequent interval
78
+ *
79
+ * @access public
80
+ * @return void
81
+ */
83
82
public function main () {
84
83
85
84
// read in the config
@@ -93,6 +92,10 @@ public function main() {
93
92
$ this ->storeFile = $ config ['storeFile ' ];
94
93
}
95
94
95
+ if (isset ($ config ['processingFlagFile ' ])) {
96
+ $ this ->processingFlagFile = $ config ['processingFlagFile ' ];
97
+ }
98
+
96
99
if (isset ($ config ['processingTimeout ' ])) {
97
100
$ this ->processingTimeout = $ config ['processingTimeout ' ];
98
101
}
@@ -110,17 +113,17 @@ public function main() {
110
113
$ this ->runjobs ();
111
114
}
112
115
113
- /**
114
- * The connect method adds tasks to the schedule
115
- *
116
- * @access public
117
- * @param string $name - unique name for this job, isn't bound to anything and doesn't matter what it is
118
- * @param string $interval - date interval string "PT5M" (every 5 min) or a relative Date string "next day 10:00"
119
- * @param string $task - name of the cake task to call
120
- * @param string $action - name of the method within the task to call
121
- * @param array $pass - array of arguments to pass to the method
122
- * @return void
123
- */
116
+ /**
117
+ * The connect method adds tasks to the schedule
118
+ *
119
+ * @access public
120
+ * @param string $name - unique name for this job, isn't bound to anything and doesn't matter what it is
121
+ * @param string $interval - date interval string "PT5M" (every 5 min) or a relative Date string "next day 10:00"
122
+ * @param string $task - name of the cake task to call
123
+ * @param string $action - name of the method within the task to call
124
+ * @param array $pass - array of arguments to pass to the method
125
+ * @return void
126
+ */
124
127
public function connect ($ name , $ interval , $ task , $ action = 'main ' , $ pass = []) {
125
128
$ this ->schedule [$ name ] = [
126
129
'name ' => $ name ,
@@ -133,18 +136,18 @@ public function connect($name, $interval, $task, $action = 'main', $pass = []) {
133
136
];
134
137
}
135
138
136
- /**
137
- * Process the tasks when they need to run
138
- *
139
- * @access private
140
- * @return void
141
- */
139
+ /**
140
+ * Process the tasks when they need to run
141
+ *
142
+ * @access private
143
+ * @return void
144
+ */
142
145
private function runjobs () {
143
- $ dir = new Folder (TMP );
146
+ $ dir = new Folder ($ this -> storePath );
144
147
145
148
// set processing flag so function takes place only once at any given time
146
- $ processing = count ($ dir ->find ( ' \.scheduler_running_flag ' ) );
147
- $ processingFlag = new File ( $ dir -> slashTerm ( $ dir -> pwd ()) . ' .scheduler_running_flag ' );
149
+ $ processingFlag = new File ($ dir ->slashTerm ( $ dir -> pwd ()) . $ this -> processingFlagFile , false );
150
+ $ processing = $ processingFlag -> exists ( );
148
151
149
152
if ($ processing && (time () - $ processingFlag ->lastChange ()) < $ this ->processingTimeout ) {
150
153
$ this ->out ("Scheduler already running! Exiting. " );
@@ -154,10 +157,6 @@ private function runjobs() {
154
157
$ processingFlag ->create ();
155
158
}
156
159
157
- if (!$ this ->storePath ) {
158
- $ this ->storePath = TMP ;
159
- }
160
-
161
160
// look for a store of the previous run
162
161
$ store = "" ;
163
162
$ storeFilePath = $ this ->storePath .$ this ->storeFile ;
0 commit comments