FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
tasks/src/TaskRunner.php at develop · codeigniter4/tasks · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
codeigniter4
/
tasks
Public
Notifications
You must be signed in to change notification settings
Fork
24
Star
126
Code
Issues
4
Pull requests
2
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
tasks
/
src
/
TaskRunner.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
177 lines (145 loc) · 4.58 KB
Breadcrumbs
tasks
/
src
/
TaskRunner.php
Copy path
File metadata and controls
177 lines (145 loc) · 4.58 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
declare
(strict_types=
1
);
/**
* This file is part of CodeIgniter Tasks.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace
CodeIgniter
\
Tasks
;
use
CodeIgniter
\
CLI
\
CLI
;
use
CodeIgniter
\
I18n
\
Time
;
use
Throwable
;
/**
* Class TaskRunner
*/
class
TaskRunner
{
protected
Scheduler
$
scheduler
;
protected
?
string
$
testTime
=
null
;
/**
* Stores aliases of tasks to run
* If empty, All tasks will be executed as per their schedule
*/
protected
array
$
only
= [];
public
function
__construct
()
{
helper
(
'
setting
'
);
$
this
->
scheduler
=
service
(
'
scheduler
'
);
}
/**
* The main entry point to run tasks within the system.
* Also handles collecting output and sending out
* notifications as necessary.
*/
public
function
run
()
{
$
tasks
=
$
this
->
scheduler
->
getTasks
();
if
(
$
tasks
=== []) {
return
;
}
foreach
(
$
tasks
as
$
task
) {
// If specific tasks were chosen then skip executing remaining tasks
if
(
$
this
->
only
!== [] && !
in_array
(
$
task
->
name
,
$
this
->
only
,
true
)) {
continue
;
}
if
(!
$
task
->
shouldRun
(
$
this
->
testTime
) &&
$
this
->
only
=== []) {
continue
;
}
$
error
=
null
;
$
start
= Time::
now
();
$
output
=
null
;
$
this
->
cliWrite
(
'
Processing:
'
. (
$
task
->
name
?:
'
Task
'
),
'
green
'
);
try
{
$
output
=
$
task
->
run
();
$
this
->
cliWrite
(
'
Executed:
'
. (
$
task
->
name
?:
'
Task
'
),
'
cyan
'
);
}
catch
(
Throwable
$
e
) {
$
this
->
cliWrite
(
'
Failed:
'
. (
$
task
->
name
?:
'
Task
'
),
'
red
'
);
log_message
(
'
error
'
,
$
e
->
getMessage
(),
$
e
->
getTrace
());
$
error
=
$
e
;
}
finally
{
// Save performance info
$
taskLog
=
new
TaskLog
([
'
task
'
=>
$
task
,
'
output
'
=>
$
output
,
'
runStart
'
=>
$
start
,
'
runEnd
'
=> Time::
now
(),
'
error
'
=>
$
error
,
]);
$
this
->
updateLogs
(
$
taskLog
);
}
}
}
/**
* Specify tasks to run
*/
public
function
only
(
array
$
tasks
= []):
TaskRunner
{
$
this
->
only
=
$
tasks
;
return
$
this
;
}
/**
* Sets a time that will be used.
* Allows setting a specific time to test against.
* Must be in a DateTime-compatible format.
*/
public
function
withTestTime
(
string
$
time
):
TaskRunner
{
$
this
->
testTime
=
$
time
;
return
$
this
;
}
/**
* Write a line to command line interface
*/
protected
function
cliWrite
(
string
$
text
, ?
string
$
foreground
=
null
)
{
// Skip writing to cli in tests
if
(
defined
(
'
ENVIRONMENT
'
) &&
ENVIRONMENT
===
'
testing
'
) {
return
;
}
if
(!
is_cli
()) {
return
;
}
CLI
::
write
(
'
[
'
.
date
(
'
Y-m-d H:i:s
'
) .
'
]
'
.
$
text
,
$
foreground
);
}
/**
* Adds the performance log to the
*/
protected
function
updateLogs
(
TaskLog
$
taskLog
)
{
if
(
setting
(
'
Tasks.logPerformance
'
) ===
false
) {
return
;
}
// "unique" name will be returned if one wasn't set
$
name
=
$
taskLog
->
task
->
name
;
$
error
=
null
;
if
(
$
taskLog
->
error
instanceof
Throwable) {
$
error
=
"
Exception:
{
$
taskLog
->
error
->
getCode
()}
-
{
$
taskLog
->
error
->
getMessage
()}"
.
PHP_EOL
.
"
file:
{
$
taskLog
->
error
->
getFile
()}
:
{
$
taskLog
->
error
->
getLine
()}"
;
}
$
data
= [
'
task
'
=>
$
name
,
'
type
'
=>
$
taskLog
->
task
->
getType
(),
'
start
'
=>
$
taskLog
->
runStart
->
format
(
'
Y-m-d H:i:s
'
),
'
duration
'
=>
$
taskLog
->
duration
(),
'
output
'
=>
$
taskLog
->
output
??
null
,
'
error
'
=>
$
error
,
];
// Get existing logs
$
logs
=
setting
(
"
Tasks.log-
{
$
name
}"
);
if
(
empty
(
$
logs
)) {
$
logs
= [];
}
// Make sure we have room for one more
/** @var int $maxLogsPerTask */
$
maxLogsPerTask
=
setting
(
'
Tasks.maxLogsPerTask
'
);
if
((
is_countable
(
$
logs
) ?
count
(
$
logs
) :
0
) >
$
maxLogsPerTask
) {
array_pop
(
$
logs
);
}
// Add the log to the top of the array
array_unshift
(
$
logs
,
$
data
);
setting
(
"
Tasks.log-
{
$
name
}"
,
$
logs
);
}
}
Back
|
FazBrowse Home
|
New Git URL