jamesmh / coravel

Near-zero config .NET library that makes advanced application features like Task Scheduling, Caching, Queuing, Event Broadcasting, and more a breeze!
https://docs.coravel.net/Installation/
MIT License
3.66k stars 242 forks source link

Running invocable from UI throws error. #196

Closed VictorioBerra closed 3 years ago

VictorioBerra commented 3 years ago

image

Works when scheduled from the UI. But not when ran from Jobs page.

Error:

      Content root path: C:\Users\toryb\source\repos\CoravelSampleApp\CoravelSampleApp
info: CoravelSampleApp.Invocables.MyCliCreatedInvocable[0]
      Sleepin for 3755
info: CoravelSampleApp.Invocables.MyCliCreatedInvocable[0]
      Sleepin for 3044
info: CoravelSampleApp.Invocables.MyCliCreatedInvocable[0]
      Sleepin for 4220
fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
      An unhandled exception has occurred while executing the request.
System.MissingMethodException: Method not found: 'Void Coravel.Queuing.Interfaces.IQueue.QueueAsyncTask(System.Func`1<System.Threading.Tasks.Task>)'.
   at Coravel.Pro.Features.Jobs.UseCases.RunJobCommand.QueueAndTrackJob(String typeName, String displayName)
   at Coravel.Pro.Coravel.Pages.JobsModel.OnPostInvokeJobAsync(InvokeJobModel model)
   at lambda_method(Closure , Object , Object[] )
   at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.ExecutorFactory.ActionResultHandlerMethod.Execute(Object receiver, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeHandlerMethodAsync()
   at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeNextPageFilterAsync()
   at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.Rethrow(PageHandlerExecutedContext context)
   at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeInnerFilterAsync()
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Dotnet info:

.NET Core SDK (reflecting any global.json):
 Version:   3.1.403
 Commit:    9e895200cd

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19041
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\3.1.403\

Host (useful for support):
  Version: 3.1.9
  Commit:  774fc3d6a9

.NET Core SDKs installed:
  3.1.403 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download

Project FIle:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <CopyRefAssembliesToPublishDirectory>false</CopyRefAssembliesToPublishDirectory>
  </PropertyGroup>

  <ItemGroup>
    <None Remove="mydb.db" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="coravel" Version="4.0.2" />
    <PackageReference Include="Coravel.Pro" Version="4.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.1.9" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.9">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.9" />
  </ItemGroup>

</Project>

Startup

using System.Reflection;
using Coravel.Pro;
using CoravelSampleApp.data;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace CoravelSampleApp
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<ApplicationDbContext>(options => {
                options.UseSqlite("Data Source=.\\mydb.db;", liteOpts => {
                    liteOpts.MigrationsAssembly(Assembly.GetAssembly(typeof(Startup)).FullName);
                });
            });

            services
                .AddRazorPages()
                .AddNewtonsoftJson();

            services.AddCoravelPro(typeof(ApplicationDbContext));
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
            });

            app.UseCoravelPro();
        }
    }
}

Incoable

using Coravel.Invocable;
using Microsoft.Extensions.Logging;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace CoravelSampleApp.Invocables
{
    public class MyCliCreatedInvocable : IInvocable
    {
        private readonly Random rand;
        private readonly ILogger<MyCliCreatedInvocable> logger;

        public MyCliCreatedInvocable(ILogger<MyCliCreatedInvocable> logger)
        {
            // Seed
            rand = new Random();
            this.logger = logger;
        }

        public Task Invoke()
        {
            var sleepValue = rand.Next(1000, 5001);
            logger.LogInformation("Sleeping for {sleepTime}", sleepValue);
            Thread.Sleep(sleepValue);
            return Task.CompletedTask;
        }
    }
}
jamesmh commented 3 years ago

Thanks for the details. I'll try to have a look at this soon 👍

jamesmh commented 3 years ago

So there were some breaking changes from Coravel version 4.0.0 +

The quick fix for now is downgrade coravel explicitly to 3.6.1, and I'll have a fix for Coravel Pro soon.

VictorioBerra commented 3 years ago

Thanks @jamesmh that fixed it.

jamesmh commented 3 years ago

Awesome - real fix coming soon 😅

VictorioBerra commented 3 years ago

Any way we can have some unit or integration tests to catch that next time?

jamesmh commented 3 years ago

Yes, that's def. something I've been wanting to incorporate for some time....

4.0.1 should be available in the next 10 min or so. Let me know if that fixes for you.

Thanks!

VictorioBerra commented 3 years ago

That worked. I see we have some unit/integration tests. What type of tests would you like to incorporate that you do not already have today?

VictorioBerra commented 3 years ago

Ohh I see the bug is in Coravel.Pro which is closed source.