-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from pldin601/cron
Add cron jobs support
- Loading branch information
Showing
10 changed files
with
138 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,16 @@ FROM pldin601/musicloud-image | |
|
||
MAINTAINER Roman Lakhtadyr <[email protected]> | ||
|
||
# Install cron | ||
RUN apt-get update && \ | ||
apt-get install -y cron && \ | ||
apt-get clean && \ | ||
|
||
{ \ | ||
echo "[program:cron]"; \ | ||
echo "command=cron -f"; \ | ||
} | tee -a /etc/supervisor/conf.d/cron.conf | ||
|
||
WORKDIR /usr/app/ | ||
|
||
COPY composer.json composer.lock ./ | ||
|
@@ -14,3 +24,9 @@ COPY . ./ | |
|
||
ARG GIT_CURRENT_COMMIT="<unknown>" | ||
ENV GIT_CURRENT_COMMIT=${GIT_CURRENT_COMMIT} | ||
|
||
ARG CRON_ENDPOINT="http://guest:please@localhost:8080/cron" | ||
RUN ({ \ | ||
echo "0 5 * * * curl -X POST ${CRON_ENDPOINT}/cleanFileSystem"; \ | ||
echo "* * * * * curl -X POST ${CRON_ENDPOINT}/generatePeaks"; \ | ||
} | tee /etc/cron.d/musicloud-cron) && chmod 0644 /etc/cron.d/musicloud-cron |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: roman | ||
* Date: 7/17/17 | ||
* Time: 17:16 | ||
*/ | ||
|
||
namespace app\project\handlers\fixed\cron; | ||
|
||
|
||
use app\core\http\HttpServer; | ||
use app\core\router\RouteHandler; | ||
use app\project\exceptions\UnauthorizedException; | ||
use app\project\persistence\fs\FileServer; | ||
use malkusch\lock\mutex\FlockMutex; | ||
|
||
class DoCleanFileSystem implements RouteHandler | ||
{ | ||
public function doPost(HttpServer $server) | ||
{ | ||
if ($server->getRemoteAddress() !== '127.0.0.1') { | ||
throw new UnauthorizedException(); | ||
} | ||
|
||
(new FlockMutex(fopen(__FILE__, 'r')))->synchronized(function () { | ||
FileServer::removeUnused(); | ||
FileServer::removeDead(); | ||
}); | ||
} | ||
} |
31 changes: 12 additions & 19 deletions
31
app/project/handlers/fixed/DoJobs.php → ...t/handlers/fixed/cron/DoGeneratePeaks.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,34 @@ | ||
<?php | ||
|
||
namespace app\project\handlers\fixed; | ||
namespace app\project\handlers\fixed\cron; | ||
|
||
use app\core\db\builder\SelectQuery; | ||
use app\core\http\HttpServer; | ||
use app\core\logging\Logger; | ||
use app\core\router\RouteHandler; | ||
use app\libs\WaveformGenerator; | ||
use app\project\models\tracklist\Songs; | ||
use app\project\exceptions\UnauthorizedException; | ||
use app\project\persistence\db\dao\SongDao; | ||
use app\project\persistence\db\tables\TSongs; | ||
use app\project\persistence\fs\FileServer; | ||
use malkusch\lock\mutex\FlockMutex; | ||
|
||
class DoJobs implements RouteHandler | ||
class DoGeneratePeaks implements RouteHandler | ||
{ | ||
public function doGet() | ||
public function doPost(HttpServer $server) | ||
{ | ||
Songs::wipeOldPreviews(); | ||
|
||
FileServer::removeUnused(); | ||
FileServer::removeDead(); | ||
|
||
$limit = 30; | ||
if ($server->getRemoteAddress() !== '127.0.0.1') { | ||
throw new UnauthorizedException(); | ||
} | ||
|
||
while ($limit-- > 0) { | ||
set_time_limit(30); | ||
(new SelectQuery(TSongs::_NAME)) | ||
->select(TSongs::FILE_NAME, TSongs::FILE_ID, TSongs::ID) | ||
->where(TSongs::PEAKS_ID . " IS NULL") | ||
->where(TSongs::FILE_ID . " IS NOT NULL") | ||
->limit(1) | ||
(new FlockMutex(fopen(__FILE__, 'r')))->synchronized(function () { | ||
SongDao::scopeWithoutPeaks() | ||
->eachRow(function ($row) { | ||
Logger::printf("Creating peaks for file: %s", $row[TSongs::FILE_NAME]); | ||
$peaks = WaveformGenerator::generate(FileServer::getFileUsingId($row[TSongs::FILE_ID])); | ||
$file_id = FileServer::registerByContent(json_encode($peaks), "application/json"); | ||
SongDao::updateSongUsingId($row[TSongs::ID], [TSongs::PEAKS_ID => $file_id]); | ||
}); | ||
sleep(1); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,5 +21,3 @@ services: | |
- /usr/app/node_modules | ||
depends_on: | ||
- db | ||
links: | ||
- db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Initialize migration | ||
composer run migrate migrate:init env | ||
|
||
# Run pending migrations | ||
composer run migrate migrate:up env | ||
# Wait for database ready to handle connections | ||
wait-for-it db:5432 -- \ | ||
composer run migrate migrate:init env && \ | ||
composer run migrate migrate:up env |