-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmatchbot-cli.php
More file actions
executable file
·120 lines (108 loc) · 4.26 KB
/
matchbot-cli.php
File metadata and controls
executable file
·120 lines (108 loc) · 4.26 KB
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
#!/usr/bin/env php
<?php
declare(strict_types=1);
use MatchBot\Application\Commands\CallFrequentTasks;
use MatchBot\Application\Commands\CancelStaleDonationFundTips;
use MatchBot\Application\Commands\ClaimGiftAid;
use MatchBot\Application\Commands\Command;
use MatchBot\Application\Commands\CreateFictionalData;
use MatchBot\Application\Commands\DeleteOldTestFunds;
use MatchBot\Application\Commands\ExpireMatchFunds;
use MatchBot\Application\Commands\ExpirePendingMandates;
use MatchBot\Application\Commands\HandleOutOfSyncFunds;
use MatchBot\Application\Commands\LockingCommand;
use MatchBot\Application\Commands\MergeOpenApiDocs;
use MatchBot\Application\Commands\PullIndividualCampaignFromSF;
use MatchBot\Application\Commands\PullMetaCampaignFromSF;
use MatchBot\Application\Commands\PushDailyFundTotals;
use MatchBot\Application\Commands\PushDonations;
use MatchBot\Application\Commands\RedistributeMatchFunds;
use MatchBot\Application\Commands\ResetMatching;
use MatchBot\Application\Commands\RetrospectivelyMatch;
use MatchBot\Application\Commands\ScheduledOutOfSyncFundsCheck;
use MatchBot\Application\Commands\SendStatistics;
use MatchBot\Application\Commands\SetupTestMandate;
use MatchBot\Application\Commands\TakeRegularGivingDonations;
use MatchBot\Application\Commands\UpdateCampaignDonationStats;
use MatchBot\Application\Commands\UpdateCampaigns;
use MatchBot\Application\Commands\WriteSchemaFile;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleEvent;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Lock\LockFactory;
use Symfony\Component\Messenger\Command\ConsumeMessagesCommand;
$psr11App = require __DIR__ . '/bootstrap.php';
$commands = array_map($psr11App->get(...), [
// Alphabetical list:
CallFrequentTasks::class,
CancelStaleDonationFundTips::class,
ClaimGiftAid::class,
ConsumeMessagesCommand::class,
CreateFictionalData::class,
DeleteOldTestFunds::class,
ExpireMatchFunds::class,
ExpirePendingMandates::class,
HandleOutOfSyncFunds::class,
MergeOpenApiDocs::class,
PullIndividualCampaignFromSF::class,
PullMetaCampaignFromSF::class,
PushDailyFundTotals::class,
PushDonations::class,
RedistributeMatchFunds::class,
ResetMatching::class,
RetrospectivelyMatch::class,
ScheduledOutOfSyncFundsCheck::class,
SendStatistics::class,
SetupTestMandate::class,
TakeRegularGivingDonations::class,
UpdateCampaignDonationStats::class,
UpdateCampaigns::class,
WriteSchemaFile::class,
]);
$dispatcher = new EventDispatcher();
$dispatcher->addListener(ConsoleEvents::COMMAND, function (ConsoleEvent $event) use ($psr11App) {
$logger = $psr11App->get(Logger::class);
$input = $event->getInput();
if ($input->getOption(Command::CLI_OPTION_NOLOG)) {
array_filter(
$logger->getHandlers(),
(static fn ($handler) => $handler instanceof StreamHandler)
)[0]->setLevel(LogLevel::WARNING);
}
});
$cliApp = new Application();
$cliApp->setDispatcher($dispatcher);
$cliApp->getDefinition()->addOption(
new InputOption(
Command::CLI_OPTION_NOLOG,
null,
InputOption::VALUE_NONE,
'Suppresses debug & info log, show only warnings and errors'
)
);
foreach ($commands as $command) {
if ($command instanceof LockingCommand) { // i.e. not Symfony Messenger's built-in consumer.
$command->setLockFactory($psr11App->get(LockFactory::class));
$command->setLogger($psr11App->get(LoggerInterface::class));
}
$cliApp->add($command);
}
try {
// We don't want Symfony to catch any throwable because we want to catch it ourselves and log it
// instead (which should also mean it gets sent to Slack
$cliApp->setCatchExceptions(false);
$cliApp->setCatchErrors(false);
$cliApp->run();
} catch (Throwable $t) {
$logger = $psr11App->get(LoggerInterface::class);
$logger->error("CLI Error: " . $t->__toString());
$cliApp->renderThrowable($t, (new ConsoleOutput())->getErrorOutput());
exit(1);
}