root = rtrim( $root, '/' ); } /** * @param array $data * @throws \RuntimeException */ public function create( string $type, int $user_id, array $data = array() ): JobState { $id = bin2hex( random_bytes( 16 ) ); $job = JobState::create( $id, $type, $user_id, $data ); if ( ! wp_mkdir_p( $this->dir( $id ) ) ) { throw new \RuntimeException( 'Cannot create the job directory. Check permissions on wp-content/uploads.' ); } $this->save( $job ); return $job; } public function load( string $id ): ?JobState { if ( ! self::validId( $id ) ) { return null; } $raw = SafeFile::readJson( $this->dir( $id ) . '/state.json' ); if ( null === $raw ) { return null; } $job = JobState::fromArray( $raw ); return ( null !== $job && $job->id() === $id ) ? $job : null; } /** * @throws \RuntimeException */ public function save( JobState $job ): void { $job->touch(); $json = wp_json_encode( $job->toStorage() ); if ( false === $json || ! SafeFile::writeAtomic( $this->dir( $job->id() ) . '/state.json', $json ) ) { throw new \RuntimeException( 'Cannot save job state.' ); } } public function findActive( string $type, int $user_id ): ?JobState { foreach ( $this->ids() as $id ) { $job = $this->load( $id ); if ( null === $job || $job->type() !== $type || $job->isTerminal() || ( $job->userId() !== $user_id && ! $job->get( 'swapped' ) ) ) { continue; } if ( time() - $job->updatedAt() > self::STALE_AFTER ) { continue; } return $job; } return null; } /** * Takes the per-job lock so only one tick runs at a time. * * @param int $wait_ms How long to keep trying before giving up. * @return resource|null Null when another request holds the lock. */ public function acquire( JobState $job, int $wait_ms = 0 ) { $handle = fopen( $this->dir( $job->id() ) . '/state.lock', 'c' ); if ( false === $handle ) { return null; } $deadline = microtime( true ) + $wait_ms / 1000; do { if ( flock( $handle, LOCK_EX | LOCK_NB ) ) { return $handle; } usleep( 50000 ); } while ( microtime( true ) < $deadline ); fclose( $handle ); return null; } /** * @param resource $handle */ public function release( $handle ): void { flock( $handle, LOCK_UN ); fclose( $handle ); } public function requestCancel( JobState $job ): void { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_touch @touch( $this->dir( $job->id() ) . '/cancel.flag' ); } public function cancelRequested( JobState $job ): bool { return is_file( $this->dir( $job->id() ) . '/cancel.flag' ); } /** * Removes a job entirely, state included. */ public function delete( string $id ): void { if ( ! self::validId( $id ) ) { return; } $dir = $this->dir( $id ); if ( is_dir( $dir ) && Paths::contains( $this->root, $dir ) ) { SafeFile::deleteTree( $dir ); } } /** * Removes everything a finished job left behind except its state and log. * * @return bool True when nothing is left to remove. */ public function clearWorkspace( JobState $job, ?Budget $budget = null ): bool { $dir = $this->dir( $job->id() ); if ( ! is_dir( $dir ) ) { return true; } foreach ( scandir( $dir ) as $entry ) { if ( in_array( $entry, array( '.', '..', 'state.json', 'state.lock', 'job.log' ), true ) ) { continue; } if ( ! SafeFile::deleteTree( $dir . '/' . $entry, $budget ) ) { return false; } } return true; } /** * @return string[] Job ids present on disk. */ public function ids(): array { $ids = array(); if ( ! is_dir( $this->root ) ) { return $ids; } foreach ( scandir( $this->root ) as $entry ) { if ( self::validId( $entry ) && is_dir( $this->root . '/' . $entry ) ) { $ids[] = $entry; } } return $ids; } public function dir( string $id ): string { return $this->root . '/' . $id; } public static function validId( string $id ): bool { return 1 === preg_match( '/^[a-f0-9]{32}$/', $id ); } }