<?php

// === Job Worker =================================================================================

/** @kphp-immutable-class */
interface KphpJobWorkerSharedMemoryPiece {}

interface KphpJobWorkerRequest {}
interface KphpJobWorkerResponse {}

class KphpJobWorkerResponseError implements KphpJobWorkerResponse {
  // Job script execution errors:
  const JOB_MEMORY_LIMIT_ERROR = -101;
  const JOB_TIMEOUT_ERROR = -102;
  const JOB_EXCEPTION_ERROR = -103;
  const JOB_STACK_OVERFLOW_ERROR = -104;
  const JOB_PHP_ASSERT_ERROR = -105;

  const JOB_CLIENT_MEMORY_LIMIT_ERROR = -1001; // client doesn't have enough memory to accept job response
  const JOB_NOTHING_REPLIED_ERROR = -2001;     // kphp_job_worker_store_response() was not succeeded

  const JOB_STORE_RESPONSE_INCORRECT_CALL_ERROR = -3000;
  const JOB_STORE_RESPONSE_NOT_ENOUGH_SHARED_MESSAGES_ERROR = -3001;
  const JOB_STORE_RESPONSE_TOO_BIG_ERROR = -3002;
  const JOB_STORE_RESPONSE_CANT_SEND_ERROR = -3003;

  public function getError() ::: string;
  public function getErrorCode() ::: int; // returns one of listed above error codes
}

/** @kphp-extern-func-info interruptible */
function job_worker_send_request(string $request, float $timeout): future<string> | false;
/** @kphp-extern-func-info interruptible */
function job_worker_send_noreply_request(string $request, float $timeout): bool;
/** @kphp-extern-func-info interruptible */
function job_worker_send_multi_request(string[] $request, float $timeout): (future<string> | false)[];
/** @kphp-extern-func-info interruptible */
function job_worker_fetch_request(): string;
/** @kphp-extern-func-info interruptible */
function job_worker_store_response(string $response): int;

function is_kphp_job_workers_enabled(): bool;

function get_job_workers_number(): int;

// === Job Worker Old API =================================================================================

/** @kphp-extern-func-info interruptible generate-stub */
function kphp_job_worker_start(KphpJobWorkerRequest $request, float $timeout): future<KphpJobWorkerResponse> | false;

/** @kphp-extern-func-info interruptible generate-stub */
function kphp_job_worker_start_no_reply(KphpJobWorkerRequest $request, float $timeout): bool;

/** @kphp-extern-func-info interruptible generate-stub */
function kphp_job_worker_start_multi(KphpJobWorkerRequest[] $request, float $timeout): (future<KphpJobWorkerResponse> | false)[];

/** @kphp-extern-func-info interruptible generate-stub */
function kphp_job_worker_fetch_request() ::: KphpJobWorkerRequest;
// returns 0 on success, < 0 - on errors. All possible error codes are constants like KphpJobWorkerResponseError::JOB_STORE_RESPONSE_*
/** @kphp-extern-func-info interruptible generate-stub */
function kphp_job_worker_store_response(KphpJobWorkerResponse $response) ::: int;
