peachpiecompiler / peachpie

PeachPie - the PHP compiler and runtime for .NET and .NET Core
https://www.peachpie.io
Apache License 2.0
2.33k stars 202 forks source link

hello,why hangfire job can not be invoked? #1018

Closed avriltank closed 2 years ago

avriltank commented 2 years ago

Hi,@jakubmisek,can I get some help?why hangfire job can not be invoked?I don't konw how to deal this probelm in csharp code.

Csharp code:

        public static string send(Context ctx,IPhpCallable theFunc,params PhpValue[] theParams)
        {
            try
            {
                //why peachpie code can not be invoked?
                //but next code can be invoked
                //BackgroundJob.Enqueue(() => Console.WriteLine("hello"));
                return  BackgroundJob.Enqueue(() => theFunc.Invoke(ctx, theParams));
            }
            catch (Exception ex)
            {

                throw new Exception(ex.ToString());
            }

        }

Php code:

sharpLib\Queue::delay(30000,"test",$theTime);

This is the sample code. one_web.zip

jakubmisek commented 2 years ago

thanks for the question.

What is the exception, and what is not working?

To call the sharpLib.Queue.send() from PHP, it would look like:

sharpLib\Queue::send( fn () => echo 123 );

Please note, the "Context" ctx lives only during the request.

avriltank commented 2 years ago

There is no exception.when i push job to the background queue,the request will wati for a long time until the timeout of peachpie

jakubmisek commented 2 years ago

I think we would need more information. This needs to be debugged; where is it waiting, is there anything in Output window, is there a lock? etc.

avriltank commented 2 years ago

the "Context" ctx lives only during the request.But I just wrap in to an Action for a timer or queue,the timer works well,but the queue is dead.

avriltank commented 2 years ago
                //context code is dead
                var theContext = Context.CreateEmpty();
                Console.WriteLine(theContext.ToString());//result:Pchp.Core.Context
                return BackgroundJob.Schedule(() => Console.WriteLine(theContext),TimeSpan.FromMilliseconds(3000));//there is no result after 3000ms
                //work fine code
                 var theContext = "testStr";
                return BackgroundJob.Schedule(() => Console.WriteLine(theContext),TimeSpan.FromMilliseconds(3000));//console will echo 'testStr' after 3000ms

It seems that: the hangfire job queue can't work with the Pchp.Core.Context object

jakubmisek commented 2 years ago

1/ the Context is IDisposable - you should always dispose the object 2/ what does it mean "does not work" ? You have to debug it to see what's happening

avriltank commented 2 years ago

1,yes,I just test how it works,so I create a new context 2,if it works fine,the console window will echo theContext after 3000ms with the delay job.

jakubmisek commented 2 years ago

you have to do some debugging;

avriltank commented 2 years ago

Therefore, the body of the lambda expression () => theFunc.Invoke(ctx, theParams) ends up being a InvocationExpression, not a MethodCallExpression for hangfire!So there is no result.

avriltank commented 2 years ago

Thanks for your reply,@jakubmisek