| 1 | package PAGI::FastAPI::Queue; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use v5.38; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use experimental 'class'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use version; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 2 | our $VERSION = qv('v1.7.3'); | |||||||||
| 8 | 2 | our $AUTHORITY = 'cpan:MANWAR'; | |||||||||
| 9 | |||||||||||
| 10 | 2 | ▶ | use Future::AsyncAwait; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 2 | class PAGI::FastAPI::Queue { | |||||||||
| 13 | 2 | field $driver :param = 'Memory'; | |||||||||
| 14 | 2 | field $options :param = {}; | |||||||||
| 15 | 2 | field $backend; | |||||||||
| 16 | |||||||||||
| 17 | 11 | ▶ | ADJUST { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||
| 18 | 11 | ▶ | my $driver_class = $driver =~ /^PAGI::FastAPI::Queue::Driver::/ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 19 | ? $driver | ||||||||||
| 20 | : "PAGI::FastAPI::Queue::Driver::$driver"; | ||||||||||
| 21 | |||||||||||
| 22 | 11 | (my $driver_file = "$driver_class.pm") =~ s{::}{/}g; | |||||||||
| 23 | |||||||||||
| 24 | 11 | ▶ | eval { require $driver_file; 1 } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | or die "Failed to load queue driver '$driver_class': $@"; | ||||||||||
| 26 | |||||||||||
| 27 | 9 | $backend = $driver_class->new(%$options); | |||||||||
| 28 | } | ||||||||||
| 29 | |||||||||||
| 30 | # Returns a closure suitable for Depends(); the closure ignores any | ||||||||||
| 31 | # arguments (Depends() invokes dependency code with the request | ||||||||||
| 32 | # context as its argument). | ||||||||||
| 33 | 1 | ▶ | method dep () { | ||||||||
|
Subroutine
dep: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 34 | 1 | ▶ | return sub { $self }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 35 | 2 | } | |||||||||
| 36 | |||||||||||
| 37 | 9 | ▶ | async method push ($topic, $payload) { | ||||||||
|
Subroutine
push: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 38 | 9 | return await $backend->push($topic, $payload); | |||||||||
| 39 | 2 | } | |||||||||
| 40 | |||||||||||
| 41 | 2 | ▶ | async method pop ($topic) { | ||||||||
|
Subroutine
pop: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 42 | 2 | return await $backend->pop($topic); | |||||||||
| 43 | 2 | } | |||||||||
| 44 | |||||||||||
| 45 | 7 | ▶ | async method size ($topic = undef) { | ||||||||
|
Subroutine
size: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 46 | 7 | return await $backend->size($topic); | |||||||||
| 47 | 2 | } | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | =encoding utf-8 | ||||||||||
| 51 | |||||||||||
| 52 | =head1 NAME | ||||||||||
| 53 | |||||||||||
| 54 | PAGI::FastAPI::Queue - Pluggable Async Message Queue Facade for PAGI::FastAPI | ||||||||||
| 55 | |||||||||||
| 56 | =head1 VERSION | ||||||||||
| 57 | |||||||||||
| 58 | Version v1.7.3 | ||||||||||
| 59 | |||||||||||
| 60 | =head1 SYNOPSIS | ||||||||||
| 61 | |||||||||||
| 62 | use PAGI::FastAPI::Queue; | ||||||||||
| 63 | use PAGI::FastAPI::Depends qw(Depends); | ||||||||||
| 64 | |||||||||||
| 65 | my $queue = PAGI::FastAPI::Queue->new; # Defaults to the Memory driver | ||||||||||
| 66 | |||||||||||
| 67 | $app->post('/jobs', | ||||||||||
| 68 | dependencies => [ Depends($queue->dep, key => 'queue') ], | ||||||||||
| 69 | handler => async sub ($c) { | ||||||||||
| 70 | await $c->stash->{queue}->push('emails', { to => 'a@b.com' }); | ||||||||||
| 71 | return { queued => 1 }; | ||||||||||
| 72 | }, | ||||||||||
| 73 | ); | ||||||||||
| 74 | |||||||||||
| 75 | # Selecting a driver by short name (resolved under | ||||||||||
| 76 | # PAGI::FastAPI::Queue::Driver::*) or by fully-qualified class name | ||||||||||
| 77 | my $q1 = PAGI::FastAPI::Queue->new(driver => 'Memory'); | ||||||||||
| 78 | my $q2 = PAGI::FastAPI::Queue->new( | ||||||||||
| 79 | driver => 'PAGI::FastAPI::Queue::Driver::Memory', | ||||||||||
| 80 | options => {}, | ||||||||||
| 81 | ); | ||||||||||
| 82 | |||||||||||
| 83 | =head1 DESCRIPTION | ||||||||||
| 84 | |||||||||||
| 85 | C<PAGI::FastAPI> is an asynchronous application programming interface (API) | ||||||||||
| 86 | for interacting with the L<PAGI::FastAPI::Queue::Driver> storage mechanism. | ||||||||||
| 87 | It offers an easy way to perform operations such as C<push>, C<pop>, and | ||||||||||
| 88 | C<size>, and has included L<PAGI::FastAPI::Queue::Driver::Memory> | ||||||||||
| 89 | implementation by default in the API. | ||||||||||
| 90 | |||||||||||
| 91 | Developers may create their implementations of the L<PAGI::FastAPI::Queue::Driver> | ||||||||||
| 92 | storage mechanism for other systems such as SQS, or queue on the database level. | ||||||||||
| 93 | Developers can create a new driver by extending from the L<PAGI::FastAPI::Queue::Driver::*> | ||||||||||
| 94 | API and using the class name either in the shortened form or in the long form. | ||||||||||
| 95 | A companion C<Redis> driver is available using L<PAGI::FastAPI::Queue::Driver::Redis>. | ||||||||||
| 96 | |||||||||||
| 97 | B<Unlike> L<PAGI::FastAPI::Middleware::RateLimit>, which is working with an | ||||||||||
| 98 | instance of I<object>, C<PAGI::FastAPI::Queue> expects I<name> (a string) of | ||||||||||
| 99 | the driver and creates an instance from the given class. | ||||||||||
| 100 | |||||||||||
| 101 | =head1 METHODS | ||||||||||
| 102 | |||||||||||
| 103 | =head2 C<new(%options)> | ||||||||||
| 104 | |||||||||||
| 105 | Constructs a new queue facade. | ||||||||||
| 106 | |||||||||||
| 107 | =over 4 | ||||||||||
| 108 | |||||||||||
| 109 | =item * C<driver> - (Optional) Scalar string naming the driver to use. | ||||||||||
| 110 | Defaults to C<'Memory'>. A bare name (no C<::>) is resolved as | ||||||||||
| 111 | C<PAGI::FastAPI::Queue::Driver::$name>; a string already prefixed with | ||||||||||
| 112 | C<PAGI::FastAPI::Queue::Driver::> is used as-is. Dies if the resulting | ||||||||||
| 113 | class cannot be loaded. | ||||||||||
| 114 | |||||||||||
| 115 | =item * C<options> - (Optional) HashRef of constructor arguments forwarded | ||||||||||
| 116 | to the driver class's C<new()>. Defaults to C<{}>. | ||||||||||
| 117 | |||||||||||
| 118 | =back | ||||||||||
| 119 | |||||||||||
| 120 | =head2 C<dep()> | ||||||||||
| 121 | |||||||||||
| 122 | Returns a zero-argument closure that resolves to this C<PAGI::FastAPI::Queue> | ||||||||||
| 123 | instance, suitable for use with L<PAGI::FastAPI::Depends>: | ||||||||||
| 124 | |||||||||||
| 125 | Depends($queue->dep, key => 'queue') | ||||||||||
| 126 | |||||||||||
| 127 | =head2 C<push($topic, $payload)> | ||||||||||
| 128 | |||||||||||
| 129 | Pushes C<$payload> (any scalar) onto the end of C<$topic>. Returns a | ||||||||||
| 130 | L<Future> resolving to the underlying driver's result (true on success). | ||||||||||
| 131 | |||||||||||
| 132 | =head2 C<pop($topic)> | ||||||||||
| 133 | |||||||||||
| 134 | Removes and returns a L<Future> resolving to the item at the front of | ||||||||||
| 135 | C<$topic>, or C<undef> if the topic is empty or unknown. | ||||||||||
| 136 | |||||||||||
| 137 | =head2 C<size($topic = undef)> | ||||||||||
| 138 | |||||||||||
| 139 | Returns a L<Future> resolving to the number of pending items in | ||||||||||
| 140 | C<$topic>. If C<$topic> is omitted (or C<undef>), resolves to the total | ||||||||||
| 141 | number of pending items across all topics. | ||||||||||
| 142 | |||||||||||
| 143 | =head1 SEE ALSO | ||||||||||
| 144 | |||||||||||
| 145 | L<PAGI::FastAPI::Queue::Driver>, L<PAGI::FastAPI::Queue::Driver::Memory>, | ||||||||||
| 146 | L<PAGI::FastAPI::Depends> | ||||||||||
| 147 | |||||||||||
| 148 | =head1 AUTHOR | ||||||||||
| 149 | |||||||||||
| 150 | Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >> | ||||||||||
| 151 | |||||||||||
| 152 | =head1 REPOSITORY | ||||||||||
| 153 | |||||||||||
| 154 | L<https://github.com/manwar/PAGI-FastAPI> | ||||||||||
| 155 | |||||||||||
| 156 | =head1 BUGS | ||||||||||
| 157 | |||||||||||
| 158 | Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/PAGI-FastAPI/issues>. | ||||||||||
| 159 | I will be notified and then you'll automatically be notified of progress on your | ||||||||||
| 160 | bug as I make changes. | ||||||||||
| 161 | |||||||||||
| 162 | =head1 SUPPORT | ||||||||||
| 163 | |||||||||||
| 164 | You can find documentation for this module with the perldoc command. | ||||||||||
| 165 | |||||||||||
| 166 | perldoc PAGI::FastAPI::Queue | ||||||||||
| 167 | |||||||||||
| 168 | You can also look for information at: | ||||||||||
| 169 | |||||||||||
| 170 | =over 4 | ||||||||||
| 171 | |||||||||||
| 172 | =item * BUG Report | ||||||||||
| 173 | |||||||||||
| 174 | L<https://github.com/manwar/PAGI-FastAPI/issues> | ||||||||||
| 175 | |||||||||||
| 176 | =item * Search MetaCPAN | ||||||||||
| 177 | |||||||||||
| 178 | L<https://metacpan.org/dist/PAGI-FastAPI/> | ||||||||||
| 179 | |||||||||||
| 180 | =back | ||||||||||
| 181 | |||||||||||
| 182 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 183 | |||||||||||
| 184 | Copyright (C) 2026 Mohammad Sajid Anwar. | ||||||||||
| 185 | |||||||||||
| 186 | This program is free software; you can redistribute it and/or modify it under | ||||||||||
| 187 | the terms of the Artistic License (2.0). | ||||||||||
| 188 | |||||||||||
| 189 | =cut | ||||||||||
| 190 | |||||||||||
| 191 | 2 | 1; # End of PAGI::FastAPI::Queue | |||||||||