PHP code example of kekke / mononoke

1. Go to this page and download the library: Download kekke/mononoke library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kekke / mononoke example snippets




declare(strict_types=1);

wsSnsSqs;
use Kekke\Mononoke\Attributes\Http;
use Kekke\Mononoke\Attributes\Schedule;
use Kekke\Mononoke\Enums\Scheduler;
use Kekke\Mononoke\Service as MononokeService;

class Service extends MononokeService
{
    #[Http('GET', '/health')]
    public function status(): string
    {
        return "OK";
    }

    #[Http('GET', '/json')]
    public function json(): array
    {
        return ['status' => 'healthy'];
    }

    #[Http('GET', '/custom')]
    public function custom(): Response
    {
        return new Response(201, ['Authorization' => 'Bearer token'], 'Custom body');
    }

    #[Schedule(Scheduler::Daily)]
    public function scheduler(): void
    {
        // Runs once every day
    }

    #[AwsSnsSqs('topic-name', 'queue-name')]
    public function customSqsHandler(string $message): void
    {
        // Process message from "topic-name" via "queue-name"
    }
}

    #[Http('GET', '/health')]
    public function status(): string
    {
        return "OK";
    }

    #[Http('GET', '/json')]
    public function json(): array
    {
        return ['status' => 'healthy'];
    }

    #[Http('GET', '/custom')]
    public function custom(): \GuzzleHttp\Psr7\Response
    {
        return new \GuzzleHttp\Psr7\Response(201, ['Authorization' => 'Bearer token'], 'Custom body');
    }

    enum HttpMethod: string
    {
        case GET = 'GET';
        case POST = 'POST';
        case PUT = 'PUT';
        case DELETE = 'DELETE';
        case PATCH = 'PATCH';
        case HEAD = 'HEAD';
        case OPTIONS = 'OPTIONS';
    }

    #[Schedule(\Kekke\Mononoke\Enums\Scheduler::Daily)]
    public function dailyJob(): void
    {
        // Runs once per day
    }

    // At 14:30:00 every day
    #[Schedule(\Kekke\Mononoke\Enums\Scheduler::DailyAt)]
    public function dailyAt1430(): void
    {
        // Provide hour/minute/second via your attribute's parameters or configuration
        // e.g. #[Schedule(Scheduler::DailyAt, hour: 14, minute: 30, second: 0)]
    }

    // At minute 15 every hour
    #[Schedule(\Kekke\Mononoke\Enums\Scheduler::HourlyAt)]
    public function hourlyAt15(): void
    {
        // e.g. #[Schedule(Scheduler::HourlyAt, minute: 15, second: 0)]
    }

    enum Scheduler
    {
        case Daily;
        case DailyAt;
        case Hourly;
        case HourlyAt;
        case EveryMinute;
        case EveryMinuteAt;
        case EverySecond;

        public function         }

        public function 

    #[AwsSnsSqs('topic-name', 'queue-name')]
    public function handleMessage(string $message): void
    {
        // Process message from "topic-name" delivered through "queue-name"
        // $message is the message body (string). Parse/validate as needed.
    }

    #[AwsSnsSqs(topicName: 'sns-topic', queueName: 'sqs-queue', dlqName: 'sqs-queue-dlq')]
    public function queueWithDlq($message)
    {
        throw new Exception("Error occurred, send to dlq after 3 tries");
    }

#[Config(
    aws: new AwsConfig(dlqMaxRetryCount: 10),
)]
class Service extends MononokeService
{
    #[AwsSnsSqs(topicName: 'sns-topic', queueName: 'sqs-queue', dlqName: 'sqs-queue-dlq')]
    public function queueWithDlq($message)
    {
        throw new Exception("Error occurred, send to dlq after 3 tries");
    }
}

class Service extends MononokeService
{
    #[WebSocket(WebSocketEvent::OnOpen)]
    public function onOpened($server, $fd)
    {
        Logger::info("Opened!");
    }

    #[WebSocket(WebSocketEvent::OnMessage)]
    public function onMessage($server, $fd, $message)
    {
        Logger::info("Message received", ['message' => $message]);
        $server->push($fd, "Received: $message");
    }

    #[WebSocket(WebSocketEvent::OnClose)]
    public function onClose($server, $fd)
    {
        Logger::info("Connection closed", ['fd' => $fd]);
    }
}

    #[Http('GET', '/create')]
    public function create()
    {
        $task = new BackgroundTask($this->server);
        $task->dispatch('task_name', ['message' => 'Hello World!']);
        return "OK";
    }

    #[Task(identifier: 'task_name')]
    public function task($data)
    {
        Logger::info("Task handler executed", ['data' => $data]);
    }

#[Config(
    mononoke: new MononokeConfig(numberOfTaskWorkers: 5),
    aws: new AwsConfig(sqsPollTimeInSeconds: 10, dlqMaxRetryCount: 10),
    http: new HttpConfig(port: 8080),
)]
class Service extends MononokeService
{
    ...
}