Passed
Push — master ( 5fd064...896ccb )
by Fabio
04:59
created

TShellCronAction::listTaskInfos()   B

Complexity

Conditions 8
Paths 68

Size

Total Lines 37
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 28
c 1
b 0
f 0
nc 68
nop 1
dl 0
loc 37
rs 8.4444
1
<?php
2
3
/**
4
 * TShellCronAction class file.
5
 *
6
 * @author Brad Anderson <[email protected]>
7
 * @link https://github.com/pradosoft/prado
8
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
9
 */
10
 
11
namespace Prado\Util\Cron;
12
13
use Prado\Prado;
14
use Prado\Shell\TShellAction;
15
use Prado\Shell\TShellWriter;
16
use Prado\Util\Cron\TCronModule;
17
18
/**
19
 * TShellCronAction class.
20
 *
21
 * Runs the TCronModule from the command line.   This will run all pending
22
 * tasks when the "cron" command is run.  Other sub-commands are "info" and
23
 * "tasks"; where "info" reports the possible cron tasks registered in the
24
 * application, and "tasks" reports the installed tasks being managed.
25
 *
26
 * @author Brad Anderson <[email protected]>
27
 * @package Prado\Util\Cron
28
 * @since 4.2.0
29
 */
30
class TShellCronAction extends TShellAction
31
{
32
	protected $action = 'cron';
33
	protected $methods = ['run', 'tasks', 'index'];
34
	protected $parameters = [null, null, null];
35
	protected $optional = [null, null, null];
36
	protected $description = ['Manages Cron time-based services',
37
		'Runs the Cron Pending Tasks.',
38
		'Displays the Cron tasks configured in the application.',
39
		'Displays the registered Cron tasks information.'
40
	];
41
	
42
	/**
43
	 * @return string the Cron Class to find
44
	 */
45
	public function getModuleClass()
46
	{
47
		return 'Prado\\Util\\Cron\\TCronModule';
48
	}
49
	
50
	/**
51
	 * @retutrn null|Prado\Util\Cron\TCronModule returns the Cron Module of the applications
52
	 */
53
	public function getCronModule()
54
	{
55
		$app = Prado::getApplication();
56
		$moduleClass = $this->getModuleClass();
57
		$modules = $app->getModulesByType($moduleClass, false);
58
		$module = null;
59
		foreach ($modules as $id => $m) {
60
			if ($module = $app->getModule($id)) {
61
				break;
62
			}
63
		}
64
		if (!$module) {
65
			$this->_outWriter->writeError("A {$moduleClass} is not found");
66
			return null;
67
		}
68
		if (!$module->asa(TCronModule::SHELL_LOG_BEHAVIOR)) {
69
			$module->attachBehavior(TCronModule::SHELL_LOG_BEHAVIOR, new TShellCronLogBehavior($this->getWriter()));
70
		}
71
		return $module;
72
	}
73
	
74
	/**
75
	 * @param string[] $args the arguments to the command line action
76
	 */
77
	public function actionRun($args)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

77
	public function actionRun(/** @scrutinizer ignore-unused */ $args)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
78
	{
79
		$module = $this->getCronModule();
80
		if (!$module) {
81
			return true;
82
		}
83
		$this->_outWriter->writeLine("\nLast Task time was " . date('Y-m-d H:i:s', $module->getLastCronTime()) . '');
0 ignored issues
show
Bug introduced by
The method getLastCronTime() does not exist on Prado\TModule. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

83
		$this->_outWriter->writeLine("\nLast Task time was " . date('Y-m-d H:i:s', $module->/** @scrutinizer ignore-call */ getLastCronTime()) . '');
Loading history...
84
		$module->processPendingTasks(true);
0 ignored issues
show
Bug introduced by
The method processPendingTasks() does not exist on Prado\TModule. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
		$module->/** @scrutinizer ignore-call */ 
85
           processPendingTasks(true);
Loading history...
85
		return true;
86
	}
87
	
88
	/**
89
	 * Shows configured tasks and their run status.  For TDbCronModule, this also shows the
90
	 * database tasks as well.
91
	 * @param TCronModule $module the module servicing the action
92
	 * @param mixed $args
93
	 */
94
	public function actionTasks($args)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

94
	public function actionTasks(/** @scrutinizer ignore-unused */ $args)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
95
	{
96
		$module = $this->getCronModule();
97
		
98
		$this->_outWriter->writeLine("\nLast cron run was " . date('Y-m-d H:i:s', $module->getLastCronTime()) . "");
99
		$this->_outWriter->writeLine("The system time is " . date('Y-m-d H:i:s') . "\n");
100
		$tasks = $module->getTasks();
0 ignored issues
show
Bug introduced by
The method getTasks() does not exist on Prado\TModule. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

100
		/** @scrutinizer ignore-call */ 
101
  $tasks = $module->getTasks();
Loading history...
101
		
102
		$this->_outWriter->writeLine("  Application Cron Tasks: ", [TShellWriter::BOLD]);
103
		if (!count($tasks)) {
104
			$this->_outWriter->writeLine("     **  There are no configured Cron Tasks.  **\n");
105
			return true;
106
		}
107
		$rows = [];
108
		foreach ($tasks as $task) {
109
			$f = 'H:i:s';
110
			if (time() - $task->getLastExecTime() > 86400) {
111
				$f = 'Y-m-d H:i:s';
112
			}
113
			$lastrun = date($f, $task->getLastExecTime());
114
			
115
			$f = 'H:i:s';
116
			$trigger = $task->getNextTriggerTime();
117
			if ($trigger - time() > 86400) {
118
				$f = 'Y-m-d H:i:s';
119
			}
120
			
121
			if (($user = $task->getUserName()) == null) {
122
				$user = $module->getDefaultUserName();
0 ignored issues
show
Bug introduced by
The method getDefaultUserName() does not exist on Prado\TModule. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

122
				/** @scrutinizer ignore-call */ 
123
    $user = $module->getDefaultUserName();
Loading history...
123
			}
124
			$nextrun = date($f, $trigger) . ($task->getIsPending() ? '*' : '');
125
			if ($task->getIsPending()) {
126
				$nextrun = $this->_outWriter->format($nextrun, [TShellWriter::GREEN, TShellWriter::BOLD]);
127
			}
128
			
129
			$rows[] = [$task->getName(), $task->getSchedule(), $task->getTask(), $lastrun, $nextrun, '@' . $user, $task->getProcessCount()];
130
		}
131
		$this->_outWriter->write($this->_outWriter->tableWidget(['headers' => ['Name', 'Schedule', 'Task', 'Last Run', 'Next Run', 'User', 'Run #'],
132
			'rows' => $rows]));
133
		$this->_outWriter->writeLine("\nAny 'next run' with a * means it is Pending\n");
134
		
135
		return true;
136
	}
137
	
138
	/**
139
	 * shows the registered tasks from the application for possible configuration
140
	 * or addition to TDbCronModule.
141
	 * @param TCronModule $module the module servicing the action
142
	 * @param mixed $args
143
	 */
144
	public function actionIndex($args)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

144
	public function actionIndex(/** @scrutinizer ignore-unused */ $args)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
145
	{
146
		$module = $this->getCronModule();
147
		
148
		$infos = $module->getTaskInfos(true);
0 ignored issues
show
Bug introduced by
The method getTaskInfos() does not exist on Prado\TModule. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

148
		/** @scrutinizer ignore-call */ 
149
  $infos = $module->getTaskInfos(true);
Loading history...
149
		$this->_outWriter->writeLine();
150
		$this->_outWriter->writeLine("  Registered Cron Task Information: ", [TShellWriter::BOLD]);
151
		if (!count($infos)) {
152
			$this->_outWriter->writeLine("		(** No registered application tasks **)");
153
			return true;
154
		}
155
		$rows = [];
156
		foreach ($infos as $taskinfo) {
157
			$rows[] = [
158
					$this->_outWriter->format($taskinfo->getName(), [TShellWriter::BLUE, TShellWriter::BOLD]),
159
					$taskinfo->getTask(), $taskinfo->getModuleId(), $taskinfo->getTitle()
160
				];
161
			$rows[] = ['span' => $this->_outWriter->format('      ' . $taskinfo->getDescription(), TShellWriter::DARK_GRAY)];
162
		}
163
		$this->_outWriter->write($this->_outWriter->tableWidget(['headers' => ['Task ID', 'Task', 'Module ID', 'Title'],
164
			'rows' => $rows]));
165
		$this->_outWriter->writeLine();
166
		return true;
167
	}
168
}
169