Passed
Push — master ( 896ccb...11e3a6 )
by Fabio
07:20 queued 01:06
created

TShellCronAction::setCronModule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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
	private $_cron = false;
43
	
44
	/**
45
	 * @return string the Cron Class to find
46
	 */
47
	public function getModuleClass()
48
	{
49
		return 'Prado\\Util\\Cron\\TCronModule';
50
	}
51
	
52
	/**
53
	 * @return null|Prado\Util\Cron\TCronModule returns the Cron Module of the applications
0 ignored issues
show
Bug introduced by
The type Prado\Prado\Util\Cron\TCronModule was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
54
	 */
55
	public function getCronModule()
56
	{
57
		if ($this->_cron === false) {
58
			$app = Prado::getApplication();
59
			$moduleClass = $this->getModuleClass();
60
			$modules = $app->getModulesByType($moduleClass, false);
61
			$this->_cron = null;
62
			foreach ($modules as $id => $m) {
63
				if ($this->_cron = $app->getModule($id)) {
64
					break;
65
				}
66
			}
67
			if (!$this->_cron) {
68
				$this->_outWriter->writeError("A {$moduleClass} is not found");
69
				return null;
70
			}
71
		}
72
		if (!$this->_cron->asa(TCronModule::SHELL_LOG_BEHAVIOR)) {
73
			$this->_cron->attachBehavior(TCronModule::SHELL_LOG_BEHAVIOR, new TShellCronLogBehavior($this->getWriter()));
74
		}
75
		return $this->_cron;
76
	}
77
	
78
	/**
79
	 * @param null|Prado\Util\Cron\TCronModule $cron sets the Cron Module
80
	 */
81
	public function setCronModule($cron)
82
	{
83
		$this->_cron = $cron;
84
	}
85
	
86
	/**
87
	 * @param string[] $args the arguments to the command line action
88
	 */
89
	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

89
	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...
90
	{
91
		$module = $this->getCronModule();
92
		if (!$module) {
93
			return true;
94
		}
95
		$this->_outWriter->writeLine("\nLast Task time was " . date('Y-m-d H:i:s', $module->getLastCronTime()) . '');
96
		$module->processPendingTasks(true);
97
		return true;
98
	}
99
	
100
	/**
101
	 * Shows configured tasks and their run status.  For TDbCronModule, this also shows the
102
	 * database tasks as well.
103
	 * @param TCronModule $module the module servicing the action
104
	 * @param mixed $args
105
	 */
106
	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

106
	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...
107
	{
108
		$module = $this->getCronModule();
109
		
110
		$this->_outWriter->writeLine("\nLast cron run was " . date('Y-m-d H:i:s', $module->getLastCronTime()) . "");
111
		$this->_outWriter->writeLine("The system time is " . date('Y-m-d H:i:s') . "\n");
112
		$tasks = $module->getTasks();
113
		
114
		$this->_outWriter->writeLine("  Application Cron Tasks: ", [TShellWriter::BOLD]);
115
		if (!count($tasks)) {
116
			$this->_outWriter->writeLine("     **  There are no configured Cron Tasks.  **\n");
117
			return true;
118
		}
119
		$rows = [];
120
		foreach ($tasks as $task) {
121
			$f = 'H:i:s';
122
			if (time() - $task->getLastExecTime() > 86400) {
123
				$f = 'Y-m-d H:i:s';
124
			}
125
			$lastrun = date($f, $task->getLastExecTime());
126
			
127
			$f = 'H:i:s';
128
			$trigger = $task->getNextTriggerTime();
129
			if ($trigger - time() > 86400) {
130
				$f = 'Y-m-d H:i:s';
131
			}
132
			
133
			if (($user = $task->getUserName()) == null) {
134
				$user = $module->getDefaultUserName();
135
			}
136
			$nextrun = date($f, $trigger) . ($task->getIsPending() ? '*' : '');
137
			if ($task->getIsPending()) {
138
				$nextrun = $this->_outWriter->format($nextrun, [TShellWriter::GREEN, TShellWriter::BOLD]);
139
			}
140
			
141
			$rows[] = [$task->getName(), $task->getSchedule(), $task->getTask(), $lastrun, $nextrun, '@' . $user, $task->getProcessCount()];
142
		}
143
		$this->_outWriter->write($this->_outWriter->tableWidget(['headers' => ['Name', 'Schedule', 'Task', 'Last Run', 'Next Run', 'User', 'Run #'],
144
			'rows' => $rows]));
145
		$this->_outWriter->writeLine("\nAny 'next run' with a * means it is Pending\n");
146
		
147
		return true;
148
	}
149
	
150
	/**
151
	 * shows the registered tasks from the application for possible configuration
152
	 * or addition to TDbCronModule.
153
	 * @param TCronModule $module the module servicing the action
154
	 * @param mixed $args
155
	 */
156
	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

156
	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...
157
	{
158
		$module = $this->getCronModule();
159
		
160
		$infos = $module->getTaskInfos(true);
161
		$this->_outWriter->writeLine();
162
		$this->_outWriter->writeLine("  Registered Cron Task Information: ", [TShellWriter::BOLD]);
163
		if (!count($infos)) {
164
			$this->_outWriter->writeLine("		(** No registered application tasks **)");
165
			return true;
166
		}
167
		$rows = [];
168
		foreach ($infos as $taskinfo) {
169
			$rows[] = [
170
					$this->_outWriter->format($taskinfo->getName(), [TShellWriter::BLUE, TShellWriter::BOLD]),
171
					$taskinfo->getTask(), $taskinfo->getModuleId(), $taskinfo->getTitle()
172
				];
173
			$rows[] = ['span' => $this->_outWriter->format('      ' . $taskinfo->getDescription(), TShellWriter::DARK_GRAY)];
174
		}
175
		$this->_outWriter->write($this->_outWriter->tableWidget(['headers' => ['Task ID', 'Task', 'Module ID', 'Title'],
176
			'rows' => $rows]));
177
		$this->_outWriter->writeLine();
178
		return true;
179
	}
180
}
181