Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null Reference Error When Using RawEvent in Hangfire-Scheduled _analytics.Track Call #114

Closed
RaviAajugiya opened this issue Nov 11, 2024 · 1 comment
Assignees

Comments

@RaviAajugiya
Copy link

Describe the bug
When scheduling a Hangfire job to call _analytics.Track with a RawEvent delegate, RawEvent is found to be null, resulting in a NullReferenceException. This issue does not occur when _analytics.Track is called directly, without scheduling through Hangfire, suggesting a discrepancy in how RawEvent is instantiated or accessed within Hangfire's execution context.

To Reproduce
Steps to reproduce the behavior:

  1. Schedule ScheduleIntegrationTrialExpiringTomorrow as a background job using Hangfire.
  2. Within ScheduleIntegrationTrialExpiringTomorrow, attempt to use _analytics.Track with a delegate that modifies RawEvent.
public class HangfireService : IHangfireService
{
    private readonly Analytics _analytics;

    // Constructor that injects an Analytics instance
    public HangfireService(Analytics analytics)
    {
        _analytics = analytics;
    }

    // Method to schedule a Hangfire job that will run after a delay
    public void ScheduleIntegrationTrialJob()
    {
        // Creating a DTO with integration details to pass to the scheduled job
        var integrationDetail = new SegmentIntegrationDetailDto
        {
            CompanyIdentifier = "Company123",
            IntegrationCustomName = "Marketing Integration",
            IntegrationGuideUrl = "https://company.com/guide",
            IntegrationMessage = "Welcome to the integration",
            IntegrationName = "HubSpot Integration",
            IntegrationType = "CRM"
        };

        // Scheduling the job to run after 60 seconds
        BackgroundJob.Schedule<HangfireService>(
            x => this.ScheduleIntegrationTrialExpiringTomorrow(integrationDetail), 
            TimeSpan.FromSeconds(60)
        );
    }

    // Method called by Hangfire to track an integration trial expiring event
    public void ScheduleIntegrationTrialExpiringTomorrow(SegmentIntegrationDetailDto integrationDetail)
    {
        // First call to _analytics.Track without a RawEvent delegate - this works as expected
        _analytics.Track("integration_trial_expiring_tomorrow", integrationDetail);

        // Second call to _analytics.Track with a RawEvent delegate - this causes a NullReferenceException
        // when scheduled through Hangfire, as RawEvent is null. However, this same call works 
        // without issues when executed directly (not through Hangfire).
        _analytics.Track("integration_trial_expiring_tomorrow", integrationDetail, RawEvent =>
        {
            RawEvent.UserId = "123";  // Attempting to set the UserId, but RawEvent is null in this context
            return RawEvent;
        });
    }
}

Expected behavior
When scheduled as a background job, the RawEvent parameter is null, causing a NullReferenceException. Calling _analytics.Track directly without scheduling works as expected, even with the RawEvent delegate.

@wenxi-zeng
Copy link
Contributor

hey @RaviAajugiya, the first call works as expected because the enrichment closure/delegate is null (see this line). the second call passes a closure/delegate and that closure/delegate is invoked with the enrichment result from previous plugins (see this line). I'd suggest to check if you have any plugins that returns null after processing.

if you don't have any custom plugins, there are 2 cases you might receive null values in the closure/delegate:

  • you have a destination plugin but it's not enabled (see this line)
  • analytics is not ready (see this line). this happens when analytics failed to fetch settings from network, or there are events being tracked before analytics got settings

in either case, you should do null checks yourself to prevent NullReferenceException

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants