Skip to content

Commit

Permalink
Merge pull request #1 from pldin601/cron
Browse files Browse the repository at this point in the history
Add cron jobs support
  • Loading branch information
pyldin601 authored Jul 17, 2017
2 parents a12ffc0 + 05152d6 commit 11f3bc7
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 50 deletions.
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 ./
Expand All @@ -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
20 changes: 0 additions & 20 deletions app/project/handlers/fixed/DoTest.php

This file was deleted.

31 changes: 31 additions & 0 deletions app/project/handlers/fixed/cron/DoCleanFileSystem.php
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();
});
}
}
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);
}
});
}
}
11 changes: 11 additions & 0 deletions app/project/persistence/db/dao/SongDao.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ class SongDao {
const UNUSED_THRESHOLD = 2592000;
const UNUSED_PER_REQUEST_MAX = 10;

/**
* @return SelectQuery
*/
public static function scopeWithoutPeaks()
{
return (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");
}

/**
* @param $song_id
* @return mixed
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"php": ">=7.1.5",
"ext-pdo_pgsql": "*",
"josegonzalez/dotenv": "^3.0",
"php-database-migration/php-database-migration": "3.6.*"
"php-database-migration/php-database-migration": "3.6.*",
"malkusch/lock": "^1.0"
},
"autoload": {
"psr-4": {}
Expand Down
61 changes: 60 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,3 @@ services:
- /usr/app/node_modules
depends_on:
- db
links:
- db
4 changes: 2 additions & 2 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

$used_before = memory_get_usage();

require_once "../app/loader.php";
require_once "../vendor/autoload.php";
require_once APP_ROOT_DIR . "/app/loader.php";
require_once APP_ROOT_DIR . "/vendor/autoload.php";

if (file_exists(APP_ROOT_DIR . '/.env')) {
$loader = new Loader(APP_ROOT_DIR . '/.env');
Expand Down
9 changes: 4 additions & 5 deletions startup.sh
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

0 comments on commit 11f3bc7

Please sign in to comment.