PHP Classes

File: src/Chronicle/Scheduled.php

Recommend this page to a friend!
  Classes of Scott Arciszewski   Chronicle   src/Chronicle/Scheduled.php   Download  
File: src/Chronicle/Scheduled.php
Role: Class source
Content type: text/plain
Description: Class source
Class: Chronicle
Append arbitrary data to a storage container
Author: By
Last change: Fix docblocks
Concurrent Chronicles

Add support for multiple instances via the ?instance=name parameter.

To implement, add something like this to your local/settings.json in the
instances key:

"public_prefix" => "table_name_prefix"

Then run bin/make-tables.php as normal.

Every instance is totally independent of each other. They have their own

* Clients
* Chain data
* Cross-Signing Targets and Policies
* Replications

If merged, I will document these features and roll it into v1.1.0
Boyscouting. Update easydb to 2.7 to eliminate boolean workaround.
Docblock consistency, fix composer internal server
Type safety
Date: 1 year ago
Size: 4,229 bytes
 

Contents

Class file image Download
<?php
declare(strict_types=1);
namespace
ParagonIE\Chronicle;

use
GuzzleHttp\Exception\GuzzleException;
use
ParagonIE\Chronicle\Exception\InstanceNotFoundException;
use
ParagonIE\Chronicle\Process\{
   
Attest,
   
CrossSign,
   
Replicate
};
use
ParagonIE\Sapient\Exception\InvalidMessageException;

/**
 * Class Scheduled
 *
 * @package ParagonIE\Chronicle
 */
class Scheduled
{
   
/** @var array<string, string> */
   
protected $settings;

   
/**
     * Scheduled constructor.
     * @param array<string, string> $settings
     */
   
public function __construct(array $settings = [])
    {
        if (empty(
$settings)) {
           
$this->settings = Chronicle::getSettings();
        }
       
$this->settings = $settings;
    }

   
/**
     * Invoked by a CLI script, this runs all of the scheduled tasks.
     *
     * @return self
     *
     * @throws Exception\ChainAppendException
     * @throws Exception\FilesystemException
     * @throws Exception\InvalidInstanceException
     * @throws Exception\ReplicationSourceNotFound
     * @throws Exception\SecurityViolation
     * @throws Exception\TargetNotFound
     * @throws GuzzleException
     * @throws InvalidMessageException
     * @throws \SodiumException
     */
   
public function run(): self
   
{
       
/** @var array<string, string> $instances */
       
$instances = $this->settings['instances'];
        if (!empty(
$instances)) {
           
// Do all of the instances:
           
foreach ($instances as $instance => $prefix) {
                try {
                   
Chronicle::setTablePrefix($prefix);
                   
$this->runAll();
                } catch (
InstanceNotFoundException $ex) {
                   
// Skip this one. It might not be created yed.
               
}
            }
            return
$this;
        }
        return
$this->runAll();
    }

   
/**
     * @return Scheduled
     * @throws Exception\ChainAppendException
     * @throws Exception\FilesystemException
     * @throws Exception\InvalidInstanceException
     * @throws Exception\ReplicationSourceNotFound
     * @throws Exception\SecurityViolation
     * @throws Exception\TargetNotFound
     * @throws GuzzleException
     * @throws InvalidMessageException
     * @throws \SodiumException
     */
   
public function runAll(): self
   
{
        return
$this
           
->doCrossSigns()
            ->
doReplication()
            ->
doAttestation()
        ;
    }

   
/**
     * Cross-sign to other Chronicles (where it is needed)
     *
     * @return self
     * @throws Exception\FilesystemException
     * @throws Exception\TargetNotFound
     * @throws GuzzleException
     * @throws InvalidMessageException
     */
   
public function doCrossSigns(): self
   
{
       
/** @var array<string, int> $row */
       
foreach (Chronicle::getDatabase()->run('SELECT id FROM chronicle_xsign_targets') as $row) {
           
$xsign = CrossSign::byId((int) $row['id']);
            if (
$xsign->needsToCrossSign()) {
               
$xsign->performCrossSign();
            }
        }
        return
$this;
    }

   
/**
     * Replicate any new records from the Chronicles we're mirroring
     *
     * @return self
     *
     * @throws Exception\InvalidInstanceException
     * @throws Exception\ReplicationSourceNotFound
     * @throws Exception\SecurityViolation
     * @throws GuzzleException
     * @throws InvalidMessageException
     * @throws \SodiumException
     */
   
public function doReplication(): self
   
{
       
$query = 'SELECT id FROM ' . Chronicle::getTableName('replication_sources');
       
/** @var array<string, int> $row */
       
foreach (Chronicle::getDatabase()->run($query) as $row) {
           
Replicate::byId((int) $row['id'])->replicate();
        }
        return
$this;
    }

   
/**
     * Run the Attestation process (if it's scheduled).
     *
     * @return self
     *
     * @throws Exception\ChainAppendException
     * @throws Exception\FilesystemException
     * @throws \SodiumException
     */
   
public function doAttestation(): self
   
{
       
$attest = new Attest($this->settings);
        if (
$attest->isScheduled()) {
           
$attest->run();
        }
        return
$this;
    }
}