<?php

declare(strict_types=1);

if(
!defined(
'BASE_PATH'
)
){

define(
'BASE_PATH',
'/var/www/html/easychair'
);

}
/*
|--------------------------------------------------------------------------
| EASYCHAIR CLI WORKER
|--------------------------------------------------------------------------
*/

error_reporting(E_ALL);
ini_set('display_errors','1');

require_once
BASE_PATH .
'/vendor/autoload.php';

/*
|--------------------------------------------------------------------------
| LOAD CORE
|--------------------------------------------------------------------------
*/

$coreFiles=[

BASE_PATH.'/app/core/Database.php',

BASE_PATH.'/app/core/Controller.php',

BASE_PATH.'/app/core/Model.php',

BASE_PATH.'/app/helpers/functions.php',

BASE_PATH.'/app/helpers/auth.php',

BASE_PATH.'/app/helpers/permissions.php'

];

foreach($coreFiles as $f){

if(is_file($f)){

require_once $f;

}

}

if(
!class_exists(
'App\\Core\\Database'
)
){

throw new RuntimeException(
'Database class not found'
);

}

use App\Core\Database;

try{

$db=
Database::getInstance();

echo
"[OK] Database connected\n";

$stmt=
$db->query(

"

SELECT *

FROM conference_home_jobs

WHERE
job_state='pending'

ORDER BY id

LIMIT 5

"

);

$jobs=
$stmt
->fetchAll(
PDO::FETCH_ASSOC
);

if(
empty($jobs)
){

echo
"[INFO] No pending jobs\n";

exit(0);

}

foreach(
$jobs
as $job
){

$id=
(int)(
$job['id']
??0
);

echo
"[RUN] Job #"
.
$id
.
PHP_EOL;

$db
->prepare(

"

UPDATE
conference_home_jobs

SET

job_state='running',

started_at=NOW()

WHERE id=?

"

)

->execute([

$id

]);

sleep(2);

$db
->prepare(

"

UPDATE
conference_home_jobs

SET

job_state='done',

finished_at=NOW()

WHERE id=?

"

)

->execute([

$id

]);

echo
"[DONE] "
.
$id
.
PHP_EOL;

}

echo
"[SUCCESS]\n";

}
catch(
Throwable $e
){

echo
"[ERROR] "
.
$e->getMessage()
.
PHP_EOL;

exit(1);

}

